Simple scripting problem

Hi,

I'm just starting to learn bash scripting.... whats wrong with the following:

##############################################
#Cygwin Check
system="cygwin"
echo $system
if test [$system = cygwin]
then
      echo "Backing up a Windows based machine"
elif test [$system = linux]
then
      echo "Backing up a Linux based machine"
else
      echo "Machine Type Problem"
fi
############################################

The else condition is always executed, even though the result of the echo $system is cygwin.

Any thoughts?
pixitronAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jlevieCommented:
Use:

#Cygwin Check
system="cygwin"
echo $system
if [ $system = 'cygwin' ]
then
     echo "Backing up a Windows based machine"
elif [ $system = 'linux' ]
then
     echo "Backing up a Linux based machine"
else
     echo "Machine Type Problem"
fi
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jlevieCommented:
Another way of doing this sort of check would be:

case $system in
  cygwin)
    echo "Backing up a Windows based machine"
    ;;
  linux)
    echo "Backing up a Linux based machine"
    ;;
  *)
    echo "Machine Type Problem"
esac

Or if Cygwin supports 'uname -s':

case `uname -s` in
  cygwin)
    echo "Backing up a Windows based machine"
    ;;
  Linux)
    echo "Backing up a Linux based machine"
    ;;
  *)
    echo "Machine Type Problem"
esac
0
pixitronAuthor Commented:
cheers - works great now!


For future reference - why exactly did the lack of ' around the string cause the problem?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

255x4Commented:
Actually, there was a few problems.

1. Your test to see if system was cygwin or linux does not require a name

if test [$system = cygwin]

does not need the name test in it, just the if statement, and the comparison in the brackets.

2. You were trying to make a character test, rather than a value test, so your $system needs to equal a character string which is indicated by either single or double quotes ( ' or " ).  Either will work in this situation.

There you go.

BTW: I noticed that this is probably going to be used to test whether you are connected to a cygwin or linux system to make a backup, how are you going to test if there is a connection and what system type you are connected to?

Thanks,

255x4
0
pixitronAuthor Commented:
I want this script to be install on either a linux or windows/cygwin based setup. The user has to make a few trival changes including a set a variable called system to either linux or cygwin (i guess i probably could even poll some system variable and find this out automatically)


Hoping to do a connection test with the following (just got it from another ee question):
ping $backup_server >/dev/null
if [ $? -ne 0 ]; then
  echo "Sorry no route to the Backup Server Exists";
  echo "This is probably due to lack of a network connection?";
  echo "Quiting without completion of backup"
  exit 1;       
fi

The backup server is an old machine with fedora core 2

Thanks for the help
0
255x4Commented:
Here is something I use for our server based backup:

machinename=$1 #This tells it to us the first item on the command line such as backup WEATHER
str=$(smbclient -L $machinename | grep failed)

if [ $str = "" ]  #if smbclient fails (such as machine not powered on) this would be false
then       #this is what happens if smbclient DID NOT FAIL to find the machine

else       #This is where I put the error message to tell me that the machine was not up at the time

fi

So what you can do is remember to give a variable name, and equal sign, the $ then a command or series of commands in () and then go from there for your tests.

Thanks,

255x4
   
0
pixitronAuthor Commented:
cool - thanks for that
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Software

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.