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

This lab is asking you to use getopts commend/program to process command-line op

ID: 3714181 • Letter: T

Question

This lab is asking you to use getopts commend/program to process command-line options passed to your script. Make sure to understand slides 66-69 (last three pages of the PDF document) of 03 -- Bash Shell Programming materials (which you can find in Blackboard’s Course Content) and/or related textbook’s chapters before starting.

What you should do:

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

Your script should generate (handle) error messages

Your script should display to the screen (terminal) information which, if any, option was triggered or feedback to the user (me) that the user (I) 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

Name your script Lab_8_YourName.sh and submit it uncompressed via Blackboard. If you scripts works only partially, let me know it by providing appropriate information in the notes for your submission. The deadline for this lab is before our next class meeting.

Hint, your script should include a very similar (but extended) code from the last slide of 03 -- Bash Shell Programming.

Some examples on how your script should behave:

If I run your script by typing:
                 ./Lab_8_YourName.sh -b
it should display:
                 Option -b- was triggered.

If I run your script by typing:
                 ./Lab_8_YourName.sh -a a_argument
it should display:
                 Option -a- was triggered with argument: a_argument.

If I run your script by typing:
                 ./Lab_8_YourName.sh -d
it should display:
                 Option -d- expects its argument that was not provided.

If I run your script by typing:
                 ./Lab_8_YourName.sh -e
it should display:
                 Invalid option -e-. The valid options are -a-, -b-, -c-, and -d-, where
                 options -a- and -d- must be followed by an argument.

If I run your script by typing:
                 ./Lab_8_YourName.sh
it should display:
                 No options were triggered.

Etc.

Extra credit (up to 90 points)

Let your script handle additional arguments after the (eventual) options.
For example:

If I run your script by typing:
                 ./Lab_9_YourName.sh -b arg_1 arg_2
it should display:
                 Option -b- was triggered and arguments arg_1 arg_2 was(were) passed to the script.

Note that it is not a trivial problem, as I can call your script with multiple options and argument(s) to the script, option with argument for that option and argument(s) to the script (e.g., arg_1 arg_2), or no option with many arguments to the script, etc.. More cases your script handles, more extra points you will receive but you can print all arguments to your script at one (e.g., by using $@ or $*).

Explanation / Answer

#!/bin/bash

# logic behind below code block is that

# first check for number of arguments, if it is zero then print error message.

# if argument 1 ($1) is not any one of -a,-b,-c,-d, then print error message.

# if argument 1 is -b or -c and there is no 2nd argument, then print success message.

# if argument 1 is -a or -d and argument 2 is empty, then print error message else

# print success message.

if [ $# -eq 0 ]; then

    echo "No options were triggered."

fi

if [ “$1” -eq “-a” || “$1” -eq “-b” || “$1” -eq “-c” || “$1” -eq “-d”]

then

    if [“$1” -eq “-b” || “$1” -eq “-c”]

then

if [“$2” -eq ""]   

echo “Option $1 was triggered.”

else

echo "Invalid option $1. The valid options are -a, -b, -c and -d, where”

            echo “-a and -d must be followed by an argument.”

        fi

elfi [“$1” -eq “-a” || “$1” -eq “-d”]

    then

        if [“$2” != "" ]   

echo “Option $1 was triggered with argument: $2.”

else

            echo “$1 expects its arguments that was not provided.”

        fi

    fi

   

else

    echo "Invalid option $1. The valid options are -a, -b, -c and -d, where”

    echo “-a and -d must be followed by an argument.”

fi

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