Automation with Bash: Cron, Shell Scripts, and Pipelines Quiz Quiz

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.

  1. Understanding Cron Syntax

    Which field in a standard crontab entry represents the hour when specifying a schedule like '30 5 * * *'?

    1. The second field
    2. The first field
    3. The third field
    4. The fourth field

    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.

  2. Shell Script Basics

    Which line at the very top of a shell script designates the script’s interpreter, as in '#!/bin/bash'?

    1. Package line
    2. Function declaration
    3. Shebang line
    4. Header comment

    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.

  3. Making a Script Executable

    What Bash command would you use to grant execute permissions to a shell script named 'backup.sh'?

    1. source backup.sh
    2. chmod +x backup.sh
    3. run backup.sh
    4. chown 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.

  4. Editing User Cron Jobs

    To safely edit your personal cron jobs, which command should you use in a Bash terminal?

    1. crontab --edit
    2. crontab -e
    3. cronedit
    4. editcron

    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.

  5. Combining Commands with Pipelines

    If you want to use the output of 'ls' as the input to 'grep', which Bash symbol joins them?

    1. #
    2. |
    3. u0026
    4. u003E

    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.

  6. Redirecting Script Output

    Which redirection operator sends both standard output and standard error to the same file in Bash?

    1. u003Eu003E
    2. u003C
    3. |u0026
    4. 2u003Eu00261

    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.

  7. Scheduling Frequency with Cron

    Which crontab schedule runs a script every day at midnight?

    1. 0 12 * * *
    2. 0 0 * * *
    3. * 0 * * *
    4. 0 * * * *

    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.

  8. Identifying Shell Variables

    How do you access the value of a variable named 'FILENAME' inside a Bash script?

    1. %FILENAME
    2. FILENAME$
    3. $FILENAME
    4. FILENAME:

    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.

  9. Automating a Simple Backup

    Which command creates a compressed archive of a directory named 'documents' into 'backup.tar.gz' in Bash?

    1. zip backup.tar.gz documents
    2. gzip documents backup.tar.gz
    3. tar -xzf backup.tar.gz documents
    4. tar -czf backup.tar.gz documents

    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.

  10. Viewing Current Cron Jobs

    What is the correct Bash command to display your current user cron jobs?

    1. crontab --show
    2. crontab -l
    3. cron list
    4. listcron

    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.

  11. Exiting a Shell Script

    Which command in a Bash script is used to terminate the script and optionally provide an exit status?

    1. quit
    2. leave
    3. stop
    4. exit

    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.

  12. Crontab String for Every 15 Minutes

    Which crontab time string would run a task every 15 minutes?

    1. */15 * * * *
    2. 15 * * * *
    3. 0 15 * * *
    4. 1/15 * * * *

    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.

  13. Writing Comments in Bash Scripts

    Which symbol is used to begin a comment line in a Bash script?

    1. ;
    2. #
    3. @
    4. //

    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.

  14. Setting Environment Variables in Cron

    How can you set the PATH environment variable just for commands run by your crontab?

    1. Add 'PATH=...' at the top of the crontab
    2. Run export PATH in the shell
    3. Set PATH in your .bashrc file
    4. Include PATH=... when running the command

    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.

  15. Chaining Shell Commands

    In Bash scripts, which operator allows you to run a second command only if the first succeeds?

    1. u0026
    2. u0026u0026
    3. ||
    4. |

    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.

  16. Using Wildcards in Bash

    Which Bash wildcard character matches any number of characters in a filename, such as 'file*'?

    1. ?
    2. #
    3. *
    4. [ ]

    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.