bash脚本错误让: - :语法错误:操作数预期(错误标记为“ - ”)

时间:2013-11-18 15:12:16

标签: bash shell

鉴于下面的脚本,有人可以帮我解决这个错误吗?该脚本计算自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=$1 '
    {
            DAYOFYEAR=$1
            CURRENTYEAR=$2
            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 $1 in

    "")
        echo -e "Usage: $0 username"
        ;;

    *)
        SEVENTY=$(daysSince1970)
        PWCD=$(grep $1 /etc/shadow| awk -F":" '{print $3}')
        PWED=$(grep $1 /etc/shadow| awk -F":" '{print $5}')

        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 "$1 password good $LEFT to expire\n"
        fi
        ;;
esac

1 个答案:

答案 0 :(得分:6)

删除空格。而不是说:

let LEFT=PWED - $PWTIME

说:

let LEFT=PWED-PWTIME

但这很脆弱。除非你知道你在做什么,否则你应该使用bash的“Arithmetic Context”:

((LEFT = PWED - $PWTIME))