The Direct Path to Linux UBUNTU

Chapter 5

UNIX Shell Programming


#!/bin/sh

# Purpose: To demonstrate the if-elif-else-statement between two integer values.

# This values of VALUE1 AND VALUE2 are passed from the command line as $1 and $2.

# Author: Wahid Lutfy

# CopyRight © MyWebUnivesity.com

# Assign the values read from the command line to the variables VALUE1 AND VALUE2.

VALUE1=$1

VALUE2=$2

usage ()

{

echo ""

echo "Usage: $0 $VALUE1 $VALUE2"

echo "Example: if-args.sh 10 5"

echo

exit

}

compare ()

{

# Find it the smaller value between the two numbers read from command line

# Then, display the result of the comparison.

if [ ${VALUE1} -eq ${VALUE2} ]

then

echo "The value of ${VALUE1} is equal to the value of ${VALUE2}."

elif [ ${VALUE1} -gt ${VALUE2} ]

then

echo "The value of ${VALUE1} is greater than the value of ${VALUE2}."

else

echo "The value of ${VALUE1} is smaller than the value of ${VALUE2}."

fi

}

# If the number of arguments $# is smaller than 2, display usage.

if [ $# -lt 2 ]

then

# Call the usage function

usage

else

# Call the compare function

compare

fi


Back to UNIX Shell Programming



Contact us      |      About us      |      Term of use      |       Copyright © 2000-2024 MyWebUniversity.com ™