I have to print information about the user with the name given as a parameter. For example let's say
./script1.sh John should give me
John Doe (username) -last login-
I think I managed this, but I also have to check multiple parameters like
./script1.sh John Jane Etrjk
John Doe (username) -last login-
Jane Doe (username) -last login
Etrjk not found
and I do not know how to use multiple parameters in my script this is my script so far (works for 1 parameter):
if [ -z "$1 ]
then
echo "no arguments"
exit
fi
var = $1
if grep -q "$1" /etc/passwd
then
echo "FOUND"
awk 'BEGIN {FS = ":" }; /'$1'/ {print $5,$1}' /etc/passwd
var2=$(awk 'BEGIN {FS = ":" }; /'$1'/ {print $1}' /etc/passwd
lastlog -u $var2 | awk 'NR==2 {print $4,$5,$6}'
else
echo "not found"
fi
CodePudding user response:
You'll do:
for name in "$@"; do
...
done
The shell allows the shorhand for name do
to iterate over the positional parameters. I prefer to be explicit about it.
The safe way to pass parameters into awk is with the -v
option:
username=$(awk -v name="$name" 'BEGIN {FS = ":" }; $0 ~ name {print $1}' /etc/passwd)
I suggest you get into the habit of using meaningful variable names. It will help anyone (including you) who is reading your code.