0
lines=`grep -c "" List`
a=1
    while [$a -lt $lines] 
        do
            b=`sed "${a} q;d" List`
            a=$a+1
            echo $b
    done

So, I have this, what is supposed to take a line from a list and echo it. Now, the while loop is not working(?) and returns me this error:HARR.sh: line 9: syntax error: unexpected end of file

What is wrong with it?

Orión González
  • 309
  • 2
  • 14
  • 1
    Spaces after `[` and before `]`: `while [ $a -lt $lines ]` and `a=$a+1` -> `((a++))`. – P.P Mar 07 '17 at 15:53
  • @P.P. `++` works in some shells but it's not [required by the standard](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html), so `a=$((a+1))` is the more portable choice. – Tom Fenech Mar 07 '17 at 15:57
  • It would be useful to see some input + desired output, as running `sed` multiple times on the same file in a loop is probably unnecessary. – Tom Fenech Mar 07 '17 at 16:00
  • 1
    @TomFenech Question is tagged bash. Should be portable in bash at least ;-) – P.P Mar 07 '17 at 16:09
  • 2
    http://www.shellcheck.net/ would tell you: add space after `[` and before `]`; use `$()` instead of legacy `\`\``; double quote to prevent globbing and word splitting, i.e., most things. – Benjamin W. Mar 07 '17 at 16:43
  • Note aside: if `lines` turns out to be a single line, your loop does not execute. Perhaps use `-le`? – linuxfan says Reinstate Monica Mar 07 '17 at 16:47
  • also, why use grep to do wc's job? `lines=$(wc -l – SaintHax Mar 07 '17 at 17:03
  • This is a ridiculously inefficient way to iterate over the lines of a file. `while IFS= read -r b; do ...; done < List` is all you need, assuming there is even a reason to iterate in `bash` rather than using some other program. Is`echo $b` your actual command, or just a placeholder for the real body of the loop? – chepner Mar 07 '17 at 17:48
  • echo $b is yes, a placeholder. There I want to call another program. But I'm not sure of what to do then. I'm so new at all of this, and I need this done ASAP, so I don't know... – Orión González Mar 07 '17 at 21:22

1 Answers1

0

Is this what you want?

#!/bin/bash
#
let a=1
while read line; do
    echo "$a $line"
    let a+=1
done < List
RTLinuxSW
  • 822
  • 5
  • 9