Linux+ Bash Scripting and Automation Questions and Answers 1 — Questions and Answers
Question 1: A script has a variable defined as `FILENAME="report_final.docx"`. Which parameter expansion command will correctly remove the file extension, resulting in `report_final`?
- echo "${FILENAME#*.}"
- echo "${FILENAME%%.*}"
- echo "${FILENAME%.*}" (Correct answer)
- echo "${FILENAME/docx/}"
Correct answer: echo "${FILENAME%.*}"
The `${variable%.*}` syntax is a standard parameter expansion that removes the shortest matching suffix pattern. In this case, `.*` matches the last dot and any characters that follow, effectively stripping the file extension. `${%%.*}` would remove the longest match, which is useful for files with multiple dots (e.g., `archive.tar.gz`), and `${#*.}` removes a prefix, not a suffix.
Question 2: A system administrator wants a script to create a tar archive with the current date in `YYYY-MM-DD` format included in the filename. Which of the following commands correctly creates an archive named `archive-2026-03-18.tar.gz` (assuming the current date is March 18, 2026)?
- tar -czf archive-`date +%F`.tar.gz /var/log/
- tar -czf archive-(date +%F).tar.gz /var/log/
- tar -czf archive-$(date +%F).tar.gz /var/log/ (Correct answer)
- tar -czf "archive-date +%F.tar.gz" /var/log/
Correct answer: tar -czf archive-$(date +%F).tar.gz /var/log/
The `$(...)` syntax is the modern and preferred method for command substitution. It executes the command within the parentheses (`date +%F`) and substitutes its standard output into the command line. The `+%F` format specifier for the `date` command is a shorthand for `%Y-%m-%d`, producing the required date format.
Question 3: A developer needs a script to process all PNG images in the current directory. Which of the following Bash `for` loops correctly iterates through all files ending with a `.png` extension?
- for i in $(ls *.png); do echo "Found $i"; done
- for i in *.png; do echo "Found $i"; done (Correct answer)
- for file in `*.png`; do echo "Found $file"; done
- for i in *.png; echo "Found $i"; done
Correct answer: for i in *.png; do echo "Found $i"; done
The most robust and standard way to iterate over files is to use file globbing directly in the `for` loop statement (`for i in *.png`). The shell expands the `*.png` glob into a list of matching filenames, handling spaces and special characters correctly. Using command substitution with `ls` (`$(ls *.png)`) is discouraged as it can lead to issues with filenames containing whitespace.
Question 4: An automation script needs to check if the directory `/srv/backups` exists before attempting to write a file to it. Which conditional statement correctly performs this check?
- if [ -f "/srv/backups" ]; then echo "Exists"; fi
- if test "/srv/backups"; then echo "Exists"; fi
- if [ -d "/srv/backups" ]; then echo "Exists"; fi (Correct answer)
- if [ -e "/srv/backups" ] && [ ! -f "/srv/backups" ]; then echo "Exists"; fi
Correct answer: if [ -d "/srv/backups" ]; then echo "Exists"; fi
The `test` command, often written with square brackets `[ ]`, uses file test operators to check file attributes. The `-d` operator specifically tests if the given path exists and is a directory. The `-f` operator checks for a regular file, and `-e` checks for general existence (file, directory, socket, etc.).
Question 5: A system administrator needs to schedule a maintenance script at `/opt/scripts/sync.sh` to run at 1:15 AM every Monday. Which of the following `crontab` entries is correct?
- 15 1 * * 1 /opt/scripts/sync.sh (Correct answer)
- 1 15 * * mon /opt/scripts/sync.sh
- * 1 15 * 1 /opt/scripts/sync.sh
- 15 1 * * 7 /opt/scripts/sync.sh
Correct answer: 15 1 * * 1 /opt/scripts/sync.sh
The crontab format is `minute hour day-of-month month day-of-week command`. The values `15 1 * * 1` correctly specify the 15th minute of the 1st hour, on any day of the month and any month, but only on the 1st day of the week. In cron, both 1 and 'mon' represent Monday.
Question 6: Which of the following `sed` commands will find all occurrences of the string `api_key=SECRET` and replace them with `api_key=REDACTED` directly within the file `/var/log/app.log`?
- sed 's/api_key=SECRET/api_key=REDACTED/' /var/log/app.log
- sed -i 's/api_key=SECRET/api_key=REDACTED/g' /var/log/app.log (Correct answer)
- sed 's/api_key=SECRET/api_key=REDACTED/g' /var/log/app.log > /var/log/app.log
- sed -i 's/api_key=SECRET/api_key=REDACTED/' /var/log/app.log
Correct answer: sed -i 's/api_key=SECRET/api_key=REDACTED/g' /var/log/app.log
The `sed` command requires the `-i` flag to edit the file "in-place". The `s/find/replace/g` syntax specifies the substitution. The `g` flag at the end is crucial; it stands for "global" and ensures all occurrences on each line are replaced, not just the first one. Redirecting output to the input file (`> file`) would truncate the file before `sed` can read it.
A script has a variable defined as `FILENAME="report_final.docx"`.
Which parameter expansion command will correctly remove the file extension, resulting in `report_final`?