Ubuntu 16.04 上的 Amazon CLI bash 脚本,if 语句上的 "unexpected operator"(语法看起来正确)
Amazon CLI bash script on Ubuntu 16.04, "unexpected operator" on if statements (syntax looks correct)
我正在尝试解决在 Ubuntu 服务器 16.04 上 运行ning 的 Amazon CLI 备份脚本遇到的问题。
当我手动 运行 脚本时,我看到我的 if 语句抛出错误。根据请求,我已将我的示例简化为最小且可验证的。该脚本 运行ning 在另一台生产服务器上运行良好。我还通过 shell 语法检查器 运行 它,据我所知语法看起来是正确的。我错过了什么?
此外,语法检查器希望我将所有变量用双引号引起来。为什么?
错误:
backup.sh: 34: [: Friday: unexpected operator
示例:
#!/bin/bash
################
# User Settings:
################
# your AWS bucket name
aws_bucket="s3://mybucket"
# your server path to AWS
aws_path="/usr/bin/aws"
weekly_backup_day="Friday"
# the (2 digit?) date you want your monthly backup run
monthly_backup_date="01"
################
# Set Date Variables
################
dayOfWeek="$(date +'%A')"
monthNumber=$(date + '%m')
dateOfMonth=$(date '+%d')
################
# Run Weekly
################
if [ $# -lt 1 ] && [ "$dayOfWeek" == "$weekly_backup_day" ] ; then
echo "Running Weekly"
$aws_path s3 sync "$aws_bucket"/daily/"$dayOfWeek" "$aws_bucket"/weekly/"$dateOfMonth" --delete
fi
这中断了:
monthNumber=$(date + '%m')
必须
monthNumber=$(date +'%m')
但我怀疑这只是一个打字错误,因为它会阻止您的错误消息出现。
您可能 运行 如下所示:
sh ./backup.sh
这意味着 #!/bin/bash
行被忽略,并且 POSIX 模式中的 Bash 或另一个 shell 完全是 运行。 ==
不是POSIX,所以你要改成
if [ $# -lt 1 ] && [ "$dayOfWeek" == "$weekly_backup_day" ]
至
if [ $# -lt 1 ] && [ "$dayOfWeek" = "$weekly_backup_day" ]
或运行它与
bash ./backup.sh
或者只是
./backup.sh
考虑:
$ sh -c '[ 1 == 1 ] && echo Yes' # Can't have "==" in sh
sh: 1: [: 1: unexpected operator
$ sh -c '[ 1 = 1 ] && echo Yes' # "=" is okay
Yes
$ bash -c '[ 1 == 1 ] && echo Yes' # Bash does not care either way
Yes
$ bash -c '[ 1 = 1 ] && echo Yes'
Yes
$ bash -c '(( 1 == 1 )) && echo Yes' # (( )) is a Bashism
Yes
至于"why quote",总结为"because of word splitting and glob expansion"。 Wooledge wiki 有一个关于它的good article。
我正在尝试解决在 Ubuntu 服务器 16.04 上 运行ning 的 Amazon CLI 备份脚本遇到的问题。
当我手动 运行 脚本时,我看到我的 if 语句抛出错误。根据请求,我已将我的示例简化为最小且可验证的。该脚本 运行ning 在另一台生产服务器上运行良好。我还通过 shell 语法检查器 运行 它,据我所知语法看起来是正确的。我错过了什么?
此外,语法检查器希望我将所有变量用双引号引起来。为什么?
错误:
backup.sh: 34: [: Friday: unexpected operator
示例:
#!/bin/bash
################
# User Settings:
################
# your AWS bucket name
aws_bucket="s3://mybucket"
# your server path to AWS
aws_path="/usr/bin/aws"
weekly_backup_day="Friday"
# the (2 digit?) date you want your monthly backup run
monthly_backup_date="01"
################
# Set Date Variables
################
dayOfWeek="$(date +'%A')"
monthNumber=$(date + '%m')
dateOfMonth=$(date '+%d')
################
# Run Weekly
################
if [ $# -lt 1 ] && [ "$dayOfWeek" == "$weekly_backup_day" ] ; then
echo "Running Weekly"
$aws_path s3 sync "$aws_bucket"/daily/"$dayOfWeek" "$aws_bucket"/weekly/"$dateOfMonth" --delete
fi
这中断了:
monthNumber=$(date + '%m')
必须
monthNumber=$(date +'%m')
但我怀疑这只是一个打字错误,因为它会阻止您的错误消息出现。
您可能 运行 如下所示:
sh ./backup.sh
这意味着 #!/bin/bash
行被忽略,并且 POSIX 模式中的 Bash 或另一个 shell 完全是 运行。 ==
不是POSIX,所以你要改成
if [ $# -lt 1 ] && [ "$dayOfWeek" == "$weekly_backup_day" ]
至
if [ $# -lt 1 ] && [ "$dayOfWeek" = "$weekly_backup_day" ]
或运行它与
bash ./backup.sh
或者只是
./backup.sh
考虑:
$ sh -c '[ 1 == 1 ] && echo Yes' # Can't have "==" in sh
sh: 1: [: 1: unexpected operator
$ sh -c '[ 1 = 1 ] && echo Yes' # "=" is okay
Yes
$ bash -c '[ 1 == 1 ] && echo Yes' # Bash does not care either way
Yes
$ bash -c '[ 1 = 1 ] && echo Yes'
Yes
$ bash -c '(( 1 == 1 )) && echo Yes' # (( )) is a Bashism
Yes
至于"why quote",总结为"because of word splitting and glob expansion"。 Wooledge wiki 有一个关于它的good article。