Challenge your grasp of Bash automation with this quiz covering cron scheduling, shell script fundamentals, and command pipelines. Perfect for refining your skills in Bash scripting, cron jobs, and efficient command automation techniques.
Which field in a standard crontab entry represents the hour when specifying a schedule like '30 5 * * *'?
Explanation: The second field in a crontab entry specifies the hour. The first field is for minutes, the third for the day of the month, and the fourth for the month. Selecting the wrong field leads to incorrect scheduling. Always review cron’s field order when creating schedules.
Which line at the very top of a shell script designates the script’s interpreter, as in '#!/bin/bash'?
Explanation: The shebang line (e.g., #!/bin/bash) tells the system which interpreter to use for the script. 'Header comment' is a general comment, 'function declaration' defines functions, and 'package line' is not valid in shell scripting. Omitting or misusing the shebang can stop scripts from executing correctly.
What Bash command would you use to grant execute permissions to a shell script named 'backup.sh'?
Explanation: Using 'chmod +x backup.sh' makes the script executable by setting the execute bit. 'chown' changes ownership, not permissions. 'Source' is used to run scripts within the current shell, and 'run' is not a Bash command. Only the correct option sets the script as executable.
To safely edit your personal cron jobs, which command should you use in a Bash terminal?
Explanation: The correct command is 'crontab -e', which opens the current user’s crontab in an editor. 'cronedit' and 'editcron' are not standard commands. 'crontab --edit' seems logical but is not the actual option provided by cron. Using the correct syntax prevents command-not-found errors.
If you want to use the output of 'ls' as the input to 'grep', which Bash symbol joins them?
Explanation: The vertical bar '|' creates a pipeline, passing the output of one command as input to another. '#' starts a comment, 'u003E' redirects output to a file, and 'u0026' runs commands in the background. Using the correct symbol is essential for building pipelines.
Which redirection operator sends both standard output and standard error to the same file in Bash?
Explanation: '2u003Eu00261' merges standard error (file descriptor 2) with standard output (file descriptor 1). 'u003Eu003E' appends output, but does not merge errors. '|u0026' merges output only in newer shells, but its meaning differs from '2u003Eu00261'. 'u003C' is used for input redirection, not output.
Which crontab schedule runs a script every day at midnight?
Explanation: '0 0 * * *' means at minute 0, hour 0, every day. '* 0 * * *' is invalid because the minute field cannot be a star if you mean exactly midnight. '0 * * * *' runs hourly at minute 0, and '0 12 * * *' runs every day at noon, not midnight.
How do you access the value of a variable named 'FILENAME' inside a Bash script?
Explanation: Variables are referenced with the dollar sign prefix as '$FILENAME'. 'FILENAME$' is not valid syntax. '%FILENAME' and 'FILENAME:' are not used for variables in Bash. Using the proper syntax avoids errors in scripts.
Which command creates a compressed archive of a directory named 'documents' into 'backup.tar.gz' in Bash?
Explanation: 'tar -czf backup.tar.gz documents' compresses the directory using tar and gzip. 'zip' uses different syntax and format. 'gzip documents backup.tar.gz' is incorrect usage of gzip. 'tar -xzf' is for extracting, not compressing archives.
What is the correct Bash command to display your current user cron jobs?
Explanation: 'crontab -l' lists the cron jobs set for the current user. 'cron list' and 'listcron' are not valid Bash commands. 'crontab --show' is not a supported option for this task.
Which command in a Bash script is used to terminate the script and optionally provide an exit status?
Explanation: 'exit' is the dedicated command in Bash for terminating a script and returning a status. 'leave' and 'stop' are not valid Bash exit commands. 'quit' is not recognized either. Only 'exit' performs this function in shell scripting.
Which crontab time string would run a task every 15 minutes?
Explanation: '*/15 * * * *' means every 15 minutes. '15 * * * *' runs only at minute 15 past each hour. '0 15 * * *' schedules once daily at 15:00. '1/15 * * * *' is invalid syntax in crontab.
Which symbol is used to begin a comment line in a Bash script?
Explanation: Comments in Bash scripts begin with '#'. '//' is for comments in languages like C++ or JavaScript. ';' is used for command separation, and '@' is not a comment indicator. Commenting with '#' improves script readability and understanding.
How can you set the PATH environment variable just for commands run by your crontab?
Explanation: You can set environment variables in cron by placing entries like 'PATH=...' at the top of your crontab file. Setting PATH in .bashrc or running export PATH affects your shell but not cron. Including PATH only when running the command changes it per command but isn't applied globally inside the crontab configuration.
In Bash scripts, which operator allows you to run a second command only if the first succeeds?
Explanation: 'u0026u0026' runs the second command only if the first returns a success status. '||' executes the second command only on failure of the first. '|' passes output to another command, and 'u0026' runs the command in the background. Only 'u0026u0026' is used for conditional command execution based on success.
Which Bash wildcard character matches any number of characters in a filename, such as 'file*'?
Explanation: The asterisk '*' matches zero or more characters in filenames. '?' matches a single character, '[ ]' matches a range or specific set of characters, and '#' is not a Bash wildcard. Understanding wildcards helps in scripting file operations efficiently.