grep怎样才能解释一个字符串,它包含一个星号,并通过一个变量传递给grep? [英] How can grep interpret literally a string that contains an asterisk and is fed to grep through a variable?

查看:281
本文介绍了grep怎样才能解释一个字符串,它包含一个星号,并通过一个变量传递给grep?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #!/ bin / bash 
CLASSPATH =/ blah / libs / *:/ blah / more / libs
CMD =java -cp $ CLASSPATH MainClass
ALREADY_RUNNING_PID =`ps -ef --no-headers | grep $ CMD | grep -v grep | awk'{print $ 2}'`
if [$ ALREADY_RUNNING_PID];然后
回声已经运行
出口1
fi
$ CMD&

问题是由于CMD变量中的星号而无法正常工作。我该如何告诉grep才能看到变量值?任何解决方案通过变量传递grep是强制性的。
Thanks。

解决方案

既然你没有使用正则表达式,你可以使用 fgrep $ CMD 而不是 grep


I have this script:

#!/bin/bash
CLASSPATH="/blah/libs/*:/blah/more/libs"
CMD="java -cp $CLASSPATH MainClass"
ALREADY_RUNNING_PID=`ps -ef --no-headers | grep $CMD | grep -v grep | awk '{print $2}'`
if [ "$ALREADY_RUNNING_PID" ]; then
      echo "Already running"
      exit 1
fi
$CMD &

problem is it doesnt work due to the asterisk in the CMD variable. how can i tell grep to see the variable value as it is? Any solution? It is mandatory that grep is fed through the variable. Thanks.

解决方案

Since you are not using regular expressions you can use fgrep $CMD instead of grep

这篇关于grep怎样才能解释一个字符串,它包含一个星号,并通过一个变量传递给grep?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆