1. Write a script that generates two random numbers between 1 and 10 and if the
ID: 3592473 • 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
It would have been better if you could specify the shell that you are using. I am writing bash shell scripts for answering these questions ..
1)
#!/bin/bash
###############################################
#Program generates two random numbers and checks
#whether they are equal or not.
##############################################
#Generate a number between 0 - 9
var1=$( echo "$RANDOM % 10" | bc)
#Since we want random numbers between 1 - 10. let's add "1" to the generated number
var1=$(echo "$var1 + 1" | bc)
#Similarly, generate the second number
var2=$( echo "$RANDOM % 10" | bc)
var2=$(echo "$var2 + 1" | bc)
#Check whether numbers are same or not
if [ "$var1" -eq "$var2" ]; then
echo "MATCH FOUND"
else
echo "DIFFERENT NUMBERS"
fi
2)
#!/bin/bash
#################################################
#Program generates a random number and checks
#whether it is equal to the user supplied number.
#################################################
#Check whether only one argument is passed to the program
if [ "$#" -eq 0 ]; then
echo "Error: No args supplied"
exit 1
elif [ "$#" -gt 1 ]; then
echo "Error: More than one argument supplied"
exit 1
fi
input="$1"
#Generate a number between 0 - 9
var1=$( echo "$RANDOM % 10" | bc)
#Since we want random numbers between 1 - 10. let's add "1" to the generated number
var1=$(echo "$var1 + 1" | bc)
if [ "$var1" -eq "$1" ]; then
echo "Computer generated the same number"
else
echo "Computer generated a different number : $var1"
fi
3)
#!/bin/bash
#################################################
#Program checks whether passed file is present in
#the current directory.
#################################################
#Check whether only one argument is passed to the program
if [ "$#" -eq 0 ]; then
echo "Error: No args supplied"
exit 1
fi
#All arguments are passed to the script a numbered from 1 to n
filename="$1"
if [ -f "./$filename" ]; then
echo "$filename present in the current directory"
else
echo "$filename not present in the current directory"
fi
4)
#!/bin/bash
#################################################
#Program validates user input on a question and
#displays result.
#################################################
#print a question
echo -n "Do you agree ? (y/n) "
read input
#"tr" command can be used to convert a string from upper to lower case and vice versa.
input=$(echo "$input" | tr '[:upper:]' '[:lower:]')
case $input in
"yes")
echo "Agreed"
;;
"y")
echo "Agreed"
;;
"no")
echo "Disagreed"
;;
"n")
echo "Disagreed"
;;
*)
echo "Invalid input"
;;
esac
5)
#!/bin/bash
#################################################
#Program compares three words entered by the user
#and prints if any two matches
#################################################
echo -n "Enter the first word : "
read word1
echo -n "Enter the Second word : "
read word2
echo -n "Enter the third word : "
read word3
if [ $word1 == $word2 ]; then
if [ $word1 == $word3 ]; then
echo "Word1, Word2 and Word3 are equal"
else
echo "Word1 is equal to Word2"
fi
elif [ $word1 == $word3 ]; then
echo "Word1 is equal to Word3"
elif [ $word2 == $word3 ]; then
echo "Word2 and Word3 are equal"
else
echo "None of the two words are equal"
fi
6)
#!/bin/bash
###########################################
#Program checks for directories inside the
#current directory.
##########################################
#list only the names of the directories
list=$(ls -1)
count=0;
#loop through the list
for file in $list
do
if [ -d $file ]; then
count=$( echo "$count + 1" | bc)
echo "$file is a directory"
fi
done
if [ "$count" -eq 0 ]; then
echo "No directories found"
fi
7)
#!/bin/bash
#################################################
#Program writes 100 randomly generated values
#in a user specified file.
################################################
echo -n "Enter the filename : "
read filename
count=1
while [ "$count" -le 100 ]
do
val=$(echo "$RANDOM % 100" | bc)
echo "$val" >>$filename
count=$(echo "$count + 1" | bc)
done
8)
#!/bin/bash
#################################################
#Program writes 100 randomly generated values
#in a user specified file using "until".
################################################
echo -n "Enter the filename : "
read filename
count=1
until [ "$count" -gt 100 ]
do
val=$(echo "$RANDOM % 100" | bc)
echo "$val" >>$filename
count=$(echo "$count + 1" | bc)
done
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.