Wednesday, 15 June 2011

Source file (containing list of variables) to be run in bash loop? -



Source file (containing list of variables) to be run in bash loop? -

i've got file containing 2 variables, start , end want invoked in loop of bash script can't seem result want

outputfile2.text start=26 ; end=47 start=48 ; end=69 start=70 ; end=91 start=92 ; end=113 start=114 ; end=135 ...

the loop:

range.bash #!/bin/bash rm range.text -f while read line (( c=$start; c<=$end; c++ )) echo -n "$c "; done >> range.text done < "outputfile2.text"

desired output:

1 2 3 ... 19 20 21 26 27 28 ... 45 46 47 48 49 50 ... 67 68 69 70 71 72 ... 89 90 91 ... etc

how can done?

i'm getting:

./range.bash: line 6: ((: c=: syntax error: operand expected (error token "=")

so i'm assuming it's not reading outputfile2.text has hoped.

end goal: want print grid 15 columns wide each entry beingness 6 characters long, separated space , titled something, have no clue how code that:

line 1 xxxx1 xxxx2 xxxx3 xxxx4 xxxx5 xxxx6 xxxx7 xxxx8 xxxx9 xxx10 xxx11 xxx12 xxx13 xxx14 xxx15 xxx16 xxx17 xxx18 xxx19 xxx20 xxx21 line 2 xxx48 xxx49 xxx50 xxx51 xxx52 xxx53 xxx54 xxx55 xxx56 xxx57 xxx58 xxx59 xxx60 xxx61 xxx62 xxx63 xxx64 xxx65 xxx66 xxx67 xxx68 line 3 ... etc

update (thanks mark setchell):

#!/bin/bash while ifs="=;" read start c end e ((j++)) echo "[ l${j} ]" (( i=$start; i<=$end ; i++ )) printf "%6s" $i done echo ; echo # newline done < outputfile2.text >> range.text

produces:

[ l1 ] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 [ l2 ] 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

almost there!! :)

you can this:

#!/bin/bash cols=15 line=1 while ifs="=;" read start c end e echo line: $line col=1 (( i=$start; i<=$end ; i++ )) printf "%6s" $i if [[ $col -eq 15 ]]; col=1 echo fi (( col++ )) done echo (( line++ )) done < file

the trick set input field separator (ifs) separate variables when = seen or ;.

bash for-loop while-loop

No comments:

Post a Comment