Manos Manos - 4 months ago 26
Bash Question

Solaris shell script how to use variable inside command

I am trying to create a small shell script in

which checks the number of connections per month for the current logged in user, but I am having problem in using a variable inside a command in the right way.

This is my script:

current_user=$(who am i | awk '{print $1}')
echo The logins for user \"$current_user\" were:
echo January:
last | awk '$1=="${current_user}" && $5=="Jan" {count++} END {print count}'
echo February:
last | awk '$1=="${current_user}" && $5=="Feb" {count++} END {print count}'

and it prints:

The logins for user "username" were:




You can pass variables to awk using the -v option, for example:

last | awk -vuser="$current_user" '$1==user && $5=="Jan" {count++} END {print count}'

Alternatively, you could break out of the single quoted string:

last | awk '$1=="'"${current_user}"'" && $5=="Jan" {count++} END {print count}'