InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Branching on Conditions in bash
Bash

Branching on Conditions in bash

InfinityCoder February 14, 2017

You want to check if you have the right number of arguments and take actionsaccordingly.

You need a branching construct.

The if statement in bash is similar in appearance to that in other programming languages:

1
2
3
4
5
6
if [ $# -lt 3 ]
then
   printf "%b" "Error. Not enough arguments.\n"
   printf "%b" "usage: myscript file1 op file2\n"
   exit 1
fi

or alternatively:

1
2
3
4
5
6
if (( $# < 3 ))
then
   printf "%b" "Error. Not enough arguments.\n"
   printf "%b" "usage: myscript file1 op file2\n"
   exit 1
fi

Here’s a full-blown if with an elif (bash-talk for else-if) and an else clause:

1
2
3
4
5
6
7
8
9
10
11
12
13
if (( $# < 3 ))
then
   printf "%b" "Error. Not enough arguments.\n"
   printf "%b" "usage: myscript file1 op file2\n"
   exit 1
elif (( $# > 3 ))
then
   printf "%b" "Error. Too many arguments.\n"
   printf "%b" "usage: myscript file1 op file2\n"
   exit 2
else
   printf "%b" "Argument count correct. Proceeding...\n"
fi

You can even do things like this:

1
2
3
[ $result = 1 ] \
  && { echo "Result is 1; excellent." ; exit 0; } \
  || { echo "Uh-oh, ummm, RUN AWAY! " ; exit 120; }

We have two things we need to discuss: the basic structure of the if statement and how it is that we have different syntax (parentheses or brackets, operators or options) for the if expression.

The first may help explain the second. The general form for an if statement, from the manpage for bash, is:

1
if list; then list; [ elif list; then list; ] ... [ else list; ] fi

The [ and ] in our description here are used to delineate optional parts of the statement (e.g., some if statements have no else clause).

So let’s look for a moment at the if without any optional elements.
The simplest form for an if statement would be:

1
if list; then list; fi

The then list seems to make sense—it’s the statement or statements that will be executed provided that the if condition is true—or so we would surmise from other programming languages.

But what’s with the if list? Wouldn’t you expect it to be if expression?
You might, except that this is a shell—a command processor.

Its primary operation is to execute commands. So the list after the if is a place where you can put a list of commands.

What, you ask, will be used to determine the branching—the alternate paths of the then or the else?

It will be determined by the return value of the last command in the list.

(The return value, you might remember, is also available as the value of the variable $?.)
Let’s take a somewhat strange example to make this point:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ cat trythis.sh
if ls; pwd; cd $1;
then
   echo success;
else
   echo failed;
fi
pwd
 
$ bash ./trythis.sh /tmp
...
$ bash ./trythis.sh /nonexistant
...
$

In this strange script, the shell will execute three commands (an ls, a pwd, and a cd) before doing any branching.

The argument to the cd is the first argument supplied on the shell script invocation.

If there is no argument supplied, it will just execute cd, which returns you to your home directory.
So what happens? Try it yourself and find out. The result showing “success” or “failed” will depend on whether or not the cd command succeeds.

In our example, the cd is the last command in the if list of commands.

If the cd fails, the else clause is taken, but if it succeeds, the then clause is taken.
Properly written commands and built-ins will return a value of 0 (zero) when they encounter no errors in their execution.

If they detect a problem (e.g., bad parameters, I/O errors, file not found), they will return some non-zero value (often a different value for each different kind of error they detect).
This is why it is important for both shell script writers and C (and other language) programmers to be sure to return sensible values upon exiting from their scripts and programs.

Someone’s if statement may be depending on it! OK, so how do we get from this strange if construct to something that looks like a real if statement—the kind that you are used to seeing in programs?

What’s going on with the examples that began this recipe? After all, they don’t look like lists of statements.
Let’s try this on for size:

1
2
3
4
if test $# -lt 3
then
   echo try again.
fi

Do you see something that looks like, if not an entire list, then at least like a single shell command—the built-in command test, which will take its arguments and compares their values?

The test command will return a 0 if true or a 1 otherwise. To see this yourself, try the test command on a line by itself, and then echo $? to see its return value.
The first example we gave that began if [ $# -lt 3 ] looks a lot like the test statement— because the [ is actually the test command—with just a different name for the same command.

(When invoked with the name [ it also requires a trailing ] as the last parameter, for readability and aesthetic reasons.)

So that explains the first syntax—the expression on the if statement is actually a list of only one command, a test command.

Now what about the if (( $# < 3 )) expression in our list of examples in the Solution section?

The double parentheses are one of several types of compound commands.
This kind is useful for if statements because it performs an arithmetic evaluation of the expression between the double parentheses.

This is a more recent bash improvement, added for just such an occasion as its use in if statements.
The important distinctions to make with the two kinds of syntax that can be used with the if statement are the ways to express the tests, and the kinds of things for which they test.

The double parentheses are strictly arithmetic expressions.

The square brackets can also test for file characteristics, but its syntax is much less streamlined for arithmetic expressions.

This is particularly true if you need to group larger expressions with parentheses (which need to be quoted or escaped).

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Leaking Passwords into the Process List in bash
ps may show passwords entered on the command line in …

Leaking Passwords into the Process List in bash

Finding and Running Commands in bash
You need to find and run a particular command under …

Finding and Running Commands in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap