MLUG Bash Scripting Workshop 25/04/08
Prev Index Next

Testing with an if statement:

Bash runs commands from a script from the top down. So it will not progress to the next command until it is finished with the task at hand.

We can us the Bash built-in test to see if a variable is empty (so to speak) and then the Bash built-in if to prompt the user to provide input for the variable

Here's a snippet from our soon to be completed contovob script:


 test -n "$INPUT_FILE"
   

Test evaluates conditions. In this snippet I have used " -n " with test to see if the the string $INPUT_FILE has a non-zero length (is null). If it is not null, it means that a filename (or something) was entered for the input file. See the test manpage for all the conditional operators available.

Almost all commands will pass a number to the system when they exit. If the command has run without a problem it will exit with a zero. Anything else but a zero means something has gone wrong and some programs will actually indicate what has gone wrong by the exit number which is used as the error code.

Test will exit with either a 0 indicating a nonzero string in the variable or 1 indicating no string in the variable.

While we won't see any number it is still there until it is overwritten (in the system memory) by the next command or process so if we follow test immediately with an if statement we can check and see if it is a 1 and do something about it.


	if [ $? -eq 1 ]; then
     	echo -e "\nUsage: contovob [input file] [output file]\n"
     	exit
   	fi

	# If $? equals 1 it means test exited with a 1 which means a null string

You can probably get the basic idea of what going on in the snippet above but I'll give you some details:

  1. The condition that has to be met is enclosed in square brackets.
  2. The expression " $? " is used to grab the exit number from test for evaluation.
  3. " -eq 1 " says that we are checking to see if $? equals 1.
  4. If $? equals 1 a message will print to screen.
  5. " exit " is a bash built-in. After printing the message bash will exit the script. There is nothing more to do and the user will have to run the script again Hopefully with the variables included.
  6. The word " fi " indicates the end of the if statement.

If you are wondering why my usage of echo is different this time, I have used the " -e " option with echo. Echo exits with a newline, i.e. the cursor drops down one line and/or a command is run. By using " \n " with " echo -e " the cursor drops down a second line.

I will cover this usage of echo in greater detail later but first have a look at example02 below. Once you had a read, run it and see if it works the way you expected. Just like example01, feel free to open it up in an editor, have a play then run it in a terminal.


#!/bin/bash
# example_02
# This is just example01 with the check to see if variables 
# were entered.

echo
echo

# Secondly create two variables from the names entered
first_name=$1
second_name=$2

# Check out the first variable
test -n "$first_name"
  if [ $? -eq 1 ]; then
    echo -e "\nUsage: example_02 [first name] [surname]\n"
    exit
  fi

# Check out the second variable
test -n "$second_name"
  if [ $? -eq 1 ]; then
    echo -e "\nUsage: example_02 [first name] [surname]\n"
	echo -e "Get with the program $first_name!\n\n"
    exit
  fi


# Let's get some time and date info
echo "It is now `date`"
echo

# Now let's echo the other variables to standard output
echo "My name is $first_name $second_name"
echo
# end of script


Top