Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Logging framework in Bash Scripting - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to write a log message to a file.

Bash Scripting
echo "Starting process..." >> [1]
Drag options to blanks, or click blank then click option'
A/var/log/process.log
B/etc/passwd
C/home/user/document.txt
D/tmp/randomfile
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-log file path for logging.
Overwriting the log file instead of appending.
2fill in blank
medium

Complete the code to log an error message with a timestamp.

Bash Scripting
echo "$(date) ERROR: Disk space low" >> [1]
Drag options to blanks, or click blank then click option'
A/var/log/syslog
B/var/log/error.log
C/var/log/auth.log
D/var/log/wtmp
Attempts:
3 left
💡 Hint
Common Mistakes
Logging errors to unrelated log files.
Not including a timestamp in the log message.
3fill in blank
hard

Fix the error in the code to correctly log a warning message.

Bash Scripting
echo "WARNING: Low memory" [1] /var/log/warnings.log
Drag options to blanks, or click blank then click option'
A|
B>
C>>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using single > which overwrites the log file.
Using pipe or input redirection symbols incorrectly.
4fill in blank
hard

Fill both blanks to create a function that logs messages with levels.

Bash Scripting
log_message() {
  echo "$(date) [1]: $1" [2] /var/log/app.log
}
Drag options to blanks, or click blank then click option'
AINFO
B>>
C>
DDEBUG
Attempts:
3 left
💡 Hint
Common Mistakes
Using > which overwrites the log file.
Using an incorrect log level or missing it.
5fill in blank
hard

Fill all three blanks to create a log entry with dynamic level and message.

Bash Scripting
log() {
  level=[1]
  message=[2]
  echo "$(date) $level: $message" [3] /var/log/custom.log
}
Drag options to blanks, or click blank then click option'
A"$1"
B"$2"
C>>
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using > which overwrites the log file.
Not quoting the parameters properly.

Practice

(1/5)
1. What is the main purpose of a logging framework in a bash script?
easy
A. To record important messages during script execution
B. To speed up the script execution
C. To change file permissions automatically
D. To delete temporary files after running

Solution

  1. Step 1: Understand logging role

    Logging records messages that help track what the script does while running.
  2. Step 2: Compare options

    Only To record important messages during script execution describes recording messages; others describe unrelated tasks.
  3. Final Answer:

    To record important messages during script execution -> Option A
  4. Quick Check:

    Logging = record messages [OK]
Hint: Logging means saving messages about script actions [OK]
Common Mistakes:
  • Confusing logging with script speed optimization
  • Thinking logging changes file permissions
  • Assuming logging deletes files automatically
2. Which of the following is the correct syntax to define a simple logging function in bash that prints a message with a timestamp?
easy
A. log() echo "$(date) - $1"
B. log = echo "$(date) - $1"
C. function log echo "$(date) - $1"
D. log() { echo "$(date) - $1"; }

Solution

  1. Step 1: Review bash function syntax

    Functions use the format: name() { commands; }.
  2. Step 2: Check each option

    log() { echo "$(date) - $1"; } correctly defines a function with braces and echo command. Others have syntax errors.
  3. Final Answer:

    log() { echo "$(date) - $1"; } -> Option D
  4. Quick Check:

    Function syntax with braces = log() { echo "$(date) - $1"; } [OK]
Hint: Bash functions need () and braces { } [OK]
Common Mistakes:
  • Using spaces around = in function definition
  • Omitting braces {} for function body
  • Writing function without parentheses ()
3. Given the bash function:
log() { echo "$(date +'%Y-%m-%d %H:%M:%S') [$1] $2"; }

What is the output of log INFO "Script started"?
medium
A. Script started [INFO]
B. [INFO] Script started
C. 2024-06-01 12:00:00 [INFO] Script started
D. INFO Script started

Solution

  1. Step 1: Understand function parameters

    $1 is the log level (INFO), $2 is the message (Script started).
  2. Step 2: Understand echo format

    It prints date, then [$1], then $2, so output is timestamp + [INFO] + message.
  3. Final Answer:

    2024-06-01 12:00:00 [INFO] Script started -> Option C
  4. Quick Check:

    Format = timestamp + [level] + message [OK]
Hint: First param is level, second is message in log() [OK]
Common Mistakes:
  • Mixing order of parameters
  • Ignoring timestamp format
  • Missing brackets around log level
4. You have this logging function:
log() { echo "$(date +'%Y-%m-%d %H:%M:%S') [$1] $2"; }

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

  1. Step 1: Check function usage

    Function expects two arguments: level and message.
  2. Step 2: Analyze output

    Only level is given, so message ($2) is empty, causing missing text after [ERROR].
  3. Final Answer:

    Missing second argument for the message -> Option A
  4. 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

  1. Step 1: Understand requirement

    Logs should be saved to a file and include level only if WARNING or ERROR.
  2. 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.
  3. Final Answer:

    log() { if [[ "$1" == "WARNING" || "$1" == "ERROR" ]]; then echo "$(date) [$1] $2" >> script.log; fi; } -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard