shell - 可以在命令行上运行,但不能在 shell 脚本中运行?
问题描述
我想用它grep
来搜索文件中匹配的字符串,但是因为文件太大,我只搜索前500行。
我在shell脚本中写道:
#!/bin/bash
patterns=(
llc_prefetcher_operat
to_prefetch
llc_prefetcher_cache_fill
)
search_file_path="mix1-bimodal-no-bop-lru-4core.txt"
echo ${#patterns[*]}
cmd="head -500 ${search_file_path} | grep -a "
for(( i=0;i<${#patterns[@]};i++)) do
cmd=$cmd" -e "\"${patterns[i]}\"
done;
echo $cmd
$cmd >junk.log
运行脚本的结果是:
3
head -500 mix1-bimodal-no-bop-lru-4core.txt | grep -a -e "llc_prefetcher_operat" -e "to_prefetch" -e "llc_prefetcher_cache_fill"
head: invalid option -a
Try'head --help' for more information.
在倒数第二行,我打印出执行命令的字符串。我直接在命令行上运行,成功了。也就是下面这句话。
head -500 mix1-bimodal-no-bop-lru-4core.txt | grep -a -e "llc_prefetcher_operat" -e "to_prefetch" -e "llc_prefetcher_cache_fill"
注意,在grep
命令中,如果我不添加-a
选项,会出现matching the binary file
.
为什么会出现这个问题?谢谢!
解决方案
与其尝试构建一个包含复杂命令的字符串,不如使用grep
'-f
选项和bash
进程替换来传递要搜索的模式列表:
head -500 "$search_file_path" | grep -Faf <(printf "%s\n" "${patterns[@]}") > junk.log
它更短、更简单、更不容易出错。
(我添加-F
到grep
选项中是因为您的示例模式都没有任何正则表达式元字符;因此固定字符串搜索可能会更快)
您正在做的最大问题是|
被视为head
何时$cmd
分词的另一个论点。它不像存在文字时那样被视为管道分隔符。
推荐阅读
- ssh - 如何通过浏览器实现 SSH X11 转发
- powershell - 为什么从 Windows 批处理文件中运行 PowerShell 脚本时会输出不需要的空行?
- java - 接口作为参数,返回结果
- kotlin - 如何在 Kotlin 的片段中添加 SQLite DB 类的方法?
- java - Spring Boot:我们可以为每个请求设置单独的线程池吗?
- javascript - JQuery 在连续计算多个输入时返回 NaN 值
- wordpress - 如何创建自定义部分,如帖子、媒体?
- javascript - React on rails 显示空白网页,报告已更正的错误
- c# - 如何获取水晶报告以在客户端计算机上加载报告
- postgresql - Blazor + Identity:为什么会出现错误?