简体   繁体   中英

Awk output to string or case command error

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM