24

I have a string

$VAR="I-UAT"; 

in my shell script code. I need a conditional statement to check if "UAT" is present in that string.

What command should I use to get either true or false boolean as output? Or is there any other way of checking it?

Nate
  • 31,017
  • 13
  • 83
  • 207
batty
  • 597
  • 2
  • 6
  • 15

7 Answers7

25

What shell? Using bash:

if [[ "$VAR" =~ "UAT" ]]; then
    echo "matched"
else
    echo "didn't match"
fi
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
  • 6
    Also, bash only, `[[ "$VAR" = *UAT* ]]` -- right hand side must be unquoted for pattern matching. – glenn jackman Jul 26 '11 at 01:18
  • pretty sure that's a bash only feature. Is your /bin/sh a symlink to bash? – glenn jackman Jul 26 '11 at 01:19
  • voted -1 because this answer is bash-specific while the `case`-based solutions will work on any POSIX shell. – adl Jul 26 '11 at 20:21
  • @adl - I guess I understand the logic there, but clearly the OP was using bash or my answer would not have been accepted. I would argue that you should edit the question so that it reflects the OP's intention, rather than downvote answers that are specific to a certain shell. – Andrew Clark Jul 26 '11 at 20:29
  • 1
    @Andrew - I too believe the OP uses bash, but since he did not mention it in the question we cannot be sure this is intentional. Maybe he simply does not know that many different shells have all sort of extensions that you should avoid if you want to write portable scripts. In that case he might later wonder why his script fails in other environments. Since there is an easy solution that is *portable*, I honestly believe it is best to avoid exotic features. – adl Jul 26 '11 at 20:46
9

You can do it this way:

case "$VAR" in
  *UAT*)
   # code when var has UAT
  ;;
esac
Diego Sevilla
  • 28,636
  • 4
  • 59
  • 87
  • 3
    `case "$VAR" in` can always be replaced by `case $VAR in`. Shells do not perform *field splitting* or *pathname expansion* on the word that follows `case`. Similarly, `var="$other"` is as safe as `var=$other`. – adl Jul 26 '11 at 20:12
6

The classic way, if you know ahead of time what string you're looking for, is a case statement:

case "$VAR" in
*UAT*) : OK;;
*)     : Oops;;
esac

You can use an appropriate command in place of the : command. This will work with Bourne and Korn shells too, not just with Bash.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
4
found=`echo $VAR | grep -c UAT`

Then test for $found non-zero.

jman
  • 11,334
  • 5
  • 39
  • 61
  • This will also work. `-c, --count` Suppress normal output; instead print a count of matching lines for each input file. – Hari Ennekat Mar 03 '20 at 07:28
1

In bash script you could use

if [ "$VAR" != "${VAR/UAT/}" ]; then
  # UAT present in $VAR
fi
Neil
  • 54,642
  • 8
  • 60
  • 72
1

try with grep:

$ echo I\-UAT | grep UAT
$ echo $?
0
$ echo I\-UAT | grep UAX
$ echo $?
1

so testing

if [ $? -ne 0 ]; then
  # not found
else
  # found
fi
vitalymak
  • 47
  • 6
marcelog
  • 7,062
  • 1
  • 33
  • 46
0

I like this a little better than using case/esac (and it'll work with non-bash shells):

#!/bin/sh

full_string="I-UAT"
substring="UAT"

if [ -z "${full_string##*$substring*}" ]; then
        echo "Found substring!"
else
        echo "Substring is MIA!"
fi

If the string returned is zero-length (-z), then the substring was found.

Andy Alt
  • 297
  • 2
  • 12