When the script below is run as ./myscript.sh -d 10,6 what will the output to terminal be?#!/bin/bashOPTERR="Invalid option"declare -a argsIFS=','if [[ $# -gt 0 ]]; thenwhile getopts "a:s:d:m:" opt; do for arg in $OPTARG; do args+=("$arg") done case $opt in a) echo $( expr ${args[0]} + ${args[1]} );; s) echo $( expr ${args[0]} - ${args[1]} );; d) echo $( expr ${args[0]} / ${args[1]} );; m) echo $( expr ${args[0]} \* ${args[1]} );; *) echo "$OPTERR" && exit 1;; esac donefiIFS=" "exit 0
Question
When the script below is run as ./myscript.sh -d 10,6 what will the output to terminal be?#!/bin/bashOPTERR="Invalid option"declare -a argsIFS=','if [[ # -gt 0 ]]; thenwhile getopts "a:s:d:m:" opt; do for arg in OPTARG; do args+=("opt in a) echo {args[0]} + ( expr {args[1]} );; d) echo {args[0]} / ( expr {args[1]} );; *) echo "$OPTERR" && exit 1;; esac donefiIFS=" "exit 0
Solution
The script you provided is a bash shell script. When you run this script with the command ./myscript.sh -d 10,6, it will perform a division operation because of the -d option. The numbers to be divided are 10 and 6.
Here's a step-by-step breakdown of what the script does:
-
The script starts with
#!/bin/bash, which tells the system that this is a bash shell script. -
OPTERR="Invalid option"sets a variableOPTERRwith the value "Invalid option". -
declare -a argsdeclares an array namedargs. -
IFS=','sets the internal field separator to a comma. This is used for word splitting and to split lines into fields. -
The
if [[ $# -gt 0 ]]; thenchecks if the script was run with any arguments. If it was, it enters the loop. -
while getopts "a:s:d:m:" opt; dostarts a loop that goes through all the options provided when the script was run. -
The
for arg in $OPTARG; do args+=("$arg") doneloop adds each argument to theargsarray. -
The
case $opt instatement checks which option was provided and performs the corresponding operation. -
In your case,
-dwas provided, so it performs a division operation with theexprcommand on the first two elements of theargsarray, which are10and6. -
The result of the division operation is then echoed (printed) to the terminal.
-
If an invalid option is provided, it prints "Invalid option" and exits with a status of 1.
-
IFS=" "resets the internal field separator to a space. -
exit 0exits the script with a status of 0, indicating that it ran successfully.
So, when you run ./myscript.sh -d 10,6, the output will be the result of the division of 10 by 6. Since expr performs integer division, the output will be 1.
Similar Questions
If I run the command ./scriptname.sh parameters 5 5 with the code below, what will the output to terminal be?Note: You response must be precisely what will be printed to the terminal to be marked correct.#!/bin/bashecho $(expr substr "$1" "$2" "$3")exit 0
When the script below is run as ./myscript.sh -d 10,6 what will the output to terminal be?#!/bin/bashOPTERR="Invalid option"declare -a argsIFS=','if [[ $# -gt 0 ]]; thenwhile getopts "a:s:d:m:" opt; do for arg in $OPTARG; do args+=("$arg") done case $opt in a) echo $( expr ${args[0]} + ${args[1]} );; s) echo $( expr ${args[0]} - ${args[1]} );; d) echo $( expr ${args[0]} / ${args[1]} );; m) echo $( expr ${args[0]} \* ${args[1]} );; *) echo "$OPTERR" && exit 1;; esac donefiIFS=" "exit 0
When the script below is run as ./myscript.sh repdat.txt, what will be output to the terminal?Note: Be precise in your answer. It must be exactly what would appear in the terminal#!/bin/bashcat $1 | grep -v "^[0-9]\{2\}%" | wc -lexit 0NOTE: The repdat.txt file contains the following lines:Breach data for the report was compiled in March and April 2020 by an independent research company.Key survey findings from respondents were as follows:94% said attack volume has increased in the last 12 months, the survey found.96% said their business has suffered a security breach in the last 12 months.The average organization said they experienced 2 breaches during that time, the survey found.88% said attacks have become more sophisticated.96% said they plan to increase cyber defence spending in the coming year.OS vulnerabilities and third-party application attacks were the leading cause of breachesCompanies said they are using an average of 7 different breach prevention technologies.
If I run the bash script below as ./myscript.sh -i what will be the output to terminal?#!/bin/bashOPTERR="Invalid option"if [[ $# -gt 0 ]]; then while getopts "zpa" opt; do case $opt in z) echo "Apple";; p) echo "Banana";; a) echo "Orange";; *) echo "$OPTERR" && exit 1;; esac donefiexit 0
QUESTION: When the code below is run, what will be output to the terminal?#!/bin/bashstr='Book Title 1: <em>Think and Grow Rich</em> by Napoleon Hill'echo $str | sed 's/.*>.∗<.*/\1/'exit 0Group of answer choices<em></em>hink and Grow Ricby Napoleon HillThink and Grow Rich
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.