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

Use gets opts command To complete this assignment This lab is asking you to use

ID: 3813250 • Letter: U

Question


Use gets opts command To complete this assignment This lab is asking you to use getopts commend/program to process command-line options passed to your script. What you should do: 1- write a bash script that checks if options a, b, c, and d where used by the user when your script was invoked (called), where option a and d must take arguments 2- Your script should generate (handle) error messages 3-Mour script should display to the screen (terminal) information which, any, option was triggered or feedback to the user (me) that the user tried to invoke invalid (unexpected) option or an error message if the right option was used by the user (me) but it expects argument that was not provided Some examples on how your script should behave: If I run your script by typing Lab 9 Your Name .sh-b it should display: option -b- was triggered. If run your script by typing: Lab 9 Your Name .sh -a a argument it should display: Option -a- was triggered with argument: a argument, If I run your script by typing: LLab 9 Your Name .sh -d it should display: Option -d- expects its argument that was not provided. If I run your script by typing: Lab 9 YourName sh e it should display: Invalid option -e-. The valid options are -a-, -b-, -o-, and -e-, where options a- and -d- must be followed by an argument. If i run your script by typing LLab 9 Your Name .sh it should display: No options were triggered

Explanation / Answer

Following code represents all the requirements specified in the question:

#!/bin/bash

while getopts ":a:d:" opt;do
   case $opt in
       a)   
           echo "-a was triggered, Parameter: $OPTARG">&2
           ;;
       d)
           echo "-d was triggered, Parameter: $OPTARG">&2
           ;;
       ?)
           echo "Inva;id option. The valid option are -a-,-b-,-c-, and -d-,where options -a- and -d- must be followed by an argument: -$OPTARG">&2
           exit 1
           ;;
       :)
           echo "Option -$OPTARG requires an arguement that was not provided">&2
           exit 1
           ;;
       esac
   done

while getopts ":b:c" opt;do
   case $opt in
       b)   
           echo "-b was triggered">&2
           ;;
       c)
           echo "-d was triggered, Parameter: $OPTARG">&2
           ;;
       ?)
           echo "Invavid option. The valid option are -a-,-b-,-c-, and -d-,where options -a- and -d- must be followed by an argument: -$OPTARG">&2
           exit 1
           ;;
      
       esac
   done