Awk inside Shell Script to display /etc/passwd info [closed]

情到浓时终转凉″ 提交于 2019-12-12 02:07:14

问题


Im trying to write an awk line in my shell script that will search for the passwd files information belonging to the user that was inputted. Since awk only searches in specific columns and the fact that the first column will vary based on whoever username is used, i think i have to use the script input $username inside the awk line.

So far the line in the script is:

awk -f awkpass.awk /etc/passwd

And the line in the awkpass.awk file is:

/anyone/{print "Username\t\t\t" $1

I think i need to insert $username instead of "anyone" since $username is the variable that i used to recieve input from the user but not quite sure. Help is very appreciated. Thanks!


回答1:


You can probably use either of these:

awk -F: "\$1 ~   /$username/  { print \"Username:\t\t\t\", \$1}"
awk -F: "\$1 == \"$username\" { print \"Username:\t\t\t\", \$1}"

where the backslash before $1 protects the $ from the shell, and the backslash before the double quotes protects them from the shell. There are times when it is worth using -f awk.script; it is not clear that this is one of them.

The difference between the ~ and the == notation is that the ~ variant matches using a regex. The version using the == goes for simple equality, of course. Using a match means you could have username='^(root|daemon|bin|sys)$' and you'd get the entries for the four named users in a single invocation of the script. The downside is that if you specify just root, you might get multiple entries (for example, on my Mac, I get both root and _cmvsroot listed).

These scripts use double quotes so that the username can be embedded into the script from the command line. However, as correctly pointed out by Ed Morton in his comment, this leads to a surfeit of backslashes in the script. I generally use single quotes around scripts (despite not doing so in the first edition of this answer).

awk -F: '$1 ~  /'"$username"'/ { print "Username:\t\t\t", $1}'
awk -F: '$1 == "'"$username"'" { print "Username:\t\t\t", $1}'

Those quote sequences are subtle; it is better to avoid them. You can do that by specifying initial values for variables on the command line with -v varname="value" notation. Therefore, you could also use:

awk -F: -v username="$username" \
    "\$1 == username { print \"Username:\t\t\t\", \$1}" /etc/passwd

or, better, since it uses single quotes around the script:

awk -F: -v username="$username" \
    '$1 == username { print "Username:\t\t\t", $1}' /etc/passwd

This sets the awk variable username from the shell variable $username. This variation can be used with a file:

awk -F: -v username="$username" -f awk.script /etc/passwd

with awk.script containing:

$1 == username { print "Username\t\t\t", $1 }



回答2:


Alternatively, you can use getent:

printf "Username: \t\t\t%s\n" "$(getent passwd "$username" | cut -d: -f1)"


来源:https://stackoverflow.com/questions/23587928/awk-inside-shell-script-to-display-etc-passwd-info

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!