Linux 如何在脚本中执行 grep?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8154637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 21:49:24  来源:igfitidea点击:

How Can i Execute grep in a Script?

linuxbashshellscripting

提问by JIZZ Jid

When i do the following i get results:

当我执行以下操作时,我会得到结果:

bash$ cat launched | egrep MyTest
MyTest

but with the following script:

但使用以下脚本:

#!/bin/sh
result= `cat launched | grep MyTest`
echo $result

when launching the script i get:

启动脚本时,我得到:

bash$ ./test.sh
./test.sh: MyTest: not found

I have full access rights on the script and the script is launched in the same directory as the file launched. How can i fix the script so it will return the same result as above?

我对脚本拥有完全访问权限,并且脚本在与启动文件相同的目录中启动。如何修复脚本,使其返回与上述相同的结果?

采纳答案by Fred Foo

Drop the space:

删除空间:

result=`cat launched | grep MyTest`

Even better, drop the UUOC:

更好的是,删除UUOC

result=`grep MyTest launched`

回答by Marc B

You've got an extra space:

你有一个额外的空间:

result= `cat launched | grep MyTest`
       ^--- 

variable assignments must not have spaces on either side of the =.

变量赋值不能在=.

回答by KevinDTimm

Remove the space before the grave accent and your problems go away

在重音之前删除空格,你的问题就会消失