0

I am a beginner at Bash scripting and my script is not behavioring properly. I do not know how to fix it or the proper way to code it. The user should only be able to enter one argument. ($ nowOn f132a99) If you enter more then one argument, then it will prompt the Please enter a single, valid user id: " I am not sure how to fix this.

$ nowOn f132a99 = Correct output

$ nowOn f132a99 f132a98 = Error on Line 1 Binary Operator Expected

$ nowOn f132a99 f132a98 f132a97 = Error on Line 1 Too Many Arguments

if [ -z "$@" ]; then
    echo -n "Please enter a single, valid user id: "
    read userid
else
    userid="$@"
fi

#user validation check
if ! grep -q "$userid" /etc/passwd >/dev/null 2>&1;then
        echo
        echo "The user you entered, $userid is not a valid user on this system."
        exit 2
else
        #login check
         if ! who | grep "$userid" >/dev/null 2>&1;then
                echo
                echo `grep "$userid" /etc/passwd | cut -d: -f5 | sort | sed 's/^\(.*\), \(.*\)$/\2 \1/'` is NOT currently logged on
                exit 1
else
        #login check
        echo
        echo `grep "$userid" /etc/passwd | cut -d: -f5 | sort | sed 's/^\(.*\), \(.*\)$/\2 \1/'` is currently logged on.
        exit 0
        fi
fi

I have also tried the following code down below but if you enter more then one argument (nowOn f132a99 f132a97) then it will just display the results for f132a99 and disregard the rest, and not echo Please enter a single, valid user id:

if [ -z "$1" ]; then
    echo -n "Please enter a single, valid user id: "
    read userid
else
    userid="$1"
fi
  • You have a lot of problems than just validating user input. – Jetchisel Apr 11 at 4:40
  • For the requirement of the assignment, the code works and does what it needs to do. What problems do I have? – Derek Apr 11 at 8:19
  • Notice that your "redacted" stuff is still available in the history, see it here. – mosvy Apr 13 at 7:01
  • I rolled your edit back. The question is useless without code. – Kusalananda Apr 13 at 7:13
0

Solved it using:

if [ $# -ne 1 ]; then
    echo -n "Please enter a single, valid user id: "
    read userid
else
    userid="$1"
fi
| improve this answer | |
0

Yes, your test [ -z "$@" ] does not work as you expect it to do. The "$@" will expand to each individual command line argument, quoted, which gives you a test that essentially looks like [ -z "f132a99" "f132a98" "f132a97" ] for three arguments.

What you should do, if you want the user to only ever give a single argument, is exactly what you mention in your own answer, i.e. use a test like [ "$#" -ne 1 ] to test for invalid input.

However, there is another option, and that is to consider all of the given arguments as user IDs, and loop over them:

if [ -z "$1" ]; then
    echo 'No user ID given' >&2
    exit 1
fi

for userid do
    # code to process "$userid" goes here
done

Other notes about your code:

The test for valid user IDs is flawed. Consider a system where there is no user called bill, but where there is a billy. Entering bill as the user ID would not detect this as an invalid user ID with grep -q "$userid" /etc/passwd (this command would possibly also generate an error if the $userid string started with a dash).

It would be better to test with getent passwd "$userid", and to all operation related to getting data out of the passwd database through getent passwd.

You have a similar issue with who | grep "$userid", which is better written as who | grep -q "^$userid\>".

I mentioned these things in my answer to your previous question.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.