Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash if语句中的回车和通配符_Bash_If Statement_Wildcard_Carriage Return - Fatal编程技术网

Bash if语句中的回车和通配符

Bash if语句中的回车和通配符,bash,if-statement,wildcard,carriage-return,Bash,If Statement,Wildcard,Carriage Return,谁能告诉我为什么这不起作用: #!/bin/bash # foo="hello world" if [[ "$foo" == *"\r\r"* ]] then echo "two carriage-returns found" fi 编辑: 弗雷迪和维詹德拉,万分感谢!哎呀,我花了几个小时试着把语法弄对了 您需要检查两个换行符换行\n\n并使用: 原因有二: 您有回车符\r与换行符混淆\n 这些\R实际上是后跟小写R的反斜杠,而不是回车符 所以,你可以用它来让它们变得特别:

谁能告诉我为什么这不起作用:

#!/bin/bash
# 

foo="hello

world"

if [[ "$foo" == *"\r\r"* ]]
then 
  echo "two carriage-returns found"
fi  
编辑:
弗雷迪和维詹德拉,万分感谢!哎呀,我花了几个小时试着把语法弄对了

您需要检查两个换行符换行\n\n并使用:

原因有二:

您有回车符\r与换行符混淆\n 这些\R实际上是后跟小写R的反斜杠,而不是回车符 所以,你可以用它来让它们变得特别:

if [[ "$foo" == *$'\n\n'* ]]
或者加上两行新行,但这很难看:

if [[ "$foo" == *"

"* ]]

你得到了什么?@Sobyso:$foo和模式字符串都不包含回车符。您可以通过执行示例xxd来轻松验证这一点
if [[ "$foo" == *"

"* ]]