Shell脚本里面awk来显示/ etc / passwd文件信息 [英] Awk inside Shell Script to display /etc/passwd info

查看:328
本文介绍了Shell脚本里面awk来显示/ etc / passwd文件信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去写我的shell脚本一个awk线,将属于被输入的用户在passwd文件的信息搜索。由于AWK只在特定的列,事实上,第一列将取决于谁的用户名被用于搜索,我想我已经用AWK线内的脚本输入$ USERNAME。

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.

到目前为止,在脚本行是:

的awk -f awkpass.awk / etc / passwd文件

而在awkpass.awk文件中的行是:

/人/ {打印用户名\\ t \\ t \\ t的$ 1

我觉得我需要插入$的用户名,而不是人,因为$的用户名是我曾经收到来自用户的输入,但不是很确定的变量。帮助是非常AP preciated。谢谢!

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!

推荐答案

您也许可以使用以下两种:

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}"

,其中前 $ 1 反斜线保护 $ 从外壳和双引号前的反斜杠保护他们从shell。有些时候,这是值得使用 -f awk.script ;目前尚不清楚,这是其中之一。

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.

== 标记的是,〜<之间的区别/ code>变种匹配使用正则表达式。使用 == 的版本也适用于简单的平等,当然。使用匹配意味着你可以有用户名='^(根|守护程序|斌| SYS)$ 键,你会得到一个单一的四个指定用户的条目脚本调用。缺点是,如果你仅指定,你可能会得到多个条目(例如,在我的Mac,我得到两个 _cmvsroot 列出)。

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).

这些脚本使用双引号,这样的用户名可以嵌入到命令行脚本。然而,埃德·莫顿作为正确地指出了他的<一个 href=\"http://stackoverflow.com/questions/23587928/awk-inside-shell-script-to-display-etc-passwd-info/23588059?noredirect=1#comment36214051_23588059\">comment,这导致在脚本反斜杠的过量。我一般使用脚本左右单引号(尽管这个答案的第一个版本不这样做)。

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

这些报价序列是微妙的;最好是避开他们。您可以通过使用 -v VARNAME =值符号在命令行上指定变量的初始值做到这一点。因此,你也可以使用:

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

此设置 AWK 变量用户名从shell变量 $用户名。这种变化可以用一个文件中使用

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

awk.script 包含:

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

这篇关于Shell脚本里面awk来显示/ etc / passwd文件信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆