0% found this document useful (0 votes)
5 views7 pages

Ass 03

The document provides an overview of user-defined variables and their usage in shell scripting, including accessing variables and performing arithmetic operations. It covers comparison and logical operators, along with examples of conditional statements, case statements, and while loops. Additionally, it illustrates how to determine if a number is even or odd and compare two numbers using user input.

Uploaded by

ftwaryan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Ass 03

The document provides an overview of user-defined variables and their usage in shell scripting, including accessing variables and performing arithmetic operations. It covers comparison and logical operators, along with examples of conditional statements, case statements, and while loops. Additionally, it illustrates how to determine if a number is even or odd and compare two numbers using user input.

Uploaded by

ftwaryan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

User-Defined Variables

name="John"
num=25
Accessing Variables:
echo "Name: $name"
echo "Number: $num"

Shell scripting supports basic arithmetic using operators like + , - , * , / , and %


Use the expr command or double parentheses (( )) for arithmetic operations

Using expr
result=$(expr 5 + 3)

Using double parentheses


result=$((5 + 3))

echo "Result: $result“


Comparison Operators:
-eq: equal to
-ne: not equal to
-lt: less than
-le: less than or equal to
-gt: greater than
-ge: greater than or equal to

Logical Operators:
&&: logical AND
||: logical OR

Example using logical operators


if [ $a -lt $b ] && [ $a -gt 5 ]; then
echo "$a is between 5 and $b"
fi
Number is even or odd
echo "Enter a number:"
read num
if [ $((num % 2)) -eq 0 ]; then
echo "$num is even"
else
echo "$num is odd"
fi

Compare two numbers


echo "Enter two numbers:"
read a b
if [ $a -gt $b ]; then
echo "$a is greater than $b"
else
echo "$a is less than or equal to $b"
fi
Case statement

case variable in
pattern1)
# commands to execute if variable matches pattern1
;;
pattern2)
# commands to execute if variable matches pattern2
;;
*)
# commands to execute if variable doesn't match any pattern
;;
esac
echo "Enter a day (Monday, Tuesday, etc.):"
read day

case $day in
Monday)
echo "Start of the work week."
;;
Tuesday)
echo "Second day of the work week."
;;
Wednesday)
echo "Midweek."
;;
Thursday)
echo "Almost the weekend."
;;
Friday)
echo "Last day of the work week."
;;
Saturday | Sunday)
echo "It's the weekend!"
;;
*)
echo "Invalid day."
;;
esac
While loop

while [ condition ]
do
# commands to be executed repeatedly
done

Example:

count=1

while [ $count -le 5 ]


do
echo "Count is: $count"
((count++)) # Increment count by 1
done

You might also like