1/98
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
Describe the format of a basic shell script file:
Shell script files must be plain text files; they cannot be generated using a word processing application. The first line in the shell script should be the shebang combination (#!/bin/sh). This indicates the shell that should be used to run the script. The remainder of the shell script is a list of commands or shell statements arranged in the order in which they are to be run by the shell. Though not required, it's common to use the exit statement at the end of a shell script to control the exit status generated by the shell, especially if the shell exists with any type of error condition.
(MU1_Ch11_ExamEssentials)
Explain how using variables helps when writing a shell script:
Shell Scripts use variables to store data or other information for use later in the shell script. You assign data to a variable using the equal sign (=). The data can be any type of text or numerical data, or if you redirect the output of a command to store in a variable by placing backtick (`) characters around the command. To reference the value of a variable inside the shell script, precede the variable with a dollar sign. You can reference variables in shell commands as well as in shell statements. (MU1_Ch11_ExamEssentials)
Explain how to pass data to a shell script:
Shell scripts can use parameters, or arguments, to pass data from the command line to shell script. Just include the data parameters on the command line command when starting the shell script. Inside the shell script you can retrieve any command line parameters by using positional variables. The $0 positional variable is special; it references the shell command used to launch the shell script. Positional variable $1 references the first parameter entered on the command line, variable $2 the 2nd parameter, and so on.
(MU1_Ch11_ExamEssentials)
Describe the different types of programming features you can use in shell scripts:
Shell scripts can use conditional statements to evaluate data and make programming decisions based on the outcome of the evaluation. The if and case statements are two conditional statements that you can use for that. The if statement can be paired with the else statement to create a true false scenario for executing one set of statements if a condition is true, and another set of statements if a condition is false. The case statement allows you to specify a range of possible values that a condition can evaluate to and execute separate sets of statements for each possible value. Besides conditional statements, shell scripts can use loops to iterate through a set of statements multiple times, based on either a series of values or a condition. Finally, shell scripts can also support defining functions, which are a set of statements bundled together to be called from anywhere in the shell script as needed.
(MU1_Ch11_ExamEssentials)
After using a text editor to create a shell script, what step should you take before trying to use the script by typing its name?
(A) Set one or more executable bits using chmod.
(B) Copy the script to the /usr/bin/scripts directory.
(C) Compile the script by typing bash scriptname, where scriptname is the script's name.
(D) Run a virus checker on the script to be sure it contains no viruses.
(E) Run a spell checker on the script to ensure it contains no bugs.
Set one or more executable bits using chmod.
(MU1_Ch11_ReviewQs)
Describe the effects of the following short script, cp1, if it's called as cp1 big.c big.cc:
#!/bin/bash
cp $2 $1
(A) It has the same effect as the cp command -- copying the contents of big.c to big.cc.
(B) It copies the C program big.c and calls the result big.cc.
(C) It copies the contents of big.cc to big.c, eliminating the old big.c.
(D) It converts the C program big.c into a C++ program called big.cc.
(E) The script's first line is invalid, so it won't work.
It copies the contents of big.cc to big.c, eliminating the old big.c.
(MU1_Ch11_ReviewQs)
What is the purpose of conditional expressions and shell scripts?
(A) They prevent scripts from executing if license conditions aren't met.
(B) They display info about the script's computer environment.
(C) They enable the script to take different actions in response to variable data.
(D) They enable scripts to learn in a manner reminiscent of Pavlovian conditioning.
(E) They cause scripts to run only at specified times of day.
They enable the script to take different actions in response to variable data.
(MU1_Ch11_ReviewQs)
A user types myscript laser.txt to run a script called my script. Within myscript, the $0 variable holds the value laser.txt. True or False?
False
(MU1_Ch11_ReviewQs)
Valid looping statements in Bash include for, while, and until. True or False?
True
(MU1_Ch11_ReviewQs)
The following script launches three simultaneous instances of the terminal program.
#!/bin/bash
terminal
terminal
terminal
True or False?
False
(You've written a simple shell script that does nothing but launch programs. To ensure that the script works with most user shells, the first line should be #!/bin/sh)
(MU1_Ch11_ReviewQs)
You've written a simple shell script that does nothing but launch programs. To ensure that the script works with most user shells, the first line should be ______________.
(A) #!/bin/sh
(B) /bin/sh
(C) # /bin/sh
(D) bash
(E) #!bash
#!/bin/sh
(To ensure that the script works with most user shells, the first line should be #!/bin/sh)
(MU1_Ch11_ReviewQs)
The ______________ Bash scripting command is used to display prompts for a user in a shell script.
(A) case
(B) while
(C) if
(D) echo
(E) exit
echo
(MU1_Ch11_ReviewQs)
The ______________ Bash scripting command is used to control the program flow based on a variable that can take many values, such as all the letters of the alphabet.
(A) case
(B) while
(C) if
(D) echo
(E) exit
case
(MU1_Ch11_ReviewQs)
The ______________ Bash scripting command controls the return value generated by a script, independent of the other commands used in the script.
(A) case
(B) while
(C) if
(D) echo
(E) exit
exit
(MU1_Ch11_ReviewQs)
Which one of the following choices is known as the shebang combination?
(i) (#/!/bin/sh)
(ii) (#!/bin/sh)
(iii) (#/bin/sh/!)
(iv) (#/bin!/sh)
#!/bin/sh
(The shebang combination is (#!/bin/sh).)
(MU1_KC)
Why is scripting considered important in Linux?
It saves time by automating repetitive tasks and allows users to focus on more meaningful work.
(MU1_USec12_96)
What is a Linux script essentially made of?
A Linux script is essentially a series of linked commands executed in a specific order.
(MU1_USec12_96)
What benefit does scripting provide in terms of task execution?
It ensures procedural integrity by consistently executing steps in the exact order specified.
(MU1_USec12_96)
What is the long-term benefit of investing time into writing a script?
Once created, a script can be reused repeatedly with accuracy and consistency.
(MU1_USec12_96)
Who especially benefits from scripting, according to the lesson?
People who are "inherently lazy" benefit because scripting automates tedious tasks.
(MU1_USec12_96)
(MU1_USec12_97)
What text editors can be used to create shell scripts on Ubuntu?
Nano, Pico, and VI can be used to create shell scripts, with Nano being the default on Ubuntu.
(MU1_USec12_97)
What command installs the full version of the VI editor?
sudo apt get install vim
(MU1_USec12_97)
What is the shebang line and why is it important in a shell script?
The shebang line (#!/bin/bash) specifies the program used to execute the script and must be the first line in the script.
(MU1_USec12_97)
What does the command `set background=dark` do in Vim?
It improves syntax highlighting by adjusting the color scheme for dark terminal backgrounds.
(MU1_USec12_97)
What file can be edited to apply persistent Vim settings like background color?
~/.vimrc
(MU1_USec12_97)
What abbreviation was created in Vim to insert the shebang line quickly?
_abbr _H was created to expand into #!/bin/bash when typed.
(MU1_USec12_97)
How do you insert a new line in Vim while editing?
Press `o` while in command mode to open a new line below the cursor.
(MU1_USec12_97)
How do you save and exit a file in Vim?
Press `Esc`, then type `:x` and press `Enter`.
(MU1_USec12_97)
How do you exit a file in Vim without saving changes?
Press `Esc`, then type `:q!` and press `Enter`.
(MU1_USec12_97)
Where should the shebang line be placed in a shell script?
The shebang line should always be the very first line of a shell script.
(MU1_USec12_97)
(MU1_USec12_98)
Why is it helpful to add a .sh extension to your script filenames?
It helps ensure uniqueness and enables syntax highlighting in editors like Vim or Nano.
(MU1_USec12_98)
Why should you avoid naming your script the same as existing system commands?
To prevent command name conflicts that could interfere with system behavior or your scripts.
(MU1_USec12_98)
Where should you place your scripts so they can be run from anywhere without specifying a path?
In a directory that is part of your PATH variable, such as a ~/bin directory.
(MU1_USec12_98)
How do you create a bin directory in your home folder?
mkdir bin
(MU1_USec12_98)
How can you add your ~/bin directory to your PATH variable temporarily?
PATH="$PATH:$HOME/bin"
(MU1_USec12_98)
How do you check if your PATH variable includes your ~/bin directory?
echo $PATH
(MU1_USec12_98)
What command is used to move a script into your ~/bin directory?
mv scriptname ~/bin
(MU1_USec12_98)
What happens if a script is placed in a directory listed in your PATH?
It can be run from any location in the file system without specifying the path.
(MU1_USec12_98)
What is the benefit of using an abbreviation like _sh in Vim?
It quickly inserts a shebang line like #!/bin/bash into your script.
(MU1_USec12_98)
How do you make a script executable only by the owner?
chmod u+x scriptname
(MU1_USec12_98)
What does the variable $USER represent in a shell script?
It holds the name of the current logged-in user.
(MU1_USec12_98)
What happens when you run a shell script that contains echo $USER?
It prints the username of the person running the script.
(MU1_USec12_98)
Why is placing scripts in ~/bin useful for future reuse?
It allows centralized access to personal scripts from any location in the terminal.
(MU1_USec12_98)
What does exporting a variable in a shell script do?
It makes the variable available to subshells created by the script.
(MU1_USec12_99)
Does exporting a variable in a script make it available to the parent shell?
No, the parent shell is not a subshell, so the variable is not available after the script ends.
(MU1_USec12_99)
What is a common behavior when a script is executed in Bash?
It creates a new fork or subshell process to run the script.
(MU1_USec12_99)
How can you run a script so that it modifies the current shell environment?
By using the `source` command to execute it in the current shell.
(MU1_USec12_99)
What does the command `source scriptname` do?
It runs the script in the current shell environment without forking a new process.
(MU1_USec12_99)
What special variable holds the process ID of the current shell or script?
$$
(MU1_USec12_99)
How can you demonstrate the difference in process ID when using source vs normal execution?
Run the script both ways and echo $$ inside the script; they will differ unless sourced.
(MU1_USec12_99)
What is the result of echoing `$1` inside a script?
It prints the first argument passed to the script.
(MU1_USec12_99)
What happens if no arguments are passed to a script that echoes `$1`?
It prints a blank line or an empty value.
(MU1_USec12_99)
What happens when you execute `./script.sh Fred` and the script contains `echo Hello $1`?
It prints `Hello Fred`.
(MU1_USec12_99)
What does `$2` represent in a shell script?
The second argument passed to the script.
(MU1_USec12_99)
If a script uses `$1` and you pass two arguments to it, which one will be used?
Only the first argument will be used unless `$2` is also referenced in the script.
(MU1_USec12_99)
Why is the `source` command important for setting variables in your current shell?
Because it avoids creating a new subshell and makes variables available immediately in the current shell.
(MU1_USec12_99)
What happens when a script is run with no arguments and it only echoes $1?
It prints just the default message (e.g., "Hello") because $1 is empty.
(MU1_USec12_100)
What command is used to test if a variable is set in Bash scripts?
test -z $1
(MU1_USec12_100)
What does the command `test -z $1 && exit` do in a script?
It checks if $1 is empty, and if so, exits the script early.
(MU1_USec12_100)
What is the purpose of using `&&` in `test -z $1 && exit`?
It runs the `exit` command only if the `test` command returns true.
(MU1_USec12_100)
What is a shorthand alternative to the `test` command in conditional expressions?
Using square brackets: `[ -z $1 ]`
(MU1_USec12_100)
What is required when using square brackets as a test in Bash?
There must be a space after the opening bracket and before the closing bracket.
(MU1_USec12_100)
What does `[ -z $1 ] && exit` do?
It checks if $1 is empty, and if so, exits the script without printing anything.
(MU1_USec12_100)
How can you write the same logic in full if-statement syntax in Bash?
if [ -z $1 ]; then exit; fi
(MU1_USec12_100)
What is the benefit of using the full `if...then...fi` structure instead of `&&`?
It allows more complex logic or multiple commands inside the conditional block.
(MU1_USec12_100)
What does the `fi` keyword do in Bash scripting?
It marks the end of an `if` statement block.
(MU1_USec12_100)
When would a longhand `if` statement be more appropriate than a short `&&` expression?
When multiple commands need to be executed conditionally.
(MU1_USec12_100)
What is the main purpose of conditional statements in shell scripting?
To control script behavior based on whether certain conditions are true or false.
(MU1_USec12_100)
What is the purpose of using a loop in Bash scripting?
To perform repetitive tasks automatically without rewriting code for each item.
(MU1_USec12_101)
What is the syntax structure for a basic for loop in Bash?
for variable in list; do commands; done
(MU1_USec12_101)
Why is `sudo` used before the `passwd` command?
To ensure proper privileges are granted for setting user passwords.
(MU1_USec12_101)
What command is used to verify that the users were added to the system?
tail -n 3 /etc/passwd
(MU1_USec12_101)
What is the purpose of viewing `/etc/passwd` with `tail -n 3`?
To see the last three user entries created by the script.
(MU1_USec12_101)
What is the difference between `/etc/passwd` and `/etc/shadow`?
/etc/passwd stores user account info, while /etc/shadow stores encrypted passwords.
(MU1_USec12_101)
Are loops limited to use inside scripts in Bash?
No, loops can be typed and executed directly from the command line.
(MU1_USec12_101)
What other loop types are mentioned besides for loops?
while loops and until loops.
(MU1_USec12_101)
How does a while loop differ from an until loop?
While loops execute as long as the condition is true; until loops run until the condition becomes true.
(MU1_USec12_101)
What is an alternative to the for loop for repeating code in Bash scripts?
The while loop.
(MU1_USec12_102)
Why should variables be quoted in Bash scripts?
To prevent errors when the variable contains spaces.
(MU1_USec12_102)
Why is the '$' not used when assigning input to a variable with `read`?
Because variable names should not be prefixed with `$` on the left-hand side of an assignment.
(MU1_USec12_102)
What keywords start and end a while loop in Bash?
while and done
(MU1_USec12_102)
What Bash loop runs as long as a condition is false?
The until loop.
(MU1_USec12_102)
What happens if the user presses Enter without inputting a name during the while loop?
The loop continues prompting for input.
(MU1_USec12_102)
What is the purpose of restructuring the Hello script with a while loop?
To make the script more functional and user-friendly by handling missing input.
(MU1_USec12_102)
How does the script behave when a valid name is eventually entered?
It echoes the entered name and exits the loop.
(MU1_USec12_102)
What is the benefit of using a `.sh` or similar extension when naming scripts?
It helps ensure name uniqueness and enables syntax highlighting in editors.
(MU1_USec12_104)
Where should you create your personal script directory?
In your home directory, typically as a bin directory.
(MU1_USec12_104)
What should you do after creating a script to make it runnable?
Use chmod +x followed by the path to the script to make it executable.
(MU1_USec12_104)
What is the purpose of the shebang (#!) line in a script?
It specifies the path to the script interpreter.
(MU1_USec12_104)
What happens to variables in a forked shell?
They are lost when the shell exits and are not available to the parent shell.
(MU1_USec12_104)
How can you make variables from a script available to the current shell?
By using the `source` command to run the script.
(MU1_USec12_104)
What does sourcing a script do differently than executing it normally?
It runs the script in the current shell rather than a new forked shell.
(MU1_USec12_104)
What is the purpose of using conditional statements in a script?
To execute different code paths depending on specific conditions.
(MU1_USec12_104)
What command can be used for conditional testing in Bash scripts?
The `test` command or square brackets `[]`.
(MU1_USec12_104)
What is the syntax structure of a for loop in Bash?
The loop is enclosed between `do` and `done` keywords.
(MU1_USec12_104)
What type of loop is used to repeat code while a condition is true?
The `while` loop.
(MU1_USec12_104)
What is the primary benefit of scripting according to the instructor?
It automates repetitive tasks and saves time.
(MU1_USec12_104)