Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Tasks: You are required to write a Bourne Again Shell Script (bash) to manage a

ID: 3589136 • Letter: T

Question

Tasks: You are required to write a Bourne Again Shell Script (bash) to manage a menu driven program.

When executed the user should be presented with a menu with 4 (four) options:

1. Print the following user information: the home directory, path, userid , login shell together with the current date and time.

2. Ask a user a range and it displays eight random number between the range. [Range difference must be 100. Example Enter a range: 200 300

3. Print out the highest and the lowest numbers of the generated eight random numbers

4. Exit the program Requirements:

Your script must allow one of these options to be chosen at a time.

The menu must be presented over and over again, until the fourth option (exit) is chosen. An appropriate error message must be displayed if an invalid option is chosen.

Option one, your script should list the specified environment variables. The current output of the date command, together with the userid and the full name of the user also needs to appear in this file, before the environment variables.

Option two should prompt the user a range and displays eight (8) random numbers.

Option three should list the highest and lowest number of the generated random numbers.

Option four should cause a graceful exit from the menu and the whole program

Explanation / Answer

while true
do
echo
echo "============================================================================================================="
echo "1. Print Home Directory,path, userid , login shell together with the current date and time."
echo "2. Ask a user a range and it displays eight random number between the range. [Range difference must be 100.]"
echo "3. Print out the highest and the lowest numbers of the generated eight random numbers."
echo "4. Exit the program Requirements"
echo "============================================================================================================="
echo "Enter choice [ 1 - 4 ] : "
read ch
case $ch in
   1)
       echo "Home Directory : $HOME"       # HOME is pre-defined shell variable which will prints the home directory of the user
                                           # $ is symbol, which gives the value of the variable name
       echo "Path           : $PATH"       # PATH is also a pre-deinfed shell variable which will prints the set of directories separted by the colon
       echo "User Id        : $UID "       # UID is pre-defined shell variable which will prints the numeric value of an unix user loign id;;
       echo "Login Shel     : $SHELL"      # SHELL is pre-defined shell variable which will prints the default shell of the user
       echo "Current Date&Time : `date`"   # date is a command, which displays both current system date and time
                                           # the command date is enclosed with in a back quotations - it is a command substituion feature
       ;;
   2) echo "Enter starting range : "
      read m
      echo "Enter ending range : "
      read n
      dif=$(($n-$m))
      if [ $dif -ne 100 ] ; then
        echo "$n - $m difference is $dif"
        echo "the difference should be Hundered"
      else
        dif=$(($dif + 1))
        RANDOM=$$                  # $$ is a pre-defined shell variable, which will gives process id of the current shell script
        min=$(($(($RANDOM%$dif))+$m)) # the random number is mod with 100, so, we will get two digit number.
                                      # This two digit number is added to the starting range of number,which is obviously
                                      # between starting range and ending range.
        max=$min
        echo "Eight Random Integers between $n and $m are ..."
        echo $min
        for i in `seq 7`
         do
           R=$(($(($RANDOM%$dif))+$m))
           echo $R
           if [ $min -ge $R ] ; then
              min=$R
           fi
           if [ $max -le $R ] ; then
              max=$R
           fi
         done
      fi
      ;;
   3)
      echo "The minimum number from generated 8 random numbers : $min"
      echo "The maximum number from generated 8 random numbers : $max"
      ;;
   4) exit 1
      ;;
   *) echo "Invalid choice..."
esac
echo "============================================================================================================="
done   # End of while loop

<<'COMMENT'
lenovo@lenovo-Vbox:~/chegg$ chmod 777 menu.sh
lenovo@lenovo-Vbox:~/chegg$ ./menu.sh

=============================================================================================================
1. Print Home Directory,path, userid , login shell together with the current date and time.
2. Ask a user a range and it displays eight random number between the range. [Range difference must be 100.]
3. Print out the highest and the lowest numbers of the generated eight random numbers.
4. Exit the program Requirements
=============================================================================================================
Enter choice [ 1 - 4 ] :
1
Home Directory : /home/lenovo
Path           : /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
User Id        : 1000
Login Shel     : /bin/bash
Current Date&Time : Mon Oct 9 22:29:13 IST 2017
=============================================================================================================

=============================================================================================================
1. Print Home Directory,path, userid , login shell together with the current date and time.
2. Ask a user a range and it displays eight random number between the range. [Range difference must be 100.]
3. Print out the highest and the lowest numbers of the generated eight random numbers.
4. Exit the program Requirements
=============================================================================================================
Enter choice [ 1 - 4 ] :
2
Enter starting range :
100
Enter ending range :
60
60 - 100 difference is -40
the difference should be Hundered
=============================================================================================================

=============================================================================================================
1. Print Home Directory,path, userid , login shell together with the current date and time.
2. Ask a user a range and it displays eight random number between the range. [Range difference must be 100.]
3. Print out the highest and the lowest numbers of the generated eight random numbers.
4. Exit the program Requirements
=============================================================================================================
Enter choice [ 1 - 4 ] :
2
Enter starting range :
100
Enter ending range :
300
300 - 100 difference is 200
the difference should be Hundered
=============================================================================================================

=============================================================================================================
1. Print Home Directory,path, userid , login shell together with the current date and time.
2. Ask a user a range and it displays eight random number between the range. [Range difference must be 100.]
3. Print out the highest and the lowest numbers of the generated eight random numbers.
4. Exit the program Requirements
=============================================================================================================
Enter choice [ 1 - 4 ] :
2
Enter starting range :
200
Enter ending range :
300
Eight Random Integers between 300 and 200 are ...
256
270
251
262
290
214
258
278
=============================================================================================================

=============================================================================================================
1. Print Home Directory,path, userid , login shell together with the current date and time.
2. Ask a user a range and it displays eight random number between the range. [Range difference must be 100.]
3. Print out the highest and the lowest numbers of the generated eight random numbers.
4. Exit the program Requirements
=============================================================================================================
Enter choice [ 1 - 4 ] :
3
The minimum number from generated 8 random numbers : 214
The maximum number from generated 8 random numbers : 290
=============================================================================================================

=============================================================================================================
1. Print Home Directory,path, userid , login shell together with the current date and time.
2. Ask a user a range and it displays eight random number between the range. [Range difference must be 100.]
3. Print out the highest and the lowest numbers of the generated eight random numbers.
4. Exit the program Requirements
=============================================================================================================
Enter choice [ 1 - 4 ] :
6
Invalid choice...
=============================================================================================================

=============================================================================================================
1. Print Home Directory,path, userid , login shell together with the current date and time.
2. Ask a user a range and it displays eight random number between the range. [Range difference must be 100.]
3. Print out the highest and the lowest numbers of the generated eight random numbers.
4. Exit the program Requirements
=============================================================================================================
Enter choice [ 1 - 4 ] :
4
lenovo@lenovo-Vbox:~/chegg$
COMMENT

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote