Chapter 25 Flashcards

Deploying Bash Scripts

1
Q

Describe how to link multiple command-line commands together in a shell script. 

A

The Bash shell allows you to place multiple commands sequentially in a file and will then process each command when you run the file from the command line. The output from each command will appear in the command-line output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain how you can handle data within a Bash shell script. 

A

The Bash shell provides two ways to handle data within commands. Output redirection allows you to redirect the output of a command to a text file, which you, or another command, can read later. Piping allows you to redirect the output of one command to use as the input data for another command. The output never displays on the monitor when you run the shell script; the data transfer happens behind the scenes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain the type of data you can access from within a shell script. 

A

The Bash shell provides access to environment variables, which contain information about the shell environment the script is running in. You can obtain information about the system as well as the user account that’s running the shell script. The shell script also has access to positional variables, which allow you to pass data to the shell script from the command line when you run the shell script.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Describe how you can manipulate output data from a command before you use it in another command within a shell script. 

A

Command substitution allows you to redirect the output of a command to a user variable in your shell script. You can then use standard Linux text processing commands to manipulate the data, such as sort it or extract data records from it, before redirecting the variable data to another command.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Describe how the Bash shell performs mathematical operations. 

A

The Bash shell uses the $[] symbol to define mathematical equations to process. The Bash shell can only perform integer math, so this capability is somewhat limited.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain the different methods for implementing logic within a Bash shell script. 

A

The Bash shell supports both if statements and the case statement. They both allow you to perform a test on a numerical value, a string value, or a file and then run a block of commands based on the outcome of the test.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What character or characters make up the shebang used in Linux to define the shell used for a shell script?

>>
#!
|
>
2>
A

B. 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&raquo_space; 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Henry needs to store the output from his script into a new log file that he can read later. What character or characters should he use to do that?

>>
#!
|
>
2>
A

D. The > character redirects all of the output from a command to a new file, or overwrites an existing file, so option D is correct. The&raquo_space; character combination appends all of the output from a command to an existing file, so option A is incorrect. The #! combination defines the shell to use, so option B is incorrect. The | character pipes output from one command to another command, so option C is incorrect. The 2> character combination redirects only error messages from a command to a new file, not all of the output, so option E is incorrect.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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
A

C. 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What environment variable contains the username of the user who started the shell?

$USER
$UID
$HOME
$BASH
$1
A

A. The $USER environment variable contains the text username of the user account that started the shell, so option A is correct. The $UID environment variable contains the numeric user ID, not the text username, so option B is incorrect. The $HOME environment variable contains the home directory location of the user account, not the username, so option C is incorrect. The $BASH environment variable contains the location of the Bash shell executable file, not the username of the user who started the shell, so option D is incorrect. The $1 variable is a positional variable, not an environment variable. It’s used to retrieve data from the command-line command that launched the shell, not to identify the user who started the shell, so option E is incorrect.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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`
A

C. 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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 ]
A

B. 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What character or combination of characters do you use to redirect the output of one command to another command?

>>
#!
|
>
2>
A

C. The bar character (|) pipes the output of one command to the input of another command, so option C is correct. The&raquo_space; character combination appends the output of a command to an existing file, not to another command, so option A is incorrect. The shebang (#!) is used to identify the shell to use to run the script, not to redirect output from a command to another command, so option B is incorrect. The > character redirects the output of a command to a new file, not to another command, so option D is incorrect. The 2> character combination redirects the error messages from a command to a new file, not to another command, so option E is incorrect.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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
A

D. 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What command should you use to perform a command substitution to assign the output of a command to a variable in your shell script?

>
>>
$[]
|
$()
A

E. 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&raquo_space; 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What command should you use to perform a mathematical operation in your shell script?

>
>>
$[]
|
$()
A

C. 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&raquo_space; 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.

17
Q
A