1. Write a script that generates two random numbers between 1 and 10 and if the
ID: 3592472 • Letter: 1
Question
1. Write a script that generates two random numbers between 1 and 10 and if the numbers match then prints “Match Found” else prints “Different Numbers”. You can use $RANDOM to generate the random numbers.
2. Write a script that takes one number between 1 and 10 from the user as an argument to the command and checks if a randomly generated number between 1 and 10 matches the user supplied number. The program first checks to see if only one argument has been provided by the user otherwise flags that an error.
$ your_script 5 #will tell you if the computer also generated the same number
$ your_script #will tell you that no args were supplied
You can use $# to check for the number of supplied args.
3. Write a script that checks to see if at least one argument has been given on the command line and tests whether the argument is the name of an ordinary file (not a directory or other type of file) in the working directory.
4. Write a script that uses case statement to do print Agreed, Disagreed, or Invalid Input when the user is asked to respond as Y or N on a question. Yes and No are also valid inputs and any combination of uppercase and lower case for all possible responses.
5. Write a script using if…the…elif to compare three words entered by the user and compare if any two match.
6. Write a script using the for loop construct that lists the names of the directory files in the working directory by looping through the files in the working directory and using test to determine which files are directory files.
7. Write a script using the while loop construct that generates 100 randomly generated numbers between 0 and 99 and writes them out in a user specified filename.
8.Repeat #7 with until loop.
Explanation / Answer
Below are the shell scripts for each question and the outputs as follows:
1)
Random_num.sh :-
------------------------
#!/bin/sh
a=$(((RANDOM % 10) + 1))
b=$(((RANDOM % 10) + 1))
echo $a
echo $b
if [ $a == $b ]
then
echo "Match found"
else
echo "Match not found"
fi
Output:-
------------
>sh Random_num.sh
5
5
Match found
2)
Random_num_2.sh :-
----------------------------
#!/bin/sh
a=$1
b=$(((RANDOM % 10) + 1))
if [ $# == 0 ]
then
echo "No Arguments are supplied"
elif [ $# != 1 ]
then
echo "Error: More than one arguments are passed"
else
if [ $a == $b ]
then
echo "Match found"
else
echo "Match not found"
fi
fi
Output:-
---------------
>sh Random_num_2.sh 4
Match not found
>sh Random_num_2.sh
No Arguments are supplied
>sh Random_num_2.sh 4 7 8
Error: More than one arguments are passed
3)
found_file.sh
---------------------
#!/bin/sh
a=$1
echo $a
if [ $# == 0 ]
then
echo "No Arguments are supplied"
else
if [ -f $a ]
then
echo "The given argument is a name of file in current directory"
else
echo "The given argument is not a name of file in current directory"
fi
fi
Output:-
-------------
> sh found_file.sh nrk.txt
The given argument is a name of file in current directory
> sh found_file.sh
No Arguments are supplied
4)
option.sh :-
-------------
#!/bin/sh
echo "enter yes or no"
read a
shopt -s nocasematch
case "$a" in
"yes") echo "Agreed"
;;
"y") echo "Agreed"
;;
"no") echo "Disagreed"
;;
"n") echo "Disagreed"
;;
*) echo "Invalid"
;;
esac
Output:-
-------------
>sh option.sh
enter yes or no
yes
Agreed
>sh option.sh
enter yes or no
N
Disagreed
5)
MatchThreeWords.sh
-------------------
echo "enter 1st word"
read a
echo "enter 2nd word"
read b
echo "enter 3rd word"
read c
if [ $a == $b ]
then
echo "1st and 2nd words are matched"
elif [ $b == $c ]
then
echo "2nd and 3rd words are matched"
elif [ $c == $a ]
then
echo "1st and 3rd words are matched"
else
echo "none are matched"
fi
Output:-
-------------
>sh MatchThreeWords.sh
enter 1st word
tej
enter 2nd word
ram
enter 3rd word
tej
1st and 3rd words are matched
6)
list_files.sh
---------------
echo "list of files"
for f in *
do
echo $f
done
Output:-
-------------
list of files
nrk.txt
list.sh
7)
Rand_num_to_txt.sh:-
----------------------
a=1
while [ $a -lt 100 ]
do
echo $(((RANDOM % 100) +1 ))" " >> nrk.txt
echo "file created"
a=`expr $a + 1`
done
Output:-
----------------
>sh Rand_num_to_txt.sh
file created
8)
Rand_num_to_txt2.sh:-
----------------------
a=1
until [ $a -gt 100 ]
do
echo $(((RANDOM % 100) +1 )) >> nrk2.txt
echo "file created"
a=`expr $a + 1`
done
Output:-
----------------
>sh Rand_num_to_txt2.sh
file created
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.