不需要shell循环:
gzip -cd $(<list.txt) | ./a.out
使用“-cd”选项,gzip 会将文件列表解压缩到标准输出(或者您可以使用“gunzip -c”)。 $(<file) 表示法将命名文件的内容扩展为参数列表,而不启动子进程。否则相当于$(cat list.txt)。
但是,如果您觉得必须使用循环,那么只需将循环的输出通过管道传输到程序的单个实例中:
for i in `cat list.txt`
do
gunzip -c $i
done |
./a.out
如果循环的内容更复杂(而不是简单地压缩单个文件),这可能是必要的。你也可以使用'{ ... }'I/O重定向:
{
cat /etc/passwd /etc/group
for i in `cat list.txt`
do
gunzip -c $i
done
} |
./a.out
或者:
{
cat /etc/passwd /etc/group
for i in `cat list.txt`
do
gunzip -c $i
done; } |
./a.out
注意分号;大括号是必要的。在这个例子中,它本质上与使用带括号的正式子 shell 相同:
(
cat /etc/passwd /etc/group
for i in `cat list.txt`
do
gunzip -c $i
done
) |
./a.out
或者:
( cat /etc/passwd /etc/group
for i in `cat list.txt`
do
gunzip -c $i
done) |
./a.out
注意这里没有分号;不需要。外壳有时非常狡猾。当您需要在管道符号之后对命令进行分组时,大括号 I/O 重定向会很有用:
some_command arg1 arg2 |
{
first sub-command
second command
for i in $some_list
do
...something with $i...
done
} >$outfile 2>$errfile