Manos Manos - 15 days ago 5
Bash Question

Solaris shell script how to use variable inside command

I am trying to create a small shell script in

Solaris
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:
January:

February:

.
.
.

Answer

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}'
Comments