0

Lets say I have 3 symbolic links (pointing to different deployment version files).

A -> K
B -> L
C -> M

When I do a new deploy (called H), I would like the end result to look like:

A -> H
B -> K
C -> L

The only information that I have are the names of A, B, and C, and the name of H. The names of K, L, M are unknown me but could be found by following the symbolic links.

How would I make a script in bash shell that does that for me?

(Note: A, B, C are the symbolic links created using ln -s)

4

1 回答 1

1

我不明白为什么您需要知道旧文件名..除非您需要删除它。我写得很快,它似乎工作正常。它不需要旧的发布文件名。

!/bin/bash
remove_old()
    {
            rm -f "$1"
    }
make_new()
    {
            ln -s "$1" "$2"
    }

if [ $# -ne 3 ]; then
    {
        echo "Nope, you need to provide..."
        echo "(1) link/file name to be removed."
        echo "(2) link/file name to be created"
        echo "(3) the actual file location for #2"      
    }   
    elif [ ! -f "${3}" ]; then
    {
        echo "Sorry, your target ${3} doesn’t exist or is not accessible"
    }
    elif [ -f ${2} ]; then
    {
        echo "Sorry, the new file already exists...want me to remove it?"
        read remove
        if [ "$remove" = "Y" ]||[ "$remove" = "y"]; then
            {
                rm -f "$2"
                remove_old $1
                make_new    $3 $2   
            }
            else
            {
                exit
            }
        fi               
    }
    else
    {
        remove_old $1
        make_new $3 $2
    }
    fi

这是调试后的输出......

bash -xv autolinker.sh my_link my_new_link test_2/test_file 
#!/bin/bash
remove_old()
    {
            rm -f "$1"
    }
make_new()
    {
            ln -s "$1" "$2"
    }

if [ $# -ne 3 ]; then
    {
        echo "Nope, you need to provide..."
        echo "(1) link/file name to be removed."
        echo "(2) link/file name to be created"
        echo "(3) the actual file location for #2"      
    }   
    elif [ ! -f "${3}" ]; then
    {
        echo "Sorry, your target ${3} doesn’t exist or is not accessible"
    }
    elif [ -f ${2} ]; then
    {
        echo "Sorry, the new file already exists...want me to remove it?"
        read remove
        if [ "$remove" = "Y" ]||[ "$remove" = "y"]; then
            {
                rm -f "$2"
                remove_old $1
                make_new    $3 $2   
            }
            else
            {
                exit
            }
        fi               
    }
    else
    {
        remove_old $1
        make_new $3 $2
    }
    fi
+ '[' 3 -ne 3 ']'
+ '[' '!' -f test_2/test_file ']'
+ '[' -f my_new_link ']'
+ echo 'Sorry, the new file already exists...want me to remove it?'
Sorry, the new file already exists...want me to remove it?
+ read remove
Y
+ '[' Y = Y ']'
+ rm -f my_new_link
+ remove_old my_link
+ rm -f my_link
+ make_new test_2/test_file my_new_link
+ ln -s test_2/test_file my_new_link
于 2012-12-28T17:34:53.030 回答