如果grep找到它正在查找的内容,则执行X else Y [英] If grep finds what it is looking for do X else Y

查看:155
本文介绍了如果grep找到它正在查找的内容,则执行X else Y的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个声明中,我试图匹配如果路径/ app / $ var1(应用程序名称)中存在版本($ var2)

  if 
find / app / $ var1 -maxdepth 1 -type l -o -type d | grep $ var2#在一个不错的列表中结果,我可以手动获得一个真正的匹配。
#如果找到匹配,则执行命令。
$ realcmd$ @
rc = $?
exit $ rc
else
echo不匹配,您正在查找的版本不存在
fi

当前代码:
这包括我所有的代码(未清理)。
命令我运行:./xmodule load firefox / 3.6.12
此版本退出

 #!/ bin / bash 
#一些命令的钩子
$ b $ #echo $ @#在xmodule之后输入的sting值

cmd = $ (basename$ 0)
#echo叫做$ cmd

if [[$ cmd =xmodule]]
然后
realcmd ='/ app / modules / 0 / bin / modulecmd tcsh'

#验证参数
fi
#检查$ @是否包含值/以确定是否请求特定版本。
case
* / *)
echohas slash
var1 = $(echo$ @| grep -Eio'\s\ w *')#获取应用程序名称并将其放入var1
echo $ var1#不打印应为firefox
var2 = $(echo$ @| grep -o'[^ /] * $')#获取版本名称并将其放入var2
echo $ var2
#检查/ app / appname / $ b $中是否存在版本号/ find / app / $ var1 - noleaf -maxdepth 1 -type l -o -type d | grep $ var2;然后
$ realcmd$ @
出口$?
else
回声不匹配,您正在查找的版本不存在
#此处是否应该有退出?
fi
;;

*)
echo没有斜杠
;;
esac

输出:
mycomputer [9:55 am] [user / Desktop / script] - > ./xmodule load firefox / 3.6.12
'斜杠



3.6.12
不匹配,您的版本寻找不存在



如果有空白(高于3.6.1),应该有应用程序名称。我现在意识到,这一定是我的问题,它会使用我可能只是/ app的路径。
但是我不认为我改变了那部分代码中的任何东西。

解决方案

您可以使用整个grep管道作为 if 语句的条件。使用 grep -q 来防止打印它找到的匹配(除非你想打印它)。我也简化了退出(如果你只是立即使用它,不需要在变量中存储 $?)。结果如下:

  if find/ app / $ var1-maxdepth 1 -type l -o -type d | grep -q$ var2;然后
$ realcmd$ @
出口$?
else
回声不匹配,您正在查找的版本不存在
#此处是否应该有退出?
fi

顺便说一句,既然你要在$ realcmd后立即退出,你可以使用 exec $ realcmd$ @用$ realcmd替换shell,而不是将$ realcmd作为子进程运行。


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

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

output: mycomputer [9:55am] [user/Desktop/script] -> ./xmodule load firefox/3.6.12 'has slash

3.6.12 no match, the version you are looking for does not exist

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.

解决方案

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

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 else Y的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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