0

Here i have a list (/test/list) contains 3 words: foo, bar, bla . I tried run this code :

#!/bin/bash
list=$(cat /test/list)
for i in $list ;do
if [ "$i" = "foo" ]; then
echo "yes $i"
else 
echo "no $i"
fi
done

But the output is:

no foo
no bar
no bla

How can i do for have:

yes foo
no bar
no bla

I have tried many things but nothing work. Thx for help.

vins mv
  • 46
  • 7
  • I've just run your code on a test file and it works... it outputs as you are expecting. – Stuart Nov 20 '16 at 15:01
  • This is the wrong way to iterate over the contents of a file. See [Bash FAQ 001](http://mywiki.wooledge.org/BashFAQ/001). – chepner Nov 20 '16 at 15:03
  • 1
    It is also possible that `/test/list` contains DOS line endings, in which case the first value is `foo\r`, not `foo`, which would cause the comparison to fail. See http://stackoverflow.com/q/31885409/1126841 – chepner Nov 20 '16 at 15:05
  • @chepner yeah it was a DOS line endings ...thx – vins mv Nov 20 '16 at 15:16

0 Answers0