- use gets opt to complete this assignment - also, part 4 , please This lab is a
ID: 3814336 • Letter: #
Question
- use gets opt to complete this assignment
- also, part 4 , please 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. 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 Some examples on how your script should behave: If I run your script by typing: /Lab 9 YourName sh -b it should display: Option -b- was triggered. If I run your script by typing /Lab 9 YourName. sh -a a argument it should display: Option -a-was triggered with argument: a argument. If I run your script by typing: /Lab 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-, -c-, and -e-, where options -a-and-d- must be followed by an argument. If I run your script by typing: /Lab 9 YourName. sh it should display: No options were triggered. Etc.
Explanation / Answer
#!/bin/bash
if [ $# -eq 0 ];
then
echo "No options were triggered."
exit 0
fi
while getopts ":a:bcd:" o; do
case "${o}" in
a)
a=${OPTARG}
printf "Option -a- was triggered with argument: $a"
;;
b)
printf "Option -b- was triggered"
;;
c)
printf "Option -c- was triggered"
;;
d)
d=${OPTARG}
printf "Option -d- was triggered with arguments: $d"
;;
*)
if [ "$@" == "-a" ] || [ "$@" == "-d" ]
then
echo "Option $@- expects its argument that was not provided."
else
echo "Invalid option $@-. The valid options are -a-, -b-, -c-, and -d-, where options -a- and -d- must be followed by an argument."
fi
exit 1
;;
esac
done
if [[ -n "${@:$OPTIND}" ]];
then
echo " and arguments ${@:$OPTIND} was(were) passed to the script."
else
echo "."
fi
Sample run
$ ./Lab_9_YourName.sh -b
Option -b- was triggered.
$ ./Lab_9_YourName.sh -a a_argument
Option -a- was triggered with argument: a_argument.
$ ./Lab_9_YourName.sh -d
Option -d- expects its argument that was not provided.
$ ./Lab_9_YourName.sh -e
Invalid option -e-. The valid options are -a-, -b-, -c-, and -d-, where options -a- and -d- must be followed by an argument.
$ ./Lab_9_YourName.sh
No options were triggered.
$ ./Lab_9_YourName.sh -b arg_1 arg_2
Option -b- was triggered and arguments arg_1 arg_2 was(were) passed to the script.
Please provide positive feedback if this solved your question.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.