Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.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
Linux cat文件“U名称”grep“;“什么?”;“结果”;cat:grep:没有这样的文件或目录;壳内脚本_Linux_Bash_Shell - Fatal编程技术网

Linux cat文件“U名称”grep“;“什么?”;“结果”;cat:grep:没有这样的文件或目录;壳内脚本

Linux cat文件“U名称”grep“;“什么?”;“结果”;cat:grep:没有这样的文件或目录;壳内脚本,linux,bash,shell,Linux,Bash,Shell,我已经编写了shell脚本,它从输入文件读取命令并执行命令。我有如下命令: cat linux_unit_test_commands | grep "dmesg" 在输入文件中。我在执行shell脚本时收到以下错误消息: cat: |: No such file or directory cat: grep: No such file or directory cat: "dmesg": No such file or directory 脚本: #!/bin/bash while

我已经编写了shell脚本,它从输入文件读取命令并执行命令。我有如下命令:

cat linux_unit_test_commands | grep "dmesg" 
在输入文件中。我在执行shell脚本时收到以下错误消息:

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
执行:./linux\u unit\u tests.sh示例\u命令


请帮助我解决此问题。

特殊字符,如
|
在展开变量后不进行分析;变量展开后唯一进行的处理是分词和通配符展开。如果您希望完全解析该行,则需要使用
eval

while read line
do
    output=`eval "$line"`
    echo "$output" >> logs
done < $1
读取行时
做
输出=`eval“$line”`
回显“$output”>>日志
已完成<$1

您可能想知道为什么它不使用cat命令。 那么这就是你问题的答案

output=`$line` i.e. output=`cat linux_unit_test_commands | grep "dmesg"`
这里cat命令将(linux_unit_test_commands | grep“dmesg”)所有这些作为参数,即文件名

从手册页:

语法:cat[选项]…[文件]

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

脚本正常!

#!/bin/bash

while read line;
do
    output=`$line`
    echo $output >> logs
done < $1
无论如何,请使用
grep“dmesg”linux\u unit\u test\u命令
。请参见此处:
#!/bin/bash

while read line;
do
    output=`$line`
    echo $output >> logs
done < $1
cat linux_unit_test_commands

ls
date
grep "dmesg" linux_unit_test_commands