可以在shell脚本中使用管道或者重定向输出结果。可以通过在done命令的末尾添加处理命令来实现
for file in /home/rich/*
do
if [ -d "$file" ]
then
echo "$file is a directory"
elif
echo "$file is a file"
fi
done > output.txt
这样,就把for命令的结果重定向到文件output.txt中。
for((i=1;i<=10;i++))
do
echo "This is the $[ 10-$i ] loop"
done | sort
echo "complete"
输出:
This is the 0 loop
This is the 1 loop
This is the 2 loop
This is the 3 loop
This is the 4 loop
This is the 5 loop
This is the 6 loop
This is the 7 loop
This is the 8 loop
This is the 9 loop
complete