2

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:

.
.
.

1 Answer 1

2

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}'
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! The second option worked, but I am trying to find out why the first one didn't.
@Manos Which Solaris version of awk are you using? There are multiple different versions of awk on a normal Solaris install. The default one is pretty old.
@Andrew Henle It has an oawk version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.