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:
.
.
.
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}'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.