010-160 - LPI Linux Essentials Basic Scripting Concepts Questions and Answers — Questions and Answers
Question 1: A shell script contains the following lines: MY_VAR="Linux System" echo "I am learning about the '$MY_VAR'" echo "I am learning about the \"$MY_VAR\"" What is the output when this script is executed?
- I am learning about the '$MY_VAR' I am learning about the "Linux System" (Correct answer)
- I am learning about the 'Linux System' I am learning about the "$MY_VAR"
- I am learning about the '$MY_VAR' I am learning about the "$MY_VAR"
- I am learning about the 'Linux System' I am learning about the "Linux System"
Correct answer: I am learning about the '$MY_VAR' I am learning about the "Linux System"
Single quotes (') prevent any kind of expansion, so '$MY_VAR' is printed literally. Double quotes (") allow for variable expansion, so "$MY_VAR" is replaced with its value, "Linux System". The backslashes before the inner double quotes are necessary to escape them so they are printed as literal characters.
Question 2: Which of the following lines in a script correctly executes the `whoami` command and assigns its output to a variable named `CURRENT_USER`?
- CURRENT_USER='whoami'
- CURRENT_USER=$(whoami) (Correct answer)
- let CURRENT_USER=whoami
- CURRENT_USER->whoami
Correct answer: CURRENT_USER=$(whoami)
The `$(command)` syntax is used for command substitution. It executes the command within the parentheses (`whoami`) and substitutes its standard output. This output is then assigned to the `CURRENT_USER` variable.
Question 3: What is the primary purpose of the `#!/bin/bash` line at the beginning of a shell script?
- It is a comment indicating the script's version number.
- It sets the script's permissions to be executable by default.
- It specifies the absolute path to the interpreter that should execute the script. (Correct answer)
- It pre-loads all environment variables from the user's .bashrc file.
Correct answer: It specifies the absolute path to the interpreter that should execute the script.
This line, known as a 'shebang', is an interpreter directive. It tells the operating system's program loader which program (in this case, `/bin/bash`) should be used to run the commands contained in the script.
Question 4: A user creates a script named `greet.sh` with the content `echo "Hello, $1"`. How should the user execute this script to produce the output `Hello, David`?
- echo "David" | ./greet.sh
- David ./greet.sh
- ./greet.sh $1="David"
- ./greet.sh David (Correct answer)
Correct answer: ./greet.sh David
Arguments passed to a script on the command line are known as positional parameters. `$1` refers to the first argument, `$2` to the second, and so on. By running `./greet.sh David`, the string "David" becomes the first argument and is substituted for `$1` within the script.
Question 5: After a command fails in a shell script, what is the most reliable way to check for this failure condition?
- Check if the variable `$FAIL` is set to 1.
- Check if the output of the `$?` variable is a non-zero value. (Correct answer)
- Check if the variable `$PID` is less than 0.
- Check the system log for an error message with `dmesg`.
Correct answer: Check if the output of the `$?` variable is a non-zero value.
The special shell variable `$?` always holds the exit status of the most recently executed foreground command. By convention, an exit status of 0 indicates success, while any non-zero value indicates an error or failure.
Question 6: A user has written a script file named `run_report.sh` but receives a "Permission denied" error when trying to execute it with `./run_report.sh`. The output of `ls -l` shows the permissions as `-rw-r--r--`. Which command will resolve this issue?
- chown user run_report.sh
- chmod +x run_report.sh (Correct answer)
- set-executable run_report.sh
- run -a run_report.sh
Correct answer: chmod +x run_report.sh
To be executed directly, a script file requires the execute permission. The `chmod +x` command adds the execute (x) permission for the owner, group, and others, resolving the "Permission denied" error.
A shell script contains the following lines:
MY_VAR="Linux System"
echo "I am learning about the '$MY_VAR'"
echo "I am learning about the \"$MY_VAR\""
What is the output when this script is executed?