Shell output to text file

时间:2015-10-30 22:02:19

标签: bash shell

So, I have the following shell:

#!/bin/bash
for (( i = 1; i <= 6; i++ )) 
do
    printf in_port=$i,actions=
    for (( j = 1 ; j <= 6; j++ ))
    do
        if [ $i != $j ]; then   
                printf output:$j,
        fi
    done
    printf "\n"
done

Which, produces the following output:

home@mininet:~$ ./hostsScript.sh
in_port=1,actions=output:2,output:3,output:4,output:5,output:6,
in_port=2,actions=output:1,output:3,output:4,output:5,output:6,
in_port=3,actions=output:1,output:2,output:4,output:5,output:6,
in_port=4,actions=output:1,output:2,output:3,output:5,output:6,
in_port=5,actions=output:1,output:2,output:3,output:4,output:6,
in_port=6,actions=output:1,output:2,output:3,output:4,output:5,

How would I go about appending each line of this output to a txt file, line-by-line?

1 个答案:

答案 0 :(得分:1)

脚本中的

选项1:

#!/bin/bash
for (( i = 1; i <= 6; i++ )) 
do
    printf in_port=$i,actions=
    for (( j = 1 ; j <= 6; j++ ))
    do
        if [ $i != $j ]; then   
                printf output:$j,
        fi
    done
    printf "\n"
done >> output_file
脚本中的

选项2:

#!/bin/bash

exec >> output_file

for (( i = 1; i <= 6; i++ )) 
do
    printf in_port=$i,actions=
    for (( j = 1 ; j <= 6; j++ ))
    do
        if [ $i != $j ]; then   
                printf output:$j,
        fi
    done
    printf "\n"
done
运行命令中的

选项3:

./hostsScript.sh >> output_file