Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

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?
0
pixitron
Asked:
pixitron
  • 3
  • 2
  • 2
1 Solution
 
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
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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