猫文件名|grep“某物"结果“猫:grep:没有这样的文件或目录";在shell脚本中 [英] cat file_name | grep "something" results "cat: grep: No such file or directory" in shell scripting
问题描述
我编写了shell脚本,该脚本从输入文件读取命令并执行命令.我有这样的命令:
I have written shell script which reads commands from input file and execute commands. I have command like:
cat linux_unit_test_commands | grep "dmesg"
输入文件中的
.执行shell脚本时出现以下错误消息:
in the input file. I am getting below error message while executing shell script:
cat: |: No such file or directory
cat: grep: No such file or directory
cat: "dmesg": No such file or directory
脚本:
#!/bin/bash
while read line
do
output=`$line`
echo $output >> logs
done < $1
下面是输入文件(example_commands):
Below is input file(example_commands):
ls
date
cat linux_unit_test_commands | grep "dmesg"
执行:./linux_unit_tests.sh example_commands
Execute: ./linux_unit_tests.sh example_commands
请帮助我解决此问题.
推荐答案
在扩展变量后,不会解析特殊字符,例如 |
和"
扩展是单词拆分和通配符扩展.如果要完全解析该行,则需要使用 eval
:
Special characters like |
and "
are not parsed after expanding variables; the only processing done after variable expansion is word splitting and wildcard expansions. If you want the line to be parsed fully, you need to use eval
:
while read line
do
output=`eval "$line"`
echo "$output" >> logs
done < $1
这篇关于猫文件名|grep“某物"结果“猫:grep:没有这样的文件或目录";在shell脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!