Papantonia
Papantonia

Reputation: 111

Shell output to text file

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?

Upvotes: 1

Views: 779

Answers (1)

nabeel
nabeel

Reputation: 319

option 1 inside the script:

#!/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

option 2 inside the script:

#!/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

option 3 in run command:

./hostsScript.sh >> output_file 

Upvotes: 1

Related Questions