Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If grep finds what it is looking for do X else Y [duplicate]

Tags:

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.

like image 861
Fredrik Avatar asked Oct 23 '12 06:10

Fredrik


People also ask

What does grep return if found?

The -q (or --quiet ) tells grep to run in quiet mode not to display anything on the standard output. If a match is found, the command exits with status 0 . This is useful when using grep in shell scripts where you want to check whether a file contains a string and perform a certain action depending on the result.

What does grep return if not found?

If the pattern isn't found in the file, grep returns false (i.e. exits with a non-zero exit code), so the echo is executed.

Can we use grep in if condition in shell script?

The if condition is fulfilled if grep returns with exit code 0 (which means a match).


1 Answers

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.

like image 87
Gordon Davisson Avatar answered Oct 02 '22 00:10

Gordon Davisson