• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 521
  • Last Modified:

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

0
ifeatu
Asked:
ifeatu
1 Solution
 
TintinCommented:
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
0
 
ozoCommented:
#!/bin/bash
env=reproduce
if [ "$env" != "${env/prod}" ]; then
echo "This is a production Server!"
fi
case "$env" in
*prod*) echo "This is a production Server!" ;;
esac
0
 
ifeatuAuthor Commented:
w
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now