在进入shell脚本的过程中,我遇到了第二个障碍。我似乎无法将我的if语句评估为null值的true。我试图将一个参数传递给脚本,并让脚本让我知道我是否缺少必需的变量。不幸的是,它继续落入else语句,即使它应该将变量评估为null。这是脚本:
if [ "$1" == "d" ]; then
if [ -n "$2" ]; then
echo "Please enter the local location of the files"
else
LOCAL=$2
scp -r -i ~/Dropbox/Business/aws/first.pem $LOCAL ubuntu@54.XXX.194.202:~/test/
fi
else
scp -r -i ~/Dropbox/Business/aws/first.pem ~/Dropbox/Business/aws/files/binaryhustle/ ubuntu@54.XXX.194.202:~/test/
fi
这是我运行的命令来执行脚本:`bash copyfile.sh d
我得到的回应是:
usage: scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
[-l limit] [-o ssh_option] [-P port] [-S program]
[[user@]host1:]file1 ... [[user@]host2:]file2
`
答案 0 :(得分:0)
总结评论,添加一个功能以消除代码重复:
#!/bin/bash
keyfile=~/Dropbox/Business/aws/first.pem
destination=ubuntu@54.XXX.194.202:~/test/
default_source=~/Dropbox/Business/aws/files/binaryhustle/
do_copy() {
source=$1
scp -r -i $keyfile "$source" "$destination"
}
if [ "$1" == "d" ]; then
if [ -z "$2" ]; then
echo "Please enter the local location of the files"
else
do_copy "$2"
fi
else
do_copy "$default_source"
fi