Pattern matching if statement in shell script

Posted on 2009-12-29
Last Modified: 2013-12-26
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

# unixinf
# gather information for unix system
env=`grep hcsEnvironment /etc/`
mname=`uname -n`
supteam=`grep hcsTEAM /etc/`

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

echo "This is a production Server!"
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

Question by:ifeatu
    LVL 48

    Expert Comment

    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
         echo "This is a production server"
    LVL 84

    Accepted Solution

    if [ "$env" != "${env/prod}" ]; then
    echo "This is a production Server!"
    case "$env" in
    *prod*) echo "This is a production Server!" ;;

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now