Link to home
Start Free TrialLog in
Avatar of pixitron
pixitron

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of jlevie
jlevie

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 jlevie
jlevie

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
Avatar of pixitron

ASKER

cheers - works great now!


For future reference - why exactly did the lack of ' around the string cause the problem?
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
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
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
   
cool - thanks for that