-1

在我的 shell 脚本编写过程中,我遇到了第二个障碍。对于空值,我似乎无法让我的 if 语句评估为真。我正在尝试将参数传递给脚本,并让脚本让我知道我是否缺少所需的变量。不幸的是,它一直落入 else 语句,即使它应该将变量评估为空值。这是脚本:

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 `

4

1 回答 1

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
于 2013-09-01T09:00:38.350 回答