But when you run log ERROR it prints: 2024-06-01 12:00:00 [ERROR] What is the problem?
medium
A. Missing second argument for the message
B. Date command syntax is wrong
C. Function name 'log' is reserved
D. Brackets around $1 cause error
Solution
Step 1: Check function usage
Function expects two arguments: level and message.
Step 2: Analyze output
Only level is given, so message ($2) is empty, causing missing text after [ERROR].
Final Answer:
Missing second argument for the message -> Option A
Quick Check:
Two args needed: level and message [OK]
Hint: Always provide both level and message to log() [OK]
Common Mistakes:
Passing only one argument to log()
Assuming date syntax is incorrect
Thinking function name conflicts cause this
5. You want to improve your bash logging function to write logs to a file and include log levels only if they are WARNING or ERROR. Which code snippet correctly implements this?
hard
A. log() { if [ "$1" = "INFO" ]; then echo "$(date) [$1] $2" >> script.log; fi; }
B. log() { if [[ "$1" == "WARNING" || "$1" == "ERROR" ]]; then echo "$(date) [$1] $2" >> script.log; fi; }
C. log() { echo "$(date) [$1] $2" >> script.log; }
D. log() { echo "$2" >> script.log; }
Solution
Step 1: Understand requirement
Logs should be saved to a file and include level only if WARNING or ERROR.
Step 2: Check each option
log() { if [[ "$1" == "WARNING" || "$1" == "ERROR" ]]; then echo "$(date) [$1] $2" >> script.log; fi; } writes to file and filters levels correctly. log() { echo "$(date) [$1] $2" >> script.log; } writes all levels. log() { if [ "$1" = "INFO" ]; then echo "$(date) [$1] $2" >> script.log; fi; } only logs INFO. log() { echo "$2" >> script.log; } logs message without level.
Final Answer:
log() { if [[ "$1" == "WARNING" || "$1" == "ERROR" ]]; then echo "$(date) [$1] $2" >> script.log; fi; } -> Option B
Quick Check:
Filter levels and append to file = log() { if [[ "$1" == "WARNING" || "$1" == "ERROR" ]]; then echo "$(date) [$1] $2" >> script.log; fi; } [OK]
Hint: Use if condition to filter levels before logging to file [OK]
Common Mistakes:
Logging all levels without filtering
Not appending logs to file
Logging message without level info
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.