Quiz: Shell Scripting and Automation
Test your understanding of shell scripts, automation, and cron jobs.
1. What is a shebang and why is it important?
- A shell command for making noise
- The first line (#!/bin/bash) that tells the system which interpreter to use
- A debugging tool
- A type of variable
Show Answer
The correct answer is B. The shebang (like #!/bin/bash) is the first line of a script that tells the system which interpreter should execute it. Without it, the system may not know how to run your script properly.
Concept Tested: Script Shebang
See: Chapter 13 - Shebang
2. What must you do before running a shell script you created?
- Compile it
- Make it executable with chmod +x
- Convert it to binary
- Register it with the system
Show Answer
The correct answer is B. Shell scripts are text files and need execute permission to run directly. Use chmod +x script.sh to make it executable, then run with ./script.sh.
Concept Tested: Script Permissions
3. How do you access the first argument passed to a script?
- $0
- $1
- $first
- $arg1
Show Answer
The correct answer is B. In bash scripts, $1 is the first argument, $2 is the second, and so on. $0 is the script name itself, and $# is the count of arguments. $@ represents all arguments.
Concept Tested: Script Arguments
4. What is cron used for?
- Compressing files
- Scheduling commands to run automatically at specified times
- Creating users
- Managing network connections
Show Answer
The correct answer is B. Cron is a time-based job scheduler that runs commands automatically at specified intervals. It's perfect for backups, log rotation, system maintenance, and any recurring tasks.
Concept Tested: Cron Daemon
See: Chapter 13 - Cron
5. In cron syntax, what does 0 2 * * * mean?
- Every 2 minutes
- At 2:00 AM every day
- Every 2 hours
- On the 2nd of every month
Show Answer
The correct answer is B. Cron fields are: minute, hour, day-of-month, month, day-of-week. So 0 2 * * * means minute 0, hour 2, every day of month, every month, every day of week = 2:00 AM daily.
Concept Tested: Cron Syntax, Cron Schedule Fields
6. What command edits your personal crontab file?
- nano /etc/cron
- crontab -e
- vi /var/cron
- edit crontab
Show Answer
The correct answer is B. crontab -e opens your personal crontab file for editing. Use crontab -l to list current cron jobs, and crontab -r to remove all your cron jobs.
Concept Tested: Crontab Command
7. What does $? contain in a shell script?
- The current process ID
- The exit status of the last command
- The number of arguments
- The script filename
Show Answer
The correct answer is B. $? contains the exit status (return code) of the most recently executed command. A value of 0 typically means success, while non-zero values indicate various errors.
Concept Tested: Script Exit Codes
8. What is the purpose of set -e at the beginning of a script?
- Sets the editor
- Exits the script immediately if any command fails
- Enables echoing of commands
- Sets environment variables
Show Answer
The correct answer is B. set -e makes the script exit immediately if any command returns a non-zero exit status. This helps catch errors early rather than continuing with a broken state.
Concept Tested: Script Best Practices
9. What does the at command do?
- Displays file attributes
- Schedules a one-time command for future execution
- Shows the current time
- Lists all files at a location
Show Answer
The correct answer is B. Unlike cron which runs recurring jobs, at schedules a one-time command for a specific future time. For example, at 3pm tomorrow lets you schedule a job for 3 PM tomorrow.
Concept Tested: At Command
10. What is the difference between * and */5 in a cron schedule?
- They're identical
*means every time unit;*/5means every 5th time unit*/5runs 5 times longer*is invalid syntax
Show Answer
The correct answer is B. In cron, * means "every" (every minute, hour, etc.), while */5 means "every 5th." So in the minute field, * runs every minute while */5 runs at minutes 0, 5, 10, 15, 20, etc.
Concept Tested: Cron Syntax