how to compare a substring in FreeBSD default shell?

Hello. I'm trying to check for the existence of a string from program output.

E.g. (not a real application but I just want to give an example)
Code:
#my-app-here list
The cow jump over the moon.
#
I want to check for the existence of the word "cow" in the program output. Something like
Code:
if [ "cow" is a substring of "`my-app-here list`" ]; then
  echo "I love beef"
fi

I tried searching the net but I'm not really sure what to look for. There must be a command to do it. I'm just not sure what it is.

Any ideas what script/program could do it for me? Thanks a lot again! :)
 
Simply use grep -q and check $?. The following works for me:
Code:
echo "The cow jumped over the moon" | grep -q cow
test $? -eq 0 && echo "I love beef." || echo "No dice."
 
Thank you for that. What if I need to run multiple commands and not just saying "I love beef"?

Something like
Code:
if [ "cow" is a substring of "`my-app-here list`" ]; then
  do something 1
  case $test in
  ..
  ..
  esac
  do more stuff here.
  if [ condition1 ]; then
  fi
fi

Thanks again :)
 
mrjayviper said:
What if I need to run multiple commands and not just saying "I love beef"?
Code:
my-app-here list | grep -q cow
if [ $? -eq 0 ]; then
  do something here
  do something else too
fi
Or, if you need the output for further processing, something like:
Code:
OUTPUT=`my-app-here list`
echo ${OUTPUT} | grep -q cow
if [ $? -eq 0 ]; then
  do something here
  do something else too
fi
 
mrjayviper said:
I tried searching the net but I'm not really sure what to look for. There must be a command to do it. I'm just not sure what it is.

Any ideas what script/program could do it for me? Thanks a lot again! :)

FreeBSD uses the tcsh shell, a.k.a "c-shell with extras" (http://www.freebsd.org/doc/en/articles/linux-users/shells.html)

On any *nix environment, you can get a listing of the current shell by typing chsh:
Code:
#Changing user information for seo
Login: root
Password: *
Shell: /bin/csh

Notice that it says "/bin/csh" and not "/bin/tcsh". They are actually the same exact binaries as tcsh is just an extension on csh.
Code:
seo@fittr.com:~ # diff /bin/csh /bin/tcsh -s
Files /bin/tcsh and /bin/csh are identical
 
sotownsend said:
mrjayviper said:
I tried searching the net but I'm not really sure what to look for. There must be a command to do it. I'm just not sure what it is.

Any ideas what script/program could do it for me? Thanks a lot again! :)

FreeBSD uses the tcsh shell, a.k.a "c-shell with extras" (http://www.freebsd.org/doc/en/articles/linux-users/shells.html)

On any *nix environment, you can get a listing of the current shell by typing chsh:
Code:
#Changing user information for seo
Login: root
Password: *
Shell: /bin/csh

Notice that it says "/bin/csh" and not "/bin/tcsh". They are actually the same exact binaries as tcsh is just an extension on csh.
Code:
seo@fittr.com:~ # diff /bin/csh /bin/tcsh -s
Files /bin/tcsh and /bin/csh are identical

This is true only for the default interactive shell of the root account (and it's that way for purely historical reasons, let's not start another bikeshed about it). Otherwise you're pretty much expected to use the standard bourne shell sh(1) for scripting and more complicated pipelines.
 
Back
Top