2012-10-21 73 views
0

假設我有3個符號鏈接(指向不同的部署版本文件)。重命名shell腳本中的符號鏈接

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

當我做了新的部署(叫2H),我想最終的結果看起來像:

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

,我已經是A,B的名字的唯一信息,並C和H的名字.K,L,M的名字是未知的,但可以通過遵循符號鏈接找到。

我該如何在bash shell中爲此做一個腳本?

(注:A,B,C是使用ln -s創建符號鏈接)

+1

您需要的是['readlink'(http://man.cx/readlink)工具 –

+0

謝謝,我認爲,應該讓我走在最前面。從未聽過這個命令。 – LazyCubicleMonkey

回答

1

我不明白你爲什麼會需要知道你需要刪除舊文件name..unless。我寫得很快,看起來很好。它不需要舊的發佈文件名稱。

!/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