Solaris shell 脚本如何在命令中使用变量

Solaris shell script how to use variable inside command

我正在尝试在 Solaris 中创建一个小的 shell 脚本 来检查当前登录用户每月的连接数,但是我在 以正确的方式在命令 中使用变量时遇到问题。

这是我的脚本:

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

并打印:

The logins for user "username" were:
January:

February:

.
.
.

您可以使用 -v 选项将变量传递给 awk,例如:

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

或者,您可以跳出单引号字符串:

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