2012-10-21 27 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