1

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?

5
  • 4
    command > output_file Commented Oct 30, 2015 at 22:03
  • Or command >> output_file for the next lines (1 > overwrites the file, >> appends to the file). Commented Oct 30, 2015 at 22:06
  • Or ( command1; command2 ) > output_file. Commented Oct 30, 2015 at 22:06
  • { command1; command2; } > output_file would be better yet. Commented Oct 30, 2015 at 22:07
  • Actually, neither parentheses are needed after the loop: for ... do; done > output_file :-) Commented Oct 30, 2015 at 22:35

1 Answer 1

1

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 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.