简体   繁体   English

bash脚本中的grep无法按预期工作

[英]grep in bash script not working as expected

If I run 如果我跑步

grep -i "echo" *

I get the results I want, but if I try the following simple bash script 我得到了想要的结果,但是如果尝试以下简单的bash脚本

#search.sh
grep -i "$1" *
echo "####--DONE--####"

and I run it with sh -x search.sh "echo" I get the following error output: 然后用sh -x search.sh "echo"运行它,得到以下错误输出:

' grep -i echo '*
: No such file or directory
' echo '####--DONE--####
####--DONE--####

How come? 怎么会? I'm on CentOS 我在使用CentOS

Add the sha-bang line at the top of your script 在脚本顶部添加“ shabang”

#!/bin/bash

and after making it executable, run the script using 并使其可执行后,使用

./search.sh "echo"

The "sh -x" should print the files that '*' matches. “ sh -x”应打印与“ *”匹配的文件。 It looks like it's not matching any files. 似乎与任何文件都不匹配。 Are you maybe running it in a directory with no readable files? 您是否可能在没有可读文件的目录中运行它?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM