如果grep的发现它是什么期待脱颖而出做X别的ÿ [英] IF grep finds what it is looking fore do X else Y
问题描述
在此声明我想如果匹配一个版本($ VAR2)在路径/应用/ $ VAR1(应用程序名)存在
In this statement I am trying to match if a version ($var2) exist in the path /app/$var1 (application name)
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
目前的code:
这包括所有我的code(不清洗)。
命令我跑:./xmodule负荷的Firefox / 3.6.12
这个版本确实退出
current code: this include all my code (not cleaned). command I run: "./xmodule load firefox/3.6.12" this version does exit
#!/bin/bash
# hook for some commands
#echo $@ #value of sting that is entered after "xmodule"
cmd=$(basename "$0")
#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
输出:
MYCOMPUTER [上午9:55] [用户/桌面/脚本] - > ./xmodule加载的firefox / 3.6.12
有斜线
output: mycomputer [9:55am] [user/Desktop/script] -> ./xmodule load firefox/3.6.12 'has slash
3.6.12
不匹配,您正在寻找的版本不存在
3.6.12 no match, the version you are looking for does not exist
哪里有空白(3.6.1以上)应该有应用程序名称。我现在意识到这一定是我的问题的罪,它使用我可能只是/应用程序的路径。
但我不认为我在code的那部分改变任何东西。
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.
推荐答案
您可以使用整个grep的管道仿佛语句的的状态。使用
的grep -q
来保持它打印找到匹配(除非你想要的打印)。我还简化了出口(有没有必要 $?
来存储在一个变量,如果你只是要立即使用它)。这里的结果是:
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:
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
顺便说一句,因为你要$ realcmd后立即退出,你可以使用 EXEC $ realcmd$ @
来替换$外壳realcmd而不是运行$ realcmd作为子进程。
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.
这篇关于如果grep的发现它是什么期待脱颖而出做X别的ÿ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!