使用shell脚本进行I / O重定向

时间:2014-08-03 10:21:42

标签: shell

我正在编写一个脚本脚本来打印可执行文件输出的第7列

   get_events -r > f
   awk '{print $7}' f > k
   while read h
   do
           fsstat $h
   done <k

我需要直接执行命令fsstat,它接受来自使用get_events派生的o / p的i / p。 如何在不涉及上述文件(f和h)的存储的情况下执行命令fsstat

2 个答案:

答案 0 :(得分:2)

get_events -r | awk '{print $7}' | while read h; do
    fsstat "$h"
done

如果您使用bash,则为另一个:

while read h; do
    fsstat "$h"
done < <(get_events -r | awk '{print $7}')

这会阻止fsstat吃掉输入:

while read -u 4 h; do
    fsstat "$h"
done 4< <(get_events -r | awk '{print $7}')

更新

这是另一种便携式方式,您无需使用awk

get_events -r | while read _ _ _ _ _ _ h _; do
    fsstat "$h"
done

答案 1 :(得分:2)

您可以将此脚本合并为一个:

while IFS=' ' read -ra arr; do
   fsstat "${arr[6]}"
done < <(get_events -r)
  1. 此脚本使用process substitution < <(get_events -r)来阅读。{ 输出命令get_events -r
  2. 使用array
  3. 将命令输出的每一行读入数组read -a
  4. 然后,从读取数组开始,我们使用${arr[6]},它是第7个索引,因为它以0开头。
相关问题