Command Testing

Tutorial 4 of 4

Command Testing Tutorial

1. Introduction

In this tutorial, we will learn how to use test commands in shell scripting. Test commands are used to check certain conditions and are often utilized within if statements or loops.

After completing this tutorial, you will be able to:
- Understand the syntax of test commands
- Use test commands in if statements and loops
- Test different conditions in your scripts

Prerequisites: Basic understanding of shell scripting.

2. Step-by-Step Guide

Test commands in shell scripting are used to check file types and compare values. The syntax for a test command is as follows: test EXPRESSION.

2.1 Using test commands in if statements

You can use test commands in if statements to execute code based on certain conditions. Here's an example:

if test -f /etc/passwd
then
  echo "File exists"
else
  echo "File does not exist"
fi

In this script, the -f flag checks if the file /etc/passwd exists. If it does, the script prints "File exists". If not, it prints "File does not exist".

2.2 Using test commands in loops

You can use test commands in loops to execute code multiple times based on a condition. Here's an example:

i=0
while test $i -lt 10
do
  echo $i
  i=$((i+1))
done

In this script, the -lt flag checks if the variable i is less than 10. If it is, the script prints the value of i and increments i by 1. This loop continues until i is no longer less than 10.

3. Code Examples

3.1 Checking if a file exists

# Check if a file exists
if test -f /etc/passwd
then
  # If the file exists, print this message
  echo "File exists"
else
  # If the file does not exist, print this message
  echo "File does not exist"
fi

Expected output: "File exists"

3.2 Checking if a number is greater than another

# Define a number
num=10

# Check if the number is greater than 5
if test $num -gt 5
then
  # If the number is greater than 5, print this message
  echo "The number is greater than 5"
else
  # If the number is not greater than 5, print this message
  echo "The number is not greater than 5"
fi

Expected output: "The number is greater than 5"

4. Summary

In this tutorial, we learned how to use test commands in shell scripting. We covered the syntax of test commands and how to use them in if statements and loops. We also explored different flags for testing conditions.

Next steps for learning include exploring other flags for test commands and using test commands in more complex scripts.

5. Practice Exercises

  1. Write a script that checks if a directory exists. If it does, print "Directory exists". If not, print "Directory does not exist".

Solution:

if test -d /etc
then
  echo "Directory exists"
else
  echo "Directory does not exist"
fi
  1. Write a script that checks if a number is less than or equal to 5. The number should be defined at the beginning of the script.

Solution:

num=3

if test $num -le 5
then
  echo "The number is less than or equal to 5"
else
  echo "The number is greater than 5"
fi
  1. Write a script that checks if a file is readable, writable, and executable. The file should be defined at the beginning of the script.

Solution:

file=/etc/passwd

if test -r $file -a -w $file -a -x $file
then
  echo "The file is readable, writable, and executable"
else
  echo "The file is not readable, writable, and executable"
fi

For further practice, try to create more complex scripts that use multiple conditions.