[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Why do I get an error with an if statement in shell script?

Posted on 2009-12-29
7
Medium Priority
?
296 Views
Last Modified: 2013-12-26
I get a "./file.sh: test: unknown operator ==" error when I try to run my script

Can anyone help?
#!/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
Comment
Question by:ifeatu
7 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 26143114
perhaps you misspelled "="
0
 
LVL 4

Accepted Solution

by:
already_used earned 1000 total points
ID: 26143119
Test operators are:  -eq for integer test, or = for character string test.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 26143126
With ksh use a single '=' !

wmp
0
Technology Partners: 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!

 

Author Closing Comment

by:ifeatu
ID: 31671060
Thanks
0
 

Author Comment

by:ifeatu
ID: 26143163
Another question on the same script...basically Im just trying to match whether or not the variable "$env" contains the word prod.
0
 

Author Comment

by:ifeatu
ID: 26143166
The syntax I have doesnt seem to be working.
0
 
LVL 4

Expert Comment

by:already_used
ID: 26145199
Try replacing the "if $env = *prod* " expression with the following code.
echo $env|grep prod
if [ $? -eq 0 ]
then
   $env contains "prod"
else
   $env does not contain prod 
fi   

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Suggested Courses

834 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