Linux Ch. 25 Deploying Bash Scripts
Describe how to link multiple command-line commands together in a shell script.
The bash shell allows us 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.
Explain the type of data you can access from within a shell script.
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.
Explain how you can handle data within a bash shell script.
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 scrip; the data transfer happens behind the scenes.
Explain the different methods for implementing logic within a bash shell script.
The bash shell supports both if statements and the case statement. They both allow you to perform a test on a numerical value, string value, or a file and then run a block of commands based on the outcome of the test.
Describe how the bash shell performs math operations.
The bash shell uses the $[ ] symbol to define math equations to process. The bash shell can only perform integer math, so this capability is somewhat limited.
4. What environment variable contains the username of the user who started the shell? A. $USER B. $UID C. $HOME D. $BASH E. $1
A. The $USER environment variable contains the text username of the user account that started the shell, so option A is correct.
1. What character or characters make up the shebang used in Linux to define the shell used for a shell script? A. >> B. #! C. | D. > E. 2>
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.
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? A. if [ -e file ] B. if [ -f file ] C. if [ -d file ] D. if [ -x file ] E. if [ -w file ]
B. The -f file test checks if the specified object exists, and if it's a file, so option B is correct.
10. What command should you use to perform a mathematical operation in your shell script? A. > B. >> C. $[] D. | E. $()
C. The $[ ] command performs simple integer mathematical operations in the bash shell, so option C is correct.
7. What character or combination of characters do you use to redirect the output of one command to another command? A. >> B. #! C. | D. > E. 2>
C. The bar character (|) pipes the output of one command to the input of another command, so option C is correct.
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? A. 644 B. u+r C. u+x D. u+w E. u=wr
C. The u+x chmod permission assign execute permissions to the file owner so you can run the file at the command prompt, which makes option C correct.
5. Zuri is writing a bash shell script and needs to assign a number to a variable. How should he do that? A. var1=$(10) B. var1 = 10 C. var1=10 D. var1="10" E. var1=`10`
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.
Describe how you can manipulate output data from a command before you use it in another command within a shell script.
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.
2. Henry needs to store the output from his script into a log file that he can read later. What character or characters should he use to do that? A. >> B. #! C. | D. > E. 2>
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.
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? A. #! B. $? C. $1 D. exit E. while
D. The exit command allows us to return a specific error status when the shell script exits, so option D is correct.
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? A. > B. >> C. $[] D. | E. $()
E. The $( ) command assigns the output of a command to a specified variable in the shell script, so option E is correct.