Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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
Arrays 强制将Bash数组中的空格传递到grep_Arrays_Bash_Grep_Whitespace - Fatal编程技术网

Arrays 强制将Bash数组中的空格传递到grep

Arrays 强制将Bash数组中的空格传递到grep,arrays,bash,grep,whitespace,Arrays,Bash,Grep,Whitespace,我有一个非常简单的bash脚本,它应该为我想要标记的多个短语grep一个文件 它一直工作到某一点,但当我想grep“print”或“put”(注意单词前后的空格)时,我就崩溃了 grep忽略了输入中的空白 这是我的代码(不相关的东西被删掉了) 我已经考虑过将IFS设置为“~”并以这种方式拆分数组,但这完全破坏了grep命令 脚本的示例输出为 在my_test_file.txt中找到了“print” 任何帮助都将不胜感激。您需要使用“${bad_短语[@]}”。别忘了双引号。你应该养成一种习惯,引

我有一个非常简单的bash脚本,它应该为我想要标记的多个短语grep一个文件

它一直工作到某一点,但当我想grep“print”或“put”(注意单词前后的空格)时,我就崩溃了

grep忽略了输入中的空白

这是我的代码(不相关的东西被删掉了)

我已经考虑过将IFS设置为“~”并以这种方式拆分数组,但这完全破坏了grep命令

脚本的示例输出为

在my_test_file.txt中找到了“print”

任何帮助都将不胜感激。

您需要使用
“${bad_短语[@]}”
。别忘了双引号。你应该养成一种习惯,引用你所有的变量来抑制它


您是否尝试过将
${bad_词组[*]}
更改为
${bad_词组[@]}
?是的-很遗憾,这没有什么区别。
#!/bin/sh

bad_phrases=('console.log(' 'binding.pry' ':focus,' 'alert(' ' print ' ' puts ')
bad_phrase_found=false

FILE='my_test_file.txt'

for bad_phrase in ${bad_phrases[*]} ; do
  if grep -q "$bad_phrase" $FILE ; then
      bad_phrase_found=true
      echo "A '$(tput bold)$(tput setaf 1)$bad_phrase$(tput sgr0)' was found in $(tput bold)$(tput setaf 5)$FILE$(tput sgr0)"
  fi
done

if $bad_phrase_found ; then
  exit 1
fi

exit 0
for bad_phrase in "${bad_phrases[@]}" ; do
  if grep -q "$bad_phrase" "$FILE" ; then
      bad_phrase_found=true
      echo "A '$(tput bold)$(tput setaf 1)$bad_phrase$(tput sgr0)' was found in $(tput bold)$(tput setaf 5)$FILE$(tput sgr0)"
  fi
done