Quiz Module 6 - Shell Scripting
Instructions
This quiz contains 30 multiple-choice questions to validate your understanding of Module 6.
Tip: Answer each question before revealing the answer. The answers are hidden just after each question.
Question 1
What is the standard shebang for a Bash script?
See explanation
Correct answer: #!/bin/bash
Explanation: The shebang #!/bin/bash indicates that the script should be executed with Bash. It must be on the first line of the script. Example: #!/bin/bash followed by a blank line then the code.
Question 2
How to make a script executable?
See explanation
Correct answer: chmod +x script.sh
Explanation: chmod +x script.sh adds execution permission to the script. You can then execute it with ./script.sh or bash script.sh. chmod 755 script.sh does the same thing.
Question 3
How to declare a variable in Bash?
See explanation
Correct answer: var="value"
Explanation: In Bash, you declare a variable with var="value" (no spaces around the =). To use the variable: $var or ${var}. To display it: echo $var.
Question 4
Which command schedules a recurring task in Linux?
See explanation
Correct answer: cron
Explanation: cron is the daemon that runs scheduled tasks. crontab is the command to manage cron tasks. Tasks are defined in crontab files with a special syntax.
Question 5
Which command edits a user's cron tasks?
See explanation
Correct answer: crontab -e
Explanation: crontab -e opens the editor to modify the current user's cron tasks. crontab -l lists tasks, crontab -r removes all tasks. Tasks are stored in /var/spool/cron/crontabs/username.
Question 6
What is the correct syntax of a crontab line?
See explanation
Correct answer: minute hour day month day-of-week command
Explanation: Crontab format: minute hour day month day-of-week command. Examples: 0 * * * * (every hour), 0 0 * * * (midnight every day), */5 * * * * (every 5 minutes). Days of the week: 0-7 (0 and 7 = Sunday).
Question 7
Which crontab syntax runs a task every hour?
See explanation
Correct answer: 0 * * * *
Explanation: 0 * * * * means: minute 0 of every hour, every day, every month, every day of the week = every hour at minute 0. 0 0 * * * = midnight every day. * * * * * = every minute.
Question 8
Which crontab syntax runs a task every day at midnight?
See explanation
Correct answer: 0 0 * * *
Explanation: 0 0 * * * means: minute 0, hour 0 (midnight), every day, every month, every day of the week = every day at midnight. It's very common for daily backups.
Question 9
Which command lists a user's cron tasks?
See explanation
Correct answer: crontab -l
Explanation: crontab -l lists the current user's cron tasks. crontab -l -u username lists another user's tasks (requires root). crontab -e edits, crontab -r removes all tasks.
Question 10
Which control structure allows executing conditional code in Bash?
See explanation
Correct answer: if-then-else
Explanation: The if-then-else structure allows executing conditional code. Syntax: if [ condition ]; then commands; else commands; fi. Example: if [ $var -eq 10 ]; then echo "Equal"; fi.
Question 11
Which loop iterates over a list in Bash?
See explanation
Correct answer: for
Explanation: The for loop iterates over a list. Syntaxes: for var in list; do commands; done or for ((i=1; i<=10; i++)); do commands; done. Example: for file in *.txt; do echo $file; done.
Question 12
Which loop executes code as long as a condition is true?
See explanation
Correct answer: while
Explanation: The while loop executes code as long as a condition is true. Syntax: while [ condition ]; do commands; done. until does the opposite (as long as the condition is false). Beware of infinite loops!
Question 13
Which variable contains the return code of the last command?
See explanation
Correct answer: $?
Explanation: $? contains the return code of the last command. 0 = success, non-zero = error. Example: command; if [ $? -eq 0 ]; then echo "Success"; fi. You can also use directly: if command; then ....
Question 14
Which variable contains the number of arguments passed to the script?
See explanation
Correct answer: $#
Explanation: $# contains the number of arguments passed to the script. $1, $2, etc. contain individual arguments. $0 contains the script name. $@ contains all arguments.
Question 15
Which variable contains the first argument passed to the script?
See explanation
Correct answer: $1
Explanation: $1 contains the first argument, $2 the second, etc. $0 contains the script name. $@ contains all arguments. Example: ./script.sh arg1 arg2 → $1="arg1", $2="arg2".
Question 16
Which command creates a function in Bash?
See explanation
Correct answer: function_name() followed by commands in braces
Explanation: Syntax to create a function: function_name() { commands; } (with braces around the commands). You can also use function function_name { commands; }. To call: function_name or function_name arg1 arg2. Note: Braces are necessary to delimit the function body.
Question 17
Which file contains system cron tasks (for all users)?
See explanation
Correct answer: /etc/crontab
Explanation: /etc/crontab contains system cron tasks. Similar format but with a user field: minute hour day month day-of-week user command. The directories /etc/cron.d/, /etc/cron.hourly/, /etc/cron.daily/, etc. also contain system tasks.
Question 18
Which directory contains scripts executed daily by cron?
See explanation
Correct answer: /etc/cron.daily
Explanation: /etc/cron.daily/ contains scripts executed daily. Similar: /etc/cron.hourly/, /etc/cron.weekly/, /etc/cron.monthly/. These scripts are executed by anacron or run-parts.
Question 19
Which command tests if a file exists in Bash?
See explanation
Correct answer: [ -f file ] or test -f file
Explanation: [ -f file ] tests if a file exists and is a regular file. Other tests: [ -d file ] (directory), [ -e file ] (exists), [ -r file ] (readable), [ -w file ] (writable), [ -x file ] (executable).
Question 20
Which command reads user input in a Bash script?
See explanation
Correct answer: read
Explanation: read variable reads a line from standard input and stores it in the variable. Examples: read name, read -p "Enter your name: " name (with prompt), read -s password (hide input).
Question 21
Which structure allows making a multiple choice in Bash?
See explanation
Correct answer: case-esac
Explanation: case allows making a multiple choice. Syntax: case $var in pattern1) commands;; pattern2) commands;; *) commands;; esac. More readable than multiple if-elif for many choices.
Question 22
Which crontab syntax runs a task every 5 minutes?
See explanation
Correct answer: */5 * * * *
Explanation: */5 * * * * means every 5 minutes. The */5 means "every value divisible by 5". 5 * * * * = only at minute 5 of every hour. 5,10,15 * * * * = at minutes 5, 10, and 15.
Question 23
Which variable contains the current process's PID?
See explanation
Correct answer: $$
Explanation: $$ contains the current shell process's PID. Useful for creating unique temporary files: tempfile="/tmp/script.$$". $PPID contains the parent process's PID.
Question 24
Which command redirects error output to a file?
See explanation
Correct answer: command 2> error.log
Explanation: 2> redirects error output (stderr, file descriptor 2). > redirects standard output (stdout, file descriptor 1). 2>&1 redirects stderr to stdout. &> redirects both to a file.
Question 25
Which command redirects both standard and error output to a file?
See explanation
Correct answer: command &> file or command > file 2>&1
Explanation: command &> file redirects stdout and stderr to the file. Equivalent to command > file 2>&1. The order is important: 2>&1 must come after > file to redirect stderr to the same place as stdout.
Question 26
Which command creates an array in Bash?
See explanation
Correct answer: array=(1 2 3)
Explanation: Syntax to create an array: array=(1 2 3) or array=("one" "two" "three"). To access: ${array[0]}, ${array[1]}, etc. ${array[@]} contains all elements. ${#array[@]} = number of elements.
Question 27
Which crontab syntax runs a task every Monday at 9:00 AM?
See explanation
Correct answer: 0 9 * * 1
Explanation: 0 9 * * 1 = minute 0, hour 9, every day, every month, Monday (1). Days of the week: 0 or 7 = Sunday, 1 = Monday, 2 = Tuesday, ..., 6 = Saturday.
Question 28
Which command exits a function with a return value?
See explanation
Correct answer: return
Explanation: return exits a function with a return code. return 0 = success, return 1 = error. exit exits the entire script. break exits a loop. return without an argument returns the last command's code.
Question 29
Which command exits a script with a return code?
See explanation
Correct answer: exit
Explanation: exit exits the script with a return code. exit 0 = success, exit 1 (or other non-zero) = error. If no code is specified, it uses the last command's code. return only works in functions.
Question 30
Which command views cron task logs on Ubuntu 24.04?
See explanation
Correct answer: journalctl -u cron or grep CRON /var/log/syslog
Explanation: On Ubuntu 24.04, cron logs are in systemd logs. journalctl -u cron shows the cron service logs. You can also search in /var/log/syslog: grep CRON /var/log/syslog. Logs show executions and errors of cron tasks.
Quiz Result
Congratulations on completing the Module 6 quiz!
Score:
- 25-30 correct answers: Excellent! You master Bash scripting and cron.
- 20-24 correct answers: Very good! Review the concepts where you had difficulties.
- 15-19 correct answers: Good! Review the chapters on scripting and cron.
- Less than 15: Recommended to review the module before continuing.
Next Steps
Now that you have validated your knowledge:
Module 7: Networking and Configuration
Practical Labs: Practice scripting and cron with Module 6 labs
Quiz created on: December 2024 - Tested on Ubuntu 24.04