Explore essential Bash automation skills with this quiz covering cron syntax, shell scripting basics, and the use of pipelines. Enhance your knowledge of scheduling tasks, automating repetitive processes, and connecting commands efficiently in Unix-like systems.
Which cron schedule will execute a script every day at 2:30 AM?
Explanation: The correct answer is '30 2 * * *', which translates to 2:30 AM every day. The first field is minutes (30), followed by hour (2), day of month (*), month (*), and day of week (*). '2 30 * * *' would attempt to run at 30 AM on hour 2, which is invalid. '* * 2 30 *' is not a valid cron structure, and '0 30 2 * *' mixes up the fields. Understanding the field order is crucial for correct scheduling.
Before running a shell script directly by name, which command must you use to ensure it can be executed?
Explanation: Setting executable permission with 'chmod +x script.sh' is necessary to run the script directly. 'exec script.sh' is used to replace the shell with the script but only works if the script is already executable. 'make script.sh' is a build system command, not for permissions, and 'shmod script.sh' is a common typo or incorrect command. Proper permissions are essential for running scripts securely and effectively.
What is the main purpose of the shebang (e.g., #!/bin/bash) at the top of a shell script?
Explanation: The shebang line indicates the interpreter (such as /bin/bash) to use for running the script. It is not simply a comment, though marked with '#', and does not declare the filename or set environment variables. Properly specifying the interpreter ensures the script runs as intended across different environments.
Which command should you enter to edit your current user's scheduled cron tasks?
Explanation: 'crontab -e' opens the cron table editor for the current user, allowing task scheduling. 'cronedit' and 'edit-cron' are not standard system commands, and 'cron -c' is invalid syntax. Using the right command is essential for proper task management.
In Bash, which symbol is used to connect two commands so that the output of the first becomes the input of the second?
Explanation: The pipe symbol '|' connects commands, passing output from the left to input on the right. 'u0026' puts a command in background, ';' separates commands to run sequentially, and 'u003E' redirects output to a file. Piping enables powerful data manipulation within scripts and the shell.
In a cron schedule '0 6 * * 1', on which days will the job run?
Explanation: In cron, the fifth field selects the day of the week. '1' corresponds to Monday, so '0 6 * * 1' runs at 6:00 AM every Monday. 'Every Sunday' would require a '0' or '7' in that position. 'Every day at 6 AM' would be '0 6 * * *', and the first day of the month would have a '1' in the third field.
Which cron entry would run a backup script.sh every 10 minutes?
Explanation: '*/10 * * * *' means every 10 minutes past the hour. '* 10 * * *' runs at every minute, but only during the tenth hour. '10 * * * *' schedules only at minute 10 of any hour, not every 10 minutes. '*/1 10 * * *' would run every minute during hour 10. Frequency relies on proper field understanding.
Which line correctly assigns the value 'hello' to a variable named GREETING in a Bash script?
Explanation: Variable assignment in Bash is done using the '=' sign without spaces and quotes around strings, making 'GREETING='hello'' accurate. 'GREETING=hello' is valid, but if 'hello' contains spaces, it will break. 'GREETING:'hello'' and 'GREETING == 'hello'' are invalid Bash syntax for variable assignment.
Which command lists the cron jobs scheduled for the current user?
Explanation: 'crontab -l' displays the cron table for the current user. 'lscron', 'cronlist', and 'listcrontab' are not default commands on Unix-like systems. Correctly listing scheduled jobs is essential for cron management and troubleshooting.
Why might a Bash script use the command 'exit 1' at the end of its execution?
Explanation: An exit status of '1' indicates that a script completed with an error, which can be caught by other processes or automation tools. It does not log messages, pause execution, or cause automatic restarts. Using exit codes properly helps other scripts or users identify whether the script succeeded or failed.