3

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.

#!/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
  • On which line is this error? Commented Nov 18, 2013 at 15:15

1 Answer 1

6

Remove the spaces. Instead of saying:

let LEFT=PWED - $PWTIME

say:

let LEFT=PWED-PWTIME

But this is brittle. Unless you know what you're doing, you should use bash's "Arithmetic Context":

((LEFT = PWED - $PWTIME))
Sign up to request clarification or add additional context in comments.

2 Comments

or use ((LEFT = PWED - $PWTIME)) since it will handle spaces correctly.
@AaronDigulla Yes, arithmetic context is always better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.