0

I'm stuck in a surely simple question (absolutely sure I'll feel a dummy when a solution comes up). I need to read a string in a bash script and proceed depending on the string value. This piece of code it is not working by now

k=`awk '{ print $1 }' flag.txt`

echo "FLAG = "$k

case "$k" in
     "S") echo " Yes it worked - Flag = " $k ;;
     "N") echo " NOOOOOOOOOOOO - Flag = " $k ;;
     *) echo "Not getting the right string??" ;;
esac

The string value is right as echo command shows the right value, flag.txt just contains "S" or "N" (N in this case). Absolutely stuck here, can't figure out what's wrong, awk or case. Working in Ubuntu 12.04

Script output:

paco@NIMBUS:~/temp$ cat flag.txt 
S
paco@NIMBUS:~/temp$ ./prova2
FLAG = S
Not getting the right string??

Thanks in advance for your help and sorry if it is a silly question.

pacomet
  • 5,011
  • 12
  • 59
  • 111

1 Answers1

2

It appears that the file flag.txt has CRLF line endings. Execute the following and you'd be certain:

$ awk '{ print $1 }' flag.txt | od

This would in all likelihood return:

0000000 006523 000012
0000003

whereas it should have returned:

0000000 005123
0000002

Get rid of \r from the input file and the script would work as expected.

devnull
  • 118,548
  • 33
  • 236
  • 227
  • It is just what you said. How do I get rid of \r? flag.txt was generated by `echo N > flag.txt` Then is sent to a server with `wput -u flag.txt` and retreived with wget in another machine that runs the script. – pacomet Jul 23 '13 at 14:46
  • @pacomet there are various ways; see http://stackoverflow.com/questions/800030/remove-carriage-return-in-unix – devnull Jul 23 '13 at 14:47
  • Thanks @devnull it works fine now, not first time meeting this type of problem, should remember for the next time. – pacomet Jul 23 '13 at 14:53