[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

branching in scripts

Posted on 2000-01-12
9
Medium Priority
?
290 Views
Last Modified: 2010-04-21
I have this script where I need to test for the existance of three different files.
Anyone of the three files can be missing so I need to check for all possibilities. Once I find the correct combination, how do I skip or branch off avoiding the rest of the tests and continue on with the rest of the script. I looked up the exit or goto function but the manual is pretty vague. It doesn't really say how to insert labels. Help
0
Comment
Question by:scuzz1
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 2347994
Which scripting language?
0
 

Author Comment

by:scuzz1
ID: 2349558
I need to know for sh (not csh or ksh).
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 2350029
sh does not have goto's and labels.  Meanwhile, the traditional way to do this sort of thing is with an if-else construct.  In sh, this would look something like

if [ test condition ]
then  do some stuff
elif [ other condition ]
then  do some other stuff
elif [ yet another condition ]
then  do yet different stuff
fi

mainline code goes here


Alternately, if you really need to do stuff between the if's, you could do some kind of hack like this:

while true
do  if a
      then ...
              break
      fi
      ...
      if b
      then ...
              break
      fi
      ...
      if c
      then ...
              break
      fi
      break
done

mainline code goes here


The 'break' statements will break out of the loop, so you'll never execute it a second time.
0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
LVL 51

Expert Comment

by:ahoffmann
ID: 2355428
or:

if [ ! test condition ] ; exit
0
 

Author Comment

by:scuzz1
ID: 2360851
chris,

 I am using the if statements in this script. It is possible though that finding condition "A" true when it continues on down say to condition "C" it will find condition "C" true also. That is where the problem is. This will result in a file name appearing in a spawned text file twice. I need to be able to bounce by the other if statements but stay in the while loop.

0
 
LVL 85

Expert Comment

by:ozo
ID: 2360887
    if list then list [ elif list then list ] ... [ else list ] fi
          The list following if is executed and, if it returns a zero exit
          status, the list following the first then is executed.  Otherwise,
          the list following elif is executed and, if its value is zero, the
          list following the next then is executed.  Failing that, the else
          list is executed.  If no else list or then list is executed, then
          the if command returns a zero exit status.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 2361874
>  I need to be able to bounce by the other if statements but stay in the while loop.
hmm, you match condition A and also C, but you do not want to execute statements for C. If it A and D matches, do you want to execute D? or if A, C and D matches, what should be done?

Sounds that you're looking for something like a stated loop. If so, I recommend to switch to csh (or tcsh, or zsh), which has goto. Otherwise you have to use, and check, a couple of variables.

Please enlight me which combinations of conditions are possible, and which should perform your statements.
0
 
LVL 14

Accepted Solution

by:
chris_calabrese earned 200 total points
ID: 2363200
Sounds to me like the if/elif/else construct I originally suggested would work just fine.  Please test whether this is true before rejecting my answer again.  Such code inside a while loop would look something like



while condition
do  if A
      then
             some stuff that's executed when A is true
      elif B
      then
             some stuff that's executed if A is not true and B is
      elif C
      then
            some stuff that's executed if both A and B are not
            true and C is
      else
           some stuff that's executed if A B and C are all not true
      fi
      some stuff that's executed regardless of A B and C
done
0
 

Author Comment

by:scuzz1
ID: 2363874
I got it.

I had to embed some more if statements inside of the original if statement (if that makes any sense). I was having problems with the if and elif statements. That last little syntax chris wrote helped me out.

                                                              Thanks :)
                                                              Jim (scuzz1)
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

591 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