What is the name of the startup file that we will be using in this course which contains commands that are run when you first log in?
.bash_profile
8
New cards
Where are the startup files located?
In your home directory
9
New cards
What command would you use the find out more about a built-in command?
help
10
New cards
Are the commands in .bash_profile run when you create a subshell using bash?
No
11
New cards
What is the value of the file descriptor for Standard Input?
0
12
New cards
What is the value of the file descriptor for Standard Output?
1
13
New cards
What is the value of the file descriptor for Standard Error?
2
14
New cards
If you wanted to run the executable file backup in your current directory but wanted the error messages to disappear, what would you write at the command line?
./backup 2> /dev/null
15
New cards
If you wanted to run the executable file backup in your current directory but wanted both the output and the error messages to go to the file results.txt, what would you write at the command line?
./backup &> results.txt
16
New cards
What would you type at the command line if you wanted to make the script cheer.sh executable?
chmod 755 cheer.sh
17
New cards
Write the hashbang line we use for scripts in this course.
\#! /bin/bash
18
New cards
Write the hashbang line you would use if you wanted a script to run using the sh shell.
\#! /bin/sh
19
New cards
Name the three categories of people who might need to read a shell script.
The person who wrote the script, the persons who use the script, and the person who maintains the script
20
New cards
What command would you use to make the changes you made in the startup file for your login shell take effect?
source .bash_profile
21
New cards
What is special about a global variable?
You can use it in any subshell of the shell in which it was created.
22
New cards
What are the limitations of a local variable?
You can only use it in the shell in which it was created
23
New cards
What would you write on the command line to set the value of the local variable named team to Red Sox.
team="Red Sox"
24
New cards
If you define a local variable, can a script see the value of this variable?
No
25
New cards
What would you write on the command line to create the global variable named school to UMass Boston.
export school="UMass Boston"
26
New cards
Write the command you would use in a script to ask the user to provide a value for a variable named dir.
read -p "Directory: " dir
27
New cards
What command would you use to see all global variables?
env
28
New cards
What character would you use if you wanted to run two commands in the foreground, one right after the other, on a single command line?
;
29
New cards
What other two characters allow more than one command to be run on a single command line?
& and |
30
New cards
How can you continue a command onto the next line?
type \ immediately before hitting Enter
31
New cards
What parameter gives the status code returned by the last command run?
?
32
New cards
What parameter gives the total number of arguments passed to a script from the command line?
\#
33
New cards
What parameter gives the full pathname of the script that is currently being run?
0
34
New cards
What parameter gives the first command line argument to a script or function?
1
35
New cards
If you wanted to use the value of the variable team inside quotes, what kind of quotes would you use?
Double quotes
36
New cards
If you wanted to remove the value of the variable team, what would you type at the command line?
team = or unset team
37
New cards
What is the name of the first process created when the machine starts?
init or systemd
38
New cards
Can two processes running at the same time have the same process ID number?
No
39
New cards
Can a process ID number be reused after the process has finished?
Yes
40
New cards
If the process ID of your current shell is 15062, what is the parent process ID of a shell script you run from your current shell?
15062
41
New cards
What command would you run to see the last 20 commands you ran at the command line?
history 20
42
New cards
What is the pathname of the file that, by default, stores the commands you have run in a file so that they will be remembered for the next time you log in?
\~/.bash_history
43
New cards
What command would you use to edit the last command to run, change it and run it again?
fc
44
New cards
What is the name of the library of procedures that allows you to edit the command line?
The Readline library
45
New cards
Name the three things that can be completed at the command line by hitting Tab.
pathnames, commands, shell variables
46
New cards
What do you call a shortcut that is used to run a command?
an alias
47
New cards
What would you write at the command line to create the alias ghdir which changed directory to my home directory?
alias ghdir='cd ~ghoffman'
48
New cards
Can you write a function on a single command line?
Yes if every command is followed by a ; including the last
49
New cards
Can aliases be made global?
No
50
New cards
Can functions be made global?
No
51
New cards
What does Bash do when it performs history expansion?
It reruns an old command line which it retrieves from the history list
52
New cards
What does Bash do when it performs alias substitution?
It replaces the name of the alias with a Unix command
53
New cards
Write the command you would use to create the files foo1.txt to foo5.txt using brace expansion and the touch command.
touch foo{1,2,3,4,5}.txt
54
New cards
What does Bash do when it performs tilde (~) expansion?
Replaces ~ with the absolute address of your home directory or replaces ~UNIX_USERNAME with the absolute address of the home directory of UNIX_USERNAME
55
New cards
What does Bash do when it performs parameter and variable expansion?
substitutes the value of a variable or a parameter for the name of the variable or parameter when preceded by a $
56
New cards
What does Bash do when it performs arithmetic expansion?
Treats the digits inside double parentheses as numbers performs arithmetic operations on them and substitutes the result of that calculation for the expansion expression
57
New cards
What does Bash do when it performs command substitution?
Runs the commands inside the parentheses in a subshell and substitutes the output from those commands for the substitution expression
58
New cards
What would you write at the command line to define the alias ll whose value was ls -l ?
alias ll='ls -l'
59
New cards
Can an alias be made global?
No
60
New cards
Can a function be made global?
No
61
New cards
What are control structures?
Syntactic structures that control how and when other statements are executed
62
New cards
What are the two basic types of control structures?
Conditionals and loops
63
New cards
What is the basic format of the if ... then construct?
if COMMAND
Then
COMMAND_1
COMMAND_2
…
fi
64
New cards
How is the command that follows if used to determine whether or not the statements between the then and fi keywords are executed?
If the command returns an exit status of 0 then statements are executed
65
New cards
What does an exit status of 0 mean?
That the command that returned the status code ran without error
66
New cards
What are the two keywords that surround the statements inside an if statement?
then and fi
67
New cards
Can the then keyword appear on the same line as if?
Yes, but only if it is separated from it by a semicolon (;)
68
New cards
What does the test command do?
Analyze a logical expression and return an exit status that tells whether the expression is true or false
69
New cards
What is the value of the exit status returned by test to indicate that the expression is true?
0
70
New cards
Does the command that follows “if” need to be tested?
No, it can be any command that returns an exit status
71
New cards
What do [ and ] mean when used in an if statement?
They stand for the test command
72
New cards
What are the keywords used to mark a block of commands in an if ... then statement?
then and fi
73
New cards
What are the keywords used to mark the first block of commands in an if ... then ... else statement?
then and else
74
New cards
What are the keywords used to mark the second block of commands in an if ... then ... else statement?
else and fi
75
New cards
What are the keywords used to mark the block of commands following elif?
then and either else or elif or fi
76
New cards
Is there any limit to the number of blocks of commands you can have in an if ... else ... elif statement?
No
77
New cards
How would you run the script do_something.sh if you wanted to print each line of the script before it was executed?
bash -x do_something.sh
78
New cards
If you were debugging a script as in the example above, how would Bash mark the lines of the script itself to distinguish them from the output of the script?
By starting each line with a +
79
New cards
What has to follow each elif in an if ... else ... elif statement?
A Unix command
80
New cards
How does the test command indicate that the logical expression it is evaluating is true?
By returning a status code of 0
81
New cards
In a for ... in loop, where does the loop variable get its values?
From the list of values following the keyword in
82
New cards
In a simple for loop, where does the loop variable get its values?
From the command line arguments
83
New cards
Should the loop variable in a for loop have a $ in front of it?
No
84
New cards
In a three statement for loop, what does the first statement do?
Sets the initial value of a loop variable
85
New cards
In a three statement for loop, what does the second statement do?
It's a logical expression and the loop will continue as long as its value is true
86
New cards
In a three statement for loop, what does the third statement do?
changes the value of the loop variable after each pass through the loop
87
New cards
Why are the three statements in a three statement for loop surrounded by two parentheses?
So the values of all variables will be treated as numbers
88
New cards
When does a while loop stop running?
When the command following while returns an exit status that is not 0
89
New cards
What does the continue command in a loop do?
Stops that pass through the loop and returns to the top of the loop
90
New cards
What does the break command in a loop do?
Jumps completely out of the loop
91
New cards
In a case statement, what determines which block of code is executed?
The value of the variable between case and in
92
New cards
How do you mark the end of a code block in a case statement?
;;
93
New cards
What signals the end of a pattern in a case statement?
)
94
New cards
What meta-characters can you use in a case statement pattern?
all of them: ?, \* and \[ \]
95
New cards
What pattern would you use in a case statement if you wanted to match 11, 12 or 13?
**11|12|13**
96
New cards
What will you see on the screen if you ran a script that uses the select statement?
A menu of numbers followed by values
97
New cards
What happens to the value chosen by the user when responding to a select statement?
The value is assigned to the select variable
98
New cards
What function does the PS3 keyword shell variable serve in a select statement?
It contains the text used in the menu prompt
99
New cards
How do you mark the beginning of a here document in a shell script?
<< followed immediately by a string with no spaces between them
100
New cards
How do you mark the end of a here document?
With the same string that starts the here document on a line by itself