LFCS Certification Shell Scripting and Automation 1 — Questions and Answers
Question 1: What is the purpose of the shebang line (e.g., #!/bin/bash) at the top of a shell script?
- It adds a comment describing the script purpose
- It specifies the interpreter to use when executing the script (Correct answer)
- It sets the script's execution permissions automatically
- It defines the shell's PATH variable for the session
Correct answer: It specifies the interpreter to use when executing the script
The shebang line tells the operating system which interpreter to use to execute the script when it is run directly.
Question 2: Which command makes a shell script executable for the file owner, group, and others?
- chmod 644 script.sh
- chmod 400 script.sh
- chmod +x script.sh (Correct answer)
- chmod 600 script.sh
Correct answer: chmod +x script.sh
chmod +x adds the execute permission bit for owner, group, and others, making the script runnable.
Question 3: What is the correct bash syntax to check if the integer variable $NUM equals 10?
- if [ $NUM = 10 ]
- if [ $NUM -eq 10 ] (Correct answer)
- if ($NUM == 10)
- if { $NUM -eq 10 }
Correct answer: if [ $NUM -eq 10 ]
The -eq operator is the correct arithmetic comparison operator for integers inside single bracket [ ] test expressions in bash.
Question 4: Which special variable holds the exit status of the most recently executed command?
- $0
- $#
- $? (Correct answer)
- $$
Correct answer: $?
$? stores the exit code of the last foreground command; 0 means success and any non-zero value indicates failure.
Question 5: What is the correct way to assign the string 'admin' to the variable USERNAME in bash?
- set USERNAME = 'admin'
- USERNAME='admin' (Correct answer)
- $USERNAME = 'admin'
- let USERNAME = 'admin'
Correct answer: USERNAME='admin'
In bash, variable assignment uses no spaces around the = sign; spaces would cause bash to interpret it as a command.
Question 6: What does the built-in `read` command do when used in a bash script?
- Reads the contents of a file into memory for processing
- Reads a line of input from stdin and stores it in a variable (Correct answer)
- Displays the current value of a named variable
- Reads environment variables from /etc/environment
Correct answer: Reads a line of input from stdin and stores it in a variable
The read command pauses script execution, waits for user input from stdin, and assigns the input to the specified variable.
Question 7: Which bash loop construct iterates over each word in a space-separated list?
- while item in list; do ... done
- loop item list; do ... done
- for item in list; do ... done (Correct answer)
- foreach item in list; do ... done
Correct answer: for item in list; do ... done
The for...in loop is the standard bash construct for iterating over a list of words, files, or output items.
What is the purpose of the shebang line (e.g., #!/bin/bash) at the top of a shell script?