bash 如果 grep 找到它正在寻找的东西,请执行 X 否则 Y
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13024828/
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
If grep finds what it is looking for do X else Y
提问by Fredrik
In this statement I am trying to match if a version ($var2) exist in the path /app/$var1 (application name)
在此语句中,我试图匹配路径 /app/$var1(应用程序名称)中是否存在版本($var2)
if
find /app/$var1 -maxdepth 1 -type l -o -type d | grep $var2 #results in a nice list where i can manually get a true match.
# if match is found then execute command.
$realcmd "$@"
rc=$?
exit $rc
else
echo "no match, the version you are looking for does not exist"
fi
current code: this include all my code (not cleaned). command I run: "./xmodule load firefox/3.6.12" this version does exit
当前代码:这包括我所有的代码(未清理)。我运行的命令:“./xmodule load firefox/3.6.12”这个版本确实退出
#!/bin/bash
# hook for some commands
#echo $@ #value of sting that is entered after "xmodule"
cmd=$(basename "if find "/app/$var1" -maxdepth 1 -type l -o -type d | grep -q "$var2"; then
$realcmd "$@"
exit $?
else
echo "no match, the version you are looking for does not exist"
# Should there be an exit here?
fi
")
#echo "called as $cmd"
if [[ $cmd = "xmodule" ]]
then
realcmd='/app/modules/0/bin/modulecmd tcsh'
# verify parameters
fi
# check if $@ contains value "/" to determine if a specific version is requested.
case "$@" in
*/*)
echo "has slash"
var1=$(echo "$@" | grep -Eio '\s\w*') # Gets the aplication name and put it into var1
echo $var1 # is not printed should be "firefox"
var2=$(echo "$@" | grep -o '[^/]*$') # Gets version name and put it into var2
echo $var2
# Checking if version number exist in /app/appname/
if find /app/$var1 -noleaf -maxdepth 1 -type l -o -type d | grep $var2; then
$realcmd "$@"
exit $?
else
echo "no match, the version you are looking for does not exist"
# Should there be an exit here?
fi
;;
*)
echo "doesn't have a slash"
;;
esac
output: mycomputer [9:55am] [user/Desktop/script] -> ./xmodule load firefox/3.6.12 'has slash
输出:mycomputer [9:55am] [user/Desktop/script] -> ./xmodule load firefox/3.6.12 '有斜线
3.6.12 no match, the version you are looking for does not exist
3.6.12 不匹配,您要找的版本不存在
Where there is a blank (above 3.6.1) there should be the application name. I am now realizing that this must be my problem sins the path that it uses i likely just /app. But I do not think I changed anything in that part of the code.
有空格的地方(3.6.1 以上)应该是应用程序名称。我现在意识到这一定是我的问题,因为它使用的路径可能只是 /app。但我不认为我对代码的那部分进行了任何更改。
回答by Gordon Davisson
You can use the entire grep pipeline as the condition of the if
statement. Use grep -q
to keep it from printing the match it finds (unless you want that printed). I also simplified the exit (there's no need to store $?
in a variable if you're just going to use it immediately). Here's the result:
您可以使用整个 grep 管道作为if
语句的条件。使用grep -q
以防止它打印找到的匹配(除非你想要的打印)。我还简化了出口($?
如果您只是要立即使用它,则无需将其存储在变量中)。结果如下:
find /app/$var1 -maxdepth 1 -type l -o -type d | grep $var2
greprc=$?
if [[ $greprc -eq 0 ]] ; then
echo Found
else
if [[ $greprc -eq 1 ]] ; then
echo Not found
else
echo Some sort of error
fi
fi
BTW, since you're going to exit immediately after $realcmd, you could use exec $realcmd "$@"
to replace the shell with $realcmd instead of running $realcmd as a subprocess.
顺便说一句,由于您将在 $realcmd 之后立即退出,您可以使用exec $realcmd "$@"
$realcmd 替换 shell,而不是将 $realcmd 作为子进程运行。
回答by paxdiablo
From the grep
manpage:
从grep
联机帮助页:
The exit status is 0 if selected lines are found, and 1 if not found. If an error occurred the exit status is 2.
如果找到选定的行,退出状态为 0,如果未找到,则退出状态为 1。如果发生错误,退出状态为 2。
In other words, immediately following your blah blah | grep $var2
, simply check the return value.
换句话说,紧跟在您的 之后blah blah | grep $var2
,只需检查返回值。
Since the exit code for a pipeline is the exit code for the last process in that pipeline, you can use something like:
由于管道的退出代码是该管道中最后一个进程的退出代码,您可以使用以下内容:
##代码##