bash 脚本错误让:-:语法错误:预期操作数(错误标记为“-”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20051146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash script error let: -: syntax error: operand expected (error token is "-")
提问by Steve Maher
Given the script below, can someone help me get past this error ? The script calculates days since the unix epoch and given the password expiration date, comes back telling me how many days till they are gonna get locked out. Its the let/math that is getting me. Ive tried quotes, different spacing around the minus operator all to no avail.
鉴于下面的脚本,有人可以帮助我克服这个错误吗?该脚本计算自 Unix 纪元以来的天数并给出密码到期日期,然后返回告诉我他们将被锁定的天数。它让我得到了让/数学。我试过引号,减号运算符周围的不同间距都无济于事。
#!/usr/bin/bash
#
# Filename: pwx
# Description: Script to tell when A password for a user expires
# Usage: pwx username
#
function daysSince1970 {
[[ -x /usr/bin/nawk ]] && AWK=/usr/bin/nawk || AWK=/usr/bin/awk
date +"%j %Y" | $AWK -v VERBOSE= '
{
DAYOFYEAR=
CURRENTYEAR=
DAYS=-1 # Because it is not 1 day since 01/01/1970 until 02/01/1970.
if (VERBOSE) { printf("%8s%8s%8s\n","Year","Days","Total") }
for (YEAR=1970; YEAR < CURRENTYEAR; YEAR++) {
if (YEAR % 4 == 0) {
if (YEAR % 100 == 0) {
if (YEAR % 1000 == 0) {
YEARDAYS=366
} else {
YEARDAYS=365
}
} else {
YEARDAYS=366
}
} else {
YEARDAYS=365
}
DAYS+=YEARDAYS
if (VERBOSE) { printf("%8s%8d%8d\n",YEAR,YEARDAYS,DAYS) }
}
DAYS+=DAYOFYEAR
if (VERBOSE) { printf("%8s%8d",YEAR,DAYOFYEAR) }
printf("%8d\n",DAYS)
}'
}
case in
"")
echo -e "Usage: let LEFT=PWED - $PWTIME
username"
;;
*)
SEVENTY=$(daysSince1970)
PWCD=$(grep /etc/shadow| awk -F":" '{print }')
PWED=$(grep /etc/shadow| awk -F":" '{print }')
let PWTIME=SEVENTY-PWCD
if [[ $PWTIME -gt $PWED ]]
then
echo -e "Password expired"
else
let LEFT=PWED - $PWTIME ####This is the line that is erroring out
echo -e " password good $LEFT to expire\n"
fi
;;
esac
回答by devnull
Remove the spaces. Instead of saying:
删除空格。而不是说:
let LEFT=PWED-PWTIME
say:
说:
((LEFT = PWED - $PWTIME))
But this is brittle. Unless you know what you're doing, you should use bash's "Arithmetic Context":
但这很脆弱。除非您知道自己在做什么,否则您应该使用 bash 的“算术上下文”:
##代码##