LOCAL_DIR变量预先设置脚本当前目录(完全不是我期望的)

时间:2013-06-07 22:27:32

标签: bash shell

考虑以下简单的rsync脚本我试着打起来:

#!/bin/bash
PROJECT="$1"
USER=stef
LOCAL_DIR="~/drupal-files/"

REMOTE_HOST="hostname.com"
REMOTE_PROJECTS_PATH=""

# Should not have anything to change below
PROJECT_LIST="proj1 proj2 proj3 quit"

echo "/nSelect project you wish to rsync\n\n"

select PROJECT in $PROJECT_LIST
do
  if [ "$PROJECT" = "quit" ]; then
   echo
   echo  "Quitting $0"
   echo
   exit
fi
echo "Rsynching $PROJECT from $REMOTE_HOST into" $LOCAL_DIR$PROJECT
rsync -avzrvP $USER@$REMOTE_HOST:/var/projects/$PROJECT/ $LOCAL_DIR$PROJECT
done
echo "Rsync complete."
exit;

rsync命令中设置的变量$LOCALDIR$PROJECT始终包含脚本路径:

输出

Rsynching casa from hostname.com.com into ~/drupal-files/casa
opening connection using: ssh -l stef hostname.com rsync --server --sender -vvlogDtprz                   e.iLsf . /var/groupe_tva/casa/
receiving incremental file list
rsync: mkdir "/home/stef/bin/~/drupal-files/proj1" failed: No such file or directory (2)
rsync error: error in file IO (code 11) at main.c(605) [Receiver=3.0.9]

mkdir的行不应该有/ home / stef / bin,为什么bash在变量上添加脚本的运行目录?

由于

1 个答案:

答案 0 :(得分:4)

LOCAL_DIR="~/drupal-files/"

字符串在引号中,因此有路径名扩展,变量将包含文字字符串。

删除引号。

$ x="~/test"; echo $x
~/test
$ x=~/test; echo $x
/home/user/test