在 bash 或 shell 中管道映射到哪个参数?
Which parameter is pipe mapped to in bash or shell?
我有一个脚本可以格式化一些难以阅读的日志文件的输出,使它们易于阅读。我按如下方式调用我的脚本
me@myHost $ cat superBigLogFile$date | grep "Stuff from log file I want to see" | /scripts/logFileFormatter
在脚本中,它使用 $0、$1 和 $2,但我不知道 cat'ed 文本映射到哪个参数。我想对脚本进行更改,只需要输入日期和我想看到的内容。如:
me@myHost $/scripts/logFileFormatter 2016-02-10 "Stuff I want to see"
以下是脚本的详细信息。技术细节是此脚本将 NDM 日志的输出格式化为人类可读的形式。
PATH=/usr/xpg4/bin:/usr/bin
# add SUMM field and end of record marker on stat lines
awk '{print [=14=]"|SUMM=N|EOR"}' |\
# format the STAT file, putting each field on a separate line
tr '|' '2' |\
# separate times from dates and reformat source and destination file fields
# to have a space after the =
awk -F= '{
if (=="DFIL" || =="SFIL") print "= "
else if (=="STAR" || =="SSTA" || =="STOP" ) {
split(,A," ")
print "=" A[1] "=" A[2]
}
else print
}' |\
# execute the ndmstat.awk that comes with Connect:Direct
awk -F= -f /cdndm/cmddbp1/cdunix/ndm/bin/ndmstat.awk |\
# additional formatting to remove the greater than sign arrows
sed 's/=>/=/g'
管道 - |
- 接受一个命令的标准输出,"wires it" 到另一个命令的标准输入。
一个简单的脚本(假设它被称为 script.sh
):
while read line
do
echo "line" $line
done
可以这样工作:
$ ls -al | ./script.sh
line total 15752
line drwxr-xr-x+ 106 kls staff 3604 Feb 10 23:13 .
line drwxr-xr-x 6 root admin 204 May 23 2015 ..
line -rwxr-xr-x 1 kls staff 56 Feb 10 23:13 a.sh
这里的关键部分是 read
命令,它从标准输入读取并将结果逐行放入 line
变量中。这样,每一行都会在一个循环中打印(在上面的示例中,它还带有 "line" 前缀,以区别于常规 ls -al
输出)。
现在,我没有 运行 你的脚本的测试数据,但它与 awk 非常相似。考虑这个脚本(保存到 script.sh
):
awk '{print }'
可以这样调用:
$ ls -al | ./script.sh
total
drwxr-xr-x+
drwxr-xr-x
-rwxr-xr-x
这表明 awk 确实在做它的工作 - 它会获取并打印 ls -al
生成的每行输出的第一个标记 (</code>)(通过标准输入传递 - <code>|
).
关于 Bash 和 Awk 中 </code> 的注释</strong></p>
<p>重要说明:<code>
这里不是 Bash 变量 - 它是在 awk 中定义的变量。它并不像 Bash 中那样表示 "first argument to the script",而是 "first token in the input"。这两者是完全独立的——这展示了如何同时使用它们:
script.sh
:
awk "{print \" \" $1}"
^ ^
| |
Bash Awk
输出:
$ ls -al | ./script.sh PREFIX <-- We pass PREFIX now that
will be bound to Bash variable.
PREFIX total
PREFIX drwxr-xr-x+
PREFIX drwxr-xr-x
PREFIX -rwxr-xr-x
一开始可能有点奇怪,所以我在代码中添加了一些注释。仔细检查双引号,以及它们是如何用 \
符号转义的。相似地。 Awk </code> 也被转义 (<code>$1
) 而 Bash' 则不是。
我有一个脚本可以格式化一些难以阅读的日志文件的输出,使它们易于阅读。我按如下方式调用我的脚本
me@myHost $ cat superBigLogFile$date | grep "Stuff from log file I want to see" | /scripts/logFileFormatter
在脚本中,它使用 $0、$1 和 $2,但我不知道 cat'ed 文本映射到哪个参数。我想对脚本进行更改,只需要输入日期和我想看到的内容。如:
me@myHost $/scripts/logFileFormatter 2016-02-10 "Stuff I want to see"
以下是脚本的详细信息。技术细节是此脚本将 NDM 日志的输出格式化为人类可读的形式。
PATH=/usr/xpg4/bin:/usr/bin
# add SUMM field and end of record marker on stat lines
awk '{print [=14=]"|SUMM=N|EOR"}' |\
# format the STAT file, putting each field on a separate line
tr '|' '2' |\
# separate times from dates and reformat source and destination file fields
# to have a space after the =
awk -F= '{
if (=="DFIL" || =="SFIL") print "= "
else if (=="STAR" || =="SSTA" || =="STOP" ) {
split(,A," ")
print "=" A[1] "=" A[2]
}
else print
}' |\
# execute the ndmstat.awk that comes with Connect:Direct
awk -F= -f /cdndm/cmddbp1/cdunix/ndm/bin/ndmstat.awk |\
# additional formatting to remove the greater than sign arrows
sed 's/=>/=/g'
管道 - |
- 接受一个命令的标准输出,"wires it" 到另一个命令的标准输入。
一个简单的脚本(假设它被称为 script.sh
):
while read line
do
echo "line" $line
done
可以这样工作:
$ ls -al | ./script.sh
line total 15752
line drwxr-xr-x+ 106 kls staff 3604 Feb 10 23:13 .
line drwxr-xr-x 6 root admin 204 May 23 2015 ..
line -rwxr-xr-x 1 kls staff 56 Feb 10 23:13 a.sh
这里的关键部分是 read
命令,它从标准输入读取并将结果逐行放入 line
变量中。这样,每一行都会在一个循环中打印(在上面的示例中,它还带有 "line" 前缀,以区别于常规 ls -al
输出)。
现在,我没有 运行 你的脚本的测试数据,但它与 awk 非常相似。考虑这个脚本(保存到 script.sh
):
awk '{print }'
可以这样调用:
$ ls -al | ./script.sh
total
drwxr-xr-x+
drwxr-xr-x
-rwxr-xr-x
这表明 awk 确实在做它的工作 - 它会获取并打印 ls -al
生成的每行输出的第一个标记 (</code>)(通过标准输入传递 - <code>|
).
关于 Bash 和 Awk 中 </code> 的注释</strong></p>
<p>重要说明:<code>
这里不是 Bash 变量 - 它是在 awk 中定义的变量。它并不像 Bash 中那样表示 "first argument to the script",而是 "first token in the input"。这两者是完全独立的——这展示了如何同时使用它们:
script.sh
:
awk "{print \" \" $1}"
^ ^
| |
Bash Awk
输出:
$ ls -al | ./script.sh PREFIX <-- We pass PREFIX now that
will be bound to Bash variable.
PREFIX total
PREFIX drwxr-xr-x+
PREFIX drwxr-xr-x
PREFIX -rwxr-xr-x
一开始可能有点奇怪,所以我在代码中添加了一些注释。仔细检查双引号,以及它们是如何用 \
符号转义的。相似地。 Awk </code> 也被转义 (<code>$1
) 而 Bash' 则不是。