# how to compare a substring in FreeBSD default shell?



## mrjayviper (May 11, 2014)

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)

```
#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. *S*omething like

```
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? *T*hanks a lot again!


----------



## fonz (May 11, 2014)

Simply use grep -q and check $?. The following works for me:

```
echo "The cow jumped over the moon" | grep -q cow
test $? -eq 0 && echo "I love beef." || echo "No dice."
```


----------



## mrjayviper (May 11, 2014)

Thank you for that. What if I need to run multiple commands and not just saying "I love beef"?

Something like

```
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


----------



## fonz (May 11, 2014)

mrjayviper said:
			
		

> What if I need to run multiple commands and not just saying "I love beef"?




```
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:

```
OUTPUT=`my-app-here list`
echo ${OUTPUT} | grep -q cow
if [ $? -eq 0 ]; then
  do something here
  do something else too
fi
```


----------



## sotownsend (Jun 21, 2014)

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? *T*hanks 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`:

```
#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.

```
seo@fittr.com:~ # diff /bin/csh /bin/tcsh -s
Files /bin/tcsh and /bin/csh are identical
```


----------



## kpa (Jun 21, 2014)

sotownsend said:
			
		

> mrjayviper said:
> 
> 
> 
> ...



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.


----------

