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