0
0
Bash Scriptingscripting~20 mins

Log rotation script in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Log Rotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this log rotation snippet?
Consider this bash snippet that rotates a log file by renaming it with a timestamp. What will be the output if the current date is 2024-06-15 and the log file exists?
Bash Scripting
LOGFILE="app.log"
BACKUP="app.log.$(date +%F)"
if [ -f "$LOGFILE" ]; then
  mv "$LOGFILE" "$BACKUP"
  echo "Rotated $LOGFILE to $BACKUP"
else
  echo "No log file to rotate"
fi
ANo log file to rotate
BRotated app.log to app.log.2024-06-15
CRotated app.log to app.log.$(date +%F)
DError: date command not found
Attempts:
2 left
💡 Hint
Check how the date command is used inside the variable BACKUP.
🔧 Debug
intermediate
2:00remaining
Why does this log rotation script fail to rotate logs?
This script is intended to rotate a log file by compressing the old log and creating a new empty log. Why does it fail to compress the old log?
Bash Scripting
LOGFILE="server.log"
if [ -f "$LOGFILE" ]; then
  gzip $LOGFILE
  > $LOGFILE
  echo "Log rotated and compressed"
else
  echo "No log to rotate"
fi
AThe if condition uses -f which does not detect regular files.
BThe > $LOGFILE line is invalid syntax and causes an error.
CThe gzip command removes the original file, so > $LOGFILE creates a new empty file with the same name, but the script expects the compressed file to exist.
DThe script does not check if gzip is installed, so it fails silently.
Attempts:
2 left
💡 Hint
Think about what gzip does to the original file after compression.
🚀 Application
advanced
3:00remaining
Which script correctly rotates logs keeping only last 5 backups?
You want a bash script that renames the current log file by appending the date, compresses it, and deletes backups older than 5 days. Which script does this correctly?
A
LOGFILE="app.log"
BACKUP="app.log.$(date +%F)"
if [ -f "$LOGFILE" ]; then
  mv "$LOGFILE" "$BACKUP"
  gzip "$BACKUP"
  find . -name 'app.log.*.gz' -mtime +5 -exec rm -f {} \;
  touch "$LOGFILE"
fi
B
LOGFILE="app.log"
BACKUP="app.log.$(date +%F)"
if [ -f "$LOGFILE" ]; then
  mv "$LOGFILE" "$BACKUP"
  gzip "$BACKUP"
  find . -name 'app.log.*.gz' -mtime +5 -delete
  touch "$LOGFILE"
fi
C
LOGFILE="app.log"
BACKUP="app.log.$(date +%F)"
if [ -f "$LOGFILE" ]; then
  cp "$LOGFILE" "$BACKUP"
  gzip "$LOGFILE"
  find . -name 'app.log.*.gz' -mtime +5 -exec rm {} \;
  > "$LOGFILE"
fi
D
LOGFILE="app.log"
BACKUP="app.log.$(date +%F)"
if [ -f "$LOGFILE" ]; then
  mv "$LOGFILE" "$BACKUP"
  gzip "$LOGFILE"
  find . -name 'app.log.*.gz' -mtime +5 -delete
  touch "$LOGFILE"
fi
Attempts:
2 left
💡 Hint
Check which file is compressed and how old backups are deleted.
📝 Syntax
advanced
2:00remaining
Which option has a syntax error in this log rotation script?
Identify the option that contains a syntax error preventing the script from running.
Bash Scripting
LOGFILE="access.log"
BACKUP="access.log.$(date +%F)"
if [ -f "$LOGFILE" ]; then
  mv "$LOGFILE" "$BACKUP"
  gzip "$BACKUP"
  touch "$LOGFILE"
fi
A
f [ -f "$LOGFILE" ]; then
  mv "$LOGFILE" "$BACKUP"
  gzip "$BACKUP"
  touch "$LOGFILE"
fi
B
if [ -f "$LOGFILE" ]; then
  mv "$LOGFILE" "$BACKUP"
  gzip "$BACKUP"
  touch "$LOGFILE"
fi
C
if
"ELIFGOL$" hcuot  
"PUKCAB$" pizg  
"PUKCAB$" "ELIFGOL$" vm  
neht ;] "ELIFGOL$" f- [ fi
D
if [ -f "$LOGFILE" ] then
  mv "$LOGFILE" "$BACKUP"
  gzip "$BACKUP"
  touch "$LOGFILE"
fi
Attempts:
2 left
💡 Hint
Check the syntax of the if statement carefully.
🧠 Conceptual
expert
1:30remaining
What is the main reason to compress rotated logs in automation?
In automated log rotation scripts, why is compressing old log files important?
ATo reduce disk space usage by storing logs in a smaller size
BTo make logs easier to read by humans
CTo prevent logs from being deleted accidentally
DTo speed up the creation of new log files
Attempts:
2 left
💡 Hint
Think about storage and resource management.