简体   繁体   English

LOCAL_DIR变量在脚本的当前目录前添加(完全不是我所期望的)

[英]LOCAL_DIR variable prepends the scripts current directory (totally not what I expect)

Consider the following simple rsync script I am tryint to slap up: 考虑下面的简单rsync脚本,我是tryint拍的:

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

The variable $LOCALDIR$PROJECT set in the rsync command always includes the scripts path, : 在rsync命令中设置的变量$LOCALDIR$PROJECT始终包含脚本路径:

OUTPUT : 输出

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]

The line with mkdir should not have /home/stef/bin, why is bash adding the script's running dir on the variable? mkdir的行不应包含/ home / stef / bin,为什么bash在变量上添加了脚本的运行目录?

Thanks 谢谢

LOCAL_DIR="~/drupal-files/"

The string is in quotes so there's pathname expansion, and the variable will contain the literal string. 该字符串用引号引起来,因此扩展了路径名,并且变量将包含文字字符串。

Remove the quotes. 删除引号。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM