0% found this document useful (0 votes)
15 views

Assignment#3

The document contains 15 sections that demonstrate various programming concepts in shell scripting like printing output, taking user input, performing operations, conditional statements, loops, and more using many built-in Linux commands.

Uploaded by

nandkishor pal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Assignment#3

The document contains 15 sections that demonstrate various programming concepts in shell scripting like printing output, taking user input, performing operations, conditional statements, loops, and more using many built-in Linux commands.

Uploaded by

nandkishor pal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

P1: Program to print hello world.

echo "Hello World"

P2: Program to Display calendar.

echo "The Month is"

cal # cal command displays current month by default

echo "An alternate view of calender"

ncal # An alternale layout for calender

P3: Program to a simple script to read user input and perform operations with them.

echo -n "Enter number 1 : " # -n option supresses newline

read NUM1 # Read the user input from Standard Input and store in Variable NUM1

echo -n "Enter number 2 : "

read NUM2

SUM=$(($NUM1 + $NUM2)) # Arithmetic expansion using double parentheses

echo "The sum is $SUM"

SUM=`expr $NUM1 + $NUM2` # Arithmetic expansion using backticks.

#Usage of expr command to evaluate the expression

echo "The sum is $SUM"

P4: A script to show usage of if condition.

NUM1=5 # variabe assignment

NUM2=2

if [ $NUM1 -gt $NUM2 ] # -gt is to test intiger numbers

then
echo "NUM1 > NUM2"

fi

P5: A script to show usage of if else condition.

NUM1=2 # Variabe assignment

NUM2=5

if [ $NUM1 -lt $NUM2 ] # -lt is to test intiger numbers

then

echo "NUM1 < NUM2"

else

echo "NUM1 > NUM2"

fi

P6: A script to show usage of else if condition.

echo -n "Enter a number: "

read NUM

if [ $NUM -gt 0 ]

then

echo "$NUM is +ve"

elif [ $NUM -lt 0 ]

then

echo "$NUM is -ve"

else

echo "$NUM is 0"

fi

echo "done"
P7: A script to demonstrate case statement.

echo -n "Enter a character: "

read CHAR

case $CHAR in

a) echo "You entered $CHAR which is a vowel";; # ;; Terminates each option

e) echo "You entered $CHAR which is a vowel";;

i) echo "You entered $CHAR which is a vowel";;

o) echo "You entered $CHAR which is a vowel";;

u) echo "You entered $CHAR which is a vowel";;

*) echo "You entered $CHAR which is not a vowel";; # Defaults to everything else

esac

echo "What if you enter upper case letters!!?, Check the next example"

P8: A script to demonstrate case statement.

echo -n "Enter a character: "

read CHAR

case $CHAR in

a | A) # Test for both Lower or Upper case letter

# You may write commands in this fashion too!!, means multiline commands

echo "You entered $CHAR which is a vowel"

;; # Terminates each option

e | E)

echo "You entered $CHAR which is a vowel"

;;

i | I)
echo "You entered $CHAR which is a vowel"

;;

o | O)

echo "You entered $CHAR which is a vowel"

;;

u | U)

echo "You entered $CHAR which is a vowel"

;;

*) # Defaults to everything else

echo "You entered $CHAR which is not a vowel"

;;

esac

P9: A script to demonstrate case statement.

echo -n "Oceans are larger than lakes (True or False) : "

read USER_INPUT

case $USER_INPUT in

"TRUE"| "True" | "true")

echo "Yes you are right"

;; # Terminates each option

"FALSE" | "Fasle" | "false")

echo "No your are wrong"

;;

*) # Defaults to everything else

echo "Please enter either True or False"

;;

Esac
P10: A simple script to show usage of string compare operator = and !=.

STR1="Hello"

STR2="Hello"

if [ ${STR1} = ${STR2} ]

then

echo "Strings match"

else

echo "Strings don't match"

fi

P11: A simple script to show usage of string compare operator -z and -n.

STR1="Hello"

STR2="Hello"

if [ -z "${STR1}" ]

then

echo "String1 is empty"

else

echo "String1 is NOT empty"

fi

echo ":$STR:"

if [ -n "${STR2}" ]

then

echo "String2 is NOT empty"

else

echo "String2 is empty"

fi
P12: A simple script to show usage of logical operators.

echo -n "Enter a NUM: "

read NUM

# Check whether a number is between 10 and 20 (Using AND -a operator)

if [ $NUM -ge 10 -a $NUM -le 20 ]

then

echo "$NUM is between 10 and 20"

else

echo "$NUM is NOT between 10 and 20"

fi

echo -n "Enter another NUM: "

read NUM

# Check whether a number is between 10 and 20 (Using OR -o operator)

if [ $NUM -lt 10 -o $NUM -gt 20 ]

then

echo "$NUM is NOT between 10 and 20"

else

echo "$NUM is between 10 and 20"

fi

P13: A simple script to show usage while loop.

COUNT=0

while [ $COUNT -lt 5 ]

do

echo "Loop count is ${COUNT}"


COUNT=$((COUNT + 1))

done

echo "Done"

P14: Sum of N natural numbers using while loop.

echo -n "Enter a number: "

read NUM

let SUM=0;

let I=1

while [ $I -le $NUM ]

do

SUM=`expr $SUM + $I`

I=$((${I} + 1))

done

echo "The sum of the first $NUM numbers is: $SUM"

P15: A simple script to demonstrate for loop.

COUNT=0

for i in 0 1 2 3 4

do

echo "Loop count is ${COUNT}"

COUNT=$((COUNT + 1))

done

You might also like