Shell Scripting / Conditional Statements in Shell

Command Testing

This tutorial covers how to use test commands in shell scripting. Test commands are used to check certain conditions and are often used within if statements or loops.

Tutorial 4 of 4 4 resources in this section

Section overview

4 resources

Covers decision-making constructs in shell scripting using if-else, case, and test conditions.

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.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

WHOIS Lookup Tool

Get domain and IP details with WHOIS lookup.

Use tool

HTML Minifier & Formatter

Minify or beautify HTML code.

Use tool

Scientific Calculator

Perform advanced math operations.

Use tool

Robots.txt Generator

Create robots.txt for better SEO management.

Use tool

JavaScript Minifier & Beautifier

Minify or beautify JavaScript code.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help