Study Guide - Chap25: Deploying Bash Scripts Flashcards
(13 cards)
1- What character or characters make up the shebang used in Linux to define the shell used for a shell script?
- ˃˃
- # !
- |
- ˃
- 2˃
#!
The #! character combination defines the shebang, which tells the Linux shell what shell to use to run the shell script code, so option B is correct. The ˃˃ character combination appends the output of a command to a file, so option A is incorrect. The | character pipes the output of a command to another command, so option C is incorrect. The ˃ character redirects the output of a command to a new file or overwrites an existing file, so option D is incorrect. The 2˃ character combination redirects error messages from a command to a file, so option E is incorrect.
shell scripting:»_space;
appends the output of a command to an existing file
shell scripting: #!
defines the shebang, which tells the Linux shell what shell to use to run the shell script code
shell scripting: |
pipes the output of a command to another command
shell scripting: >
redirects the output of a command to a new file or overwrites an existing file
shell scripting: 2>
redirects error messages from a command to a file
3- Jasmine has created a new Bash shell script and wants to run it from the command line. What chmod permissions should she assign to the file to run it as a shell script?
- 644
- u+r
- u+x
- u+w
- u=wr
u+x
The u+x chmod permission assigns execute permissions to the file owner so that you can run the file at the command prompt, which makes option C correct. The 644 octal permission assigns only read and write permissions to the file owner, not execute permissions, so option A is incorrect. The u+r permission assigns read permissions, not execute permissions, so option B is incorrect. The u+w permission assigns only write permissions and not execute permissions, so option D is incorrect. The u=wr permission assigns both read and write permissions but not execute permissions to the file owner, so option E is incorrect.
Environmental Variables:
- $USER
- $UID
- $HOME
- $BASH
- $USER: text username of the user account that started the shell
- $UID: contains the numeric user ID
- $HOME: contains the home directory location of the user account
- $BASH: contains the location of the Bash shell executable file
5- Zuri is writing a Bash shell script and needs to assign a number to a variable. How should he do that?
- var1=$(10)
- var1 = 10
- var1=10
- var1=”10”
- var1=`10`
var1=10
To assign a value to a variable, you use the equal sign, but no spaces must be used between the variable name, the equal sign, and the value, so option C is correct. Option A uses the command substitution format, which doesn’t assign a value to a variable but to the output of a command, so option A is incorrect. Option B places spaces between the variable name, equal sign, and the value, so option B is incorrect. Option D places quotes around the value, making it a string value and not a numeric value, so option D is incorrect. Option E uses backtick characters around the value, which attempts to run it using command substitution, which is incorrect.
6- Cameron is writing a Bash shell script and needs to test if a file exists and that it’s a file. What line of code should he write to do that?
- if [ -e file ]
- if [ -f file ]
- if [ -d file ]
- if [ -x file ]
- if [ -w file ]
if [ -f file ]
The -f file test checks if the specified object exists, and if it’s a file, so option B is correct. The -e file test checks if the object exists, not the object type, so option A is incorrect. The -d file test checks if the object exists but is a directory, not a file, so option C is incorrect. The -x file test checks if the current user account has execute permissions for the file, not that the object exists and is a file, so option D is incorrect. The -w file test checks if the current user account has write permissions for the file, not that the object exists and is a file, so option E is incorrect.
8- Christina is creating a Bash shell script and wants to make the script return a value of 2 if it fails. What statement should she add to do that?
- # !
- $?
- $1
- exit
- while
exit
The exit command allows us to return a specific error status when the shell script exits, so option D is correct. The #! shebang defines the shell to use to run the shell script, not the exit status, so option A is incorrect. The $? character combination displays the exit status from the last command; it doesn’t return a specific exit status, so option B is incorrect. The $1 variable contains the first command‐line parameter used when the shell script is launched from the command line; it doesn’t set the exit status for the shell script, so option C is incorrect. The while command allows us to iterate through a set of commands until a specific condition is met; it doesn’t return a specific exit status when the shell exits, so option E is incorrect.
9- What command should you use to perform a command substitution to assign the output of a command to a variable in your shell script?
- ˃
- ˃˃
- $[]
- |
- $()
$()
The $() command assigns the output of a command to a specified variable in the shell script, so option E is correct. The ˃ character redirects the output of a command to a file, not to a variable, so option A is incorrect. The ˃˃ character combination appends the output of a command to an existing file, not to a variable, so option B is incorrect. The $[] command performs integer mathematical operations in the Bash shell, so option C is incorrect. The | character redirects the output of a command to another command, not to a variable, so option D is incorrect.
10- What command should you use to perform a mathematical operation in your shell script?
- ˃
- ˃˃
- $[]
- |
- $()
$[]
The $[] command performs simple integer mathematical operations in the Bash shell, so option C is correct. The ˃ character redirects the output of a command to a new file, so option A is incorrect. The ˃˃ character combination appends the output of a command to an existing file, so option B is incorrect. The | character redirects the output of a command to another command, so option D is incorrect. The $() command redirects the output of a command to a variable in the shell script, so option E is incorrect.