可以在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