Need help with a very simple shell script

Status
Not open for further replies.
Joined
Sep 26, 2015
Messages
6
Hello,

I'm posting this here instead of the Help & Support forum because it's not really a FreeNAS question. I know next to nothing about programming/scripting but so far I have been able to google my way through most problems, but this one has me stumped.

I have a variable, $var, that's a string and I want to search that string to see if it contains another string. Here's what I have tried to far:

if [ "$var" == *string*]
then

I've also tried this and a few other things:

if [ "$var" == *"string"*]
then

I'm sure that it's a simple fix but I haven't been able to figure it out. I would greatly appreciate any help and if anyone has a good resource on learning shell scripting I would love to read it. Thanks!
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
Joined
Sep 26, 2015
Messages
6
Yup, I've done a lot of searching. Most of those links use bash which according to my understanding is not the default shell in FreeBSD/FreeNAS. I know that it's possible to install bash and change it to the default shell, but I figured it would be better to learn the correct way.
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
Bash is already installed. No need to change it to the default shell (actually it's the default shell in the web GUI shell, and csh is the default shell on SSH), just use the correct shebang (e.g. #!/bin/bash) ;)

If you want to do it the "correct" way (good reflex :)), I assume you mean the POSIX way, then add posix to your search, you should find the answers you're looking for ;)
 
Last edited:
Joined
Sep 26, 2015
Messages
6
Ah, okay. I had no idea that bash was installed out of the box. That'll make it a lot easier then. I'll give it a shot, thanks!
 

jgreco

Resident Grinch
Joined
May 29, 2011
Messages
18,680
Changing to bash is a bad idea. Bash is a crappy shell scripting language that attempts to make lots of things "easy" (and in many cases *does*) but it is often confused for sh script, since lots of systems have bash for "/bin/sh". Real shell scripters do it in real sh.

The lazy way to check for a substring within a string, using /bin/sh, portably, going back many years, is something like

Code:
case "X${var}" in
        X*abc*)       echo "String contains abc";;
        X*def*)       echo "String contains def but not abc";;
        *)            echo "String contained neither abc nor def";;
esac


You can eliminate the second and third cases if not needed. The leading "X" works around certain versions of sh that would mishandle an empty or undefined ${var} - probably NO modern sh, but old habits die hard!
 

j_r0dd

Contributor
Joined
Jan 26, 2015
Messages
134
There are many ways to achieve what you are trying to do.

Code:
if [[ -n $(grep "your string" ${VAR}) ]]; then
    echo "found your string"
  else
    echo "your shit was not found"
fi
 

jgreco

Resident Grinch
Joined
May 29, 2011
Messages
18,680
There are many ways to achieve what you are trying to do.

Code:
if [[ -n $(grep "your string" ${VAR}) ]]; then
    echo "found your string"
  else
    echo "your shit was not found"
fi

That's a bash-ism, good idea to avoid that if you want something that's portable.
 

BDMcGrew

Dabbler
Joined
Sep 22, 2015
Messages
49
echo $var | grep string >> /dev/null 2>&1
[ $? -eq 0 ] && echo "found string"
 
Joined
Sep 26, 2015
Messages
6
Thanks everyone for your help. It's been a busy couple of days but hopefully I can spend some time tonight finishing up the script!
 

jgreco

Resident Grinch
Joined
May 29, 2011
Messages
18,680
echo $var | grep string >> /dev/null 2>&1
[ $? -eq 0 ] && echo "found string"

Quoting issues aside, another classic solution, albeit one that spawns some processes. I am particularly thrilled to find someone who has used a portable grep strategy rather than "grep -q".
 
Status
Not open for further replies.
Top