What you should do: 1- Write a bash script that checks if options a, b, c, and d
ID: 3811377 • Letter: W
Question
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_ Your Name. 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_ YourName. 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-n 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 triggeredExplanation / 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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.