Which of the following will output “cat”?
-
a. case “dog” in *) echo cat;; esac
- b. case “cat” in cat) echo$1 exit
- c. case cat dog rabbit; echo $1
- d. echo cat | case \$1 in cat) echo \$1 ;; esac
- e. case (cat dog rabbit) {echo $2}
In a case statement, the wildcard pattern *) matches anything. When case "dog" in *) echo cat;; esac runs, the input "dog" matches the * pattern, so it outputs 'cat'. This is a valid, though unusual, use of case.
To answer this question, let's go through each option to understand why it is correct or incorrect:
Option A) a. case "dog" in *) echo cat;; esac - This option is incorrect because the case statement is matching "dog" instead of "cat", so it will not output "cat".
Option B) b. case "cat" in cat) echo \$1 exit - This option is incorrect because there is no closing parenthesis after the echo statement. Additionally, the exit command is missing a semicolon before it.
Option C) c. case cat dog rabbit; echo \$1 - This option is incorrect because the case statement is missing the "in" keyword. Additionally, the echo statement is outside of the case statement.
Option D) d. echo cat | case \$1 in cat) echo \$1 ;; esac - This option is incorrect because the variable $1 is not defined before the case statement. Also, the pipe "|" operator is not being used correctly in this context.
Option E) e. case (cat dog rabbit) {echo $2} - This option is incorrect because the case statement is missing the "in" keyword. Additionally, the curly braces "{" and "}" are not used correctly in this context.
The correct answer is A) a. case "dog" in *) echo cat;; esac. This option is correct because the case statement matches "dog" and outputs "cat".
Therefore, the correct answer is A) a. case "dog" in *) echo cat;; esac.