bash – 如果grep找到它正在看的东西,X else Y
发布时间:2020-12-15 18:50:42 所属栏目:安全 来源:网络整理
导读:在这个声明中,如果路径/ 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 exec
在这个声明中,如果路径/ 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 当前代码: #!/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 'sw*') # 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 输出: 3.6.12 如果有空白(3.6.1以上)应该有应用程序名称.我现在意识到这必须是我的问题,它的使用我可能只是/应用程序的路径.
您可以使用整个grep管道作为if语句的条件.使用grep -q来保持它不会打印它找到的匹配(除非你想打印).我也简化了退出(如果你要立即使用它,就不需要在变量中存储$?).结果如下:
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 BTW,因为你将在$realcmd之后立即退出,你可以使用exec $realcmd“$@”来替换shell的$realcmd,而不是运行$realcmd作为一个子进程. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |