0% found this document useful (0 votes)
18 views2 pages

Lab Problems #1

Uploaded by

yorgodaoud360
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)
18 views2 pages

Lab Problems #1

Uploaded by

yorgodaoud360
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/ 2

Assignment#1

Writing shell scripts


1) Write a shell script that checks to see if your HOME environment variable is not
empty. If empty, then exit the program. If it is not empty check to see if it contains
“/home/CSIS221”. Whether yes or no display suitable messages.

#!/bin/bash if [ -z "${HOME}" ] then echo


"empty" else result=`echo $HOME | grep
"/home/CSIS221"` if [ $? -eq 0 ] then
echo "Result found in the path /home/CSIS221"
else
echo "Result not found"
fi
fi

2) Write a shell script name (compar_num.sh) that accepts 3 numbers (num1, num2,
num3) passed as arguments ($ compar_num.sh 3 4 5).
• Accept these numbers in your script as positional parameters.
• Compare these numbers and display the largest one.

#!/bin/bash
if [ $# -ne 3 ] then
echo “Number of required arguments was not met”
elif [ $1 -gt $2 ] && [ $1 -gt $3]
then
echo $1
elif [ $2 -gt $1 ] && [ $2 -gt $3]
then
echo $2
else
echo $3
fi

3) Write a program that asks the user to enter his lucky number.
If the number he entered is equal to 111, he will get the 1st prize.
If the number he entered is equal to 112, he will get the 2nd prize.
If the number he entered is equal to 123, he will get the 3rd prize.
If none of the above, this message should be displayed: “Sorry, try for the next time”

#!/bin/bash
echo "Enter your lucky number"
read n if [[( $n == 111 )]]
then
echo "You got 1st prize"
elif [[ ( $n == 112 )]]
then
echo "You got 2nd prize"
elif [[($n == 123 )]] then
echo "You got 3rd prize"
else
echo "Sorry, try for the next time" fi
4) Make directory by checking existence:
‘-d’ option is used to test a particular directory is exist or not. Write
a script to create a directory by checking existence.

#!/bin/bash
echo "Enter directory name"
read ndir if [ -d "$ndir"
] then
echo "Directory exist"
else `mkdir $ndir`
echo "Directory created"
fi

6) Write a shell script that checks to see if your PATH environment variable contains
/usr/local/bin” as one of directories to search, and displays a suitable message.

#!/bin/bash
result=`echo $PATH | grep "/usr/local/bin"`
if [ $? -eq 0 ] then
echo "Result found in the path /usr/local/bin" else
echo "Result not found in the path" fi

7) Write a program that prompts the user to enter three integers and display the integers
in decreasing order.

#!/bin/bash echo
"Enter Num1" read
num1 echo "Enter
Num2" read num2
echo "Enter Num3"
read num3
echo -e “$num1\$num2\$num3” | sort -rn

You might also like