简体   繁体   English

awk输出到字符串或大小写命令错误

[英]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. 我需要在bash脚本中读取一个字符串,然后根据字符串值继续。 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). 字符串值是正确的,因为echo命令显示正确的值,flag.txt只包含“S”或“N”(在这种情况下为N)。 Absolutely stuck here, can't figure out what's wrong, awk or case. 绝对卡在这里,无法弄清楚什么是错的,awk或者是case。 Working in Ubuntu 12.04 在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. 似乎文件flag.txt具有CRLF行结尾。 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. 从输入文件中删除\\r ,脚本将按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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