Link to home
Start Free TrialLog in
Avatar of ifeatu
ifeatu

asked on

Pattern matching if statement in shell script

I want my script to search the variable "$env" and find the word "prod" (without the quotes)
and if it finds it I want it to say that its a production server

for some reason my pattern matching doesnt seem to work


#!/bin/sh
# unixinf
# gather information for unix system
#
env=`grep hcsEnvironment /etc/hcs.info`
mname=`uname -n`
supteam=`grep hcsTEAM /etc/hcs.info`
unixver=`uname`
#

if [ "$env" == *prod* ]; then

echo "This is a production Server!"
fi
echo "Machine Name" > /home/tsv0g0/tmp/chkout
uname -a >> /home/tsv0g0/tmp/chkout
echo " " >> /home/tsv0g0/tmp/chkout
echo "The support team for $mname: $supteam" >> /home/tsv0g0/tmp/chkout
echo "The status of $mname: $env" >> /home/tsv0g0/tmp/chkout
echo " " >> /home/tsv0g0/tmp/chkout
echo "Uptime" >> /home/tsv0g0/tmp/chkout
uptime >> /home/tsv0g0/tmp/chkout
echo " " >> /home/tsv0g0/tmp/chkout

Open in new window

Avatar of Tintin
Tintin

bourne shell doesn't allow pattern matching.  You can do it with bash.

A generic solution that will work on all Unix/Linux flavours is

if echo "$env" | grep prod >/dev/null
then
     echo "This is a production server"
fi
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ifeatu

ASKER

w