Podcast
Questions and Answers
What command is used to display environmental variables in the shell?
What command is used to display environmental variables in the shell?
The variable assignment in shell requires spaces before and after the equals sign.
The variable assignment in shell requires spaces before and after the equals sign.
False
What will the command 'echo $name' return if 'name' is assigned as 'John Doe'?
What will the command 'echo $name' return if 'name' is assigned as 'John Doe'?
John Doe
To assign a variable, you use the syntax variable1=_______.
To assign a variable, you use the syntax variable1=_______.
Signup and view all the answers
Match the shell command with its expected output:
Match the shell command with its expected output:
Signup and view all the answers
What will be the output of the command 'echo $name' if 'name' is set to 'John*'?
What will be the output of the command 'echo $name' if 'name' is set to 'John*'?
Signup and view all the answers
The command 'name=John' followed by 'name=John Doe' will successfully change the value of 'name'.
The command 'name=John' followed by 'name=John Doe' will successfully change the value of 'name'.
Signup and view all the answers
In the context of shell variables, what does 'TMOUT=0' signify?
In the context of shell variables, what does 'TMOUT=0' signify?
Signup and view all the answers
What is displayed by the variable $0 in shell scripts?
What is displayed by the variable $0 in shell scripts?
Signup and view all the answers
The variable $@ represents the same information as $*.
The variable $@ represents the same information as $*.
Signup and view all the answers
What command is used to remove the first argument from the list in a shell script?
What command is used to remove the first argument from the list in a shell script?
Signup and view all the answers
In the command cmdargs_demo a b c
, the total number of command line arguments passed is: ____.
In the command cmdargs_demo a b c
, the total number of command line arguments passed is: ____.
Signup and view all the answers
Match the shell script variables with their descriptions:
Match the shell script variables with their descriptions:
Signup and view all the answers
The command shift 3
removes the first three arguments from the list.
The command shift 3
removes the first three arguments from the list.
Signup and view all the answers
How many positional parameters can be accessed directly in a shell script?
How many positional parameters can be accessed directly in a shell script?
Signup and view all the answers
What is a shell script primarily composed of?
What is a shell script primarily composed of?
Signup and view all the answers
A shell variable can only be read but not written to.
A shell variable can only be read but not written to.
Signup and view all the answers
What command would you use to make a script file executable?
What command would you use to make a script file executable?
Signup and view all the answers
The command used to run a shell script in the Bourne Shell is $ ____ script_file.
The command used to run a shell script in the Bourne Shell is $ ____ script_file.
Signup and view all the answers
Match the shell variable to its corresponding purpose:
Match the shell variable to its corresponding purpose:
Signup and view all the answers
Which command is used for command substitution in BASH?
Which command is used for command substitution in BASH?
Signup and view all the answers
Command line arguments can be passed to shell programs.
Command line arguments can be passed to shell programs.
Signup and view all the answers
What is the significance of the shebang (#!) in a shell script?
What is the significance of the shebang (#!) in a shell script?
Signup and view all the answers
Which of the following elements should be included in a program header for a shell script?
Which of the following elements should be included in a program header for a shell script?
Signup and view all the answers
The three basic types of statements for controlling flow in a script are two-way branching, loop statements, and multiway branching.
The three basic types of statements for controlling flow in a script are two-way branching, loop statements, and multiway branching.
Signup and view all the answers
What is the purpose of using comments in a script?
What is the purpose of using comments in a script?
Signup and view all the answers
A ________ statement in programming allows a script to execute commands repeatedly.
A ________ statement in programming allows a script to execute commands repeatedly.
Signup and view all the answers
Match the following programming concepts with their descriptions:
Match the following programming concepts with their descriptions:
Signup and view all the answers
In the provided example script, what is the 'secretcode' variable set to?
In the provided example script, what is the 'secretcode' variable set to?
Signup and view all the answers
The 'while' statement continues executing as long as its condition is true.
The 'while' statement continues executing as long as its condition is true.
Signup and view all the answers
What is a program header?
What is a program header?
Signup and view all the answers
What is the purpose of command substitution in a shell?
What is the purpose of command substitution in a shell?
Signup and view all the answers
The 'readonly' command allows for the assignment of new values to a variable after it has been defined.
The 'readonly' command allows for the assignment of new values to a variable after it has been defined.
Signup and view all the answers
What command is used to read one line from standard input and assign it to variables?
What command is used to read one line from standard input and assign it to variables?
Signup and view all the answers
The command used to remove a variable or function is called ______.
The command used to remove a variable or function is called ______.
Signup and view all the answers
Match the following commands with their purposes:
Match the following commands with their purposes:
Signup and view all the answers
What does the 'shift[N]' command do in a shell script?
What does the 'shift[N]' command do in a shell script?
Signup and view all the answers
The 'set' command can only be used with arguments; it has no functionality without them.
The 'set' command can only be used with arguments; it has no functionality without them.
Signup and view all the answers
What is the purpose of the 'export' command in a shell?
What is the purpose of the 'export' command in a shell?
Signup and view all the answers
Which command is used to add execute permission to a script file?
Which command is used to add execute permission to a script file?
Signup and view all the answers
Shell programs are typically stored in ordinary UNIX files.
Shell programs are typically stored in ordinary UNIX files.
Signup and view all the answers
What is the purpose of the shebang (#!) at the beginning of a shell script?
What is the purpose of the shebang (#!) at the beginning of a shell script?
Signup and view all the answers
The command to execute a shell script directly is $ ________ script_file.
The command to execute a shell script directly is $ ________ script_file.
Signup and view all the answers
Match the following shell variable names with their descriptions:
Match the following shell variable names with their descriptions:
Signup and view all the answers
What is a shell variable primarily used for?
What is a shell variable primarily used for?
Signup and view all the answers
What is it called when command line arguments are passed to a shell script?
What is it called when command line arguments are passed to a shell script?
Signup and view all the answers
What will happen if a variable is defined as 'name=John*'?
What will happen if a variable is defined as 'name=John*'?
Signup and view all the answers
The command 'name='John Doe'' is valid for assigning a variable in the shell.
The command 'name='John Doe'' is valid for assigning a variable in the shell.
Signup and view all the answers
What is indicated by the variable 'TMOUT=0'?
What is indicated by the variable 'TMOUT=0'?
Signup and view all the answers
The command to display all environmental variables is '______'.
The command to display all environmental variables is '______'.
Signup and view all the answers
Match the following environmental variables with their descriptions:
Match the following environmental variables with their descriptions:
Signup and view all the answers
Which of the following is a correct method for defining a shell variable with spaces?
Which of the following is a correct method for defining a shell variable with spaces?
Signup and view all the answers
Environmental variables can only be read and not modified.
Environmental variables can only be read and not modified.
Signup and view all the answers
What is the output of 'echo $name' if 'name' was set to 'John Doe'?
What is the output of 'echo $name' if 'name' was set to 'John Doe'?
Signup and view all the answers
What is the purpose of the readonly
command in the shell?
What is the purpose of the readonly
command in the shell?
Signup and view all the answers
The command unset [name-list]
can be used to add new variables to the shell.
The command unset [name-list]
can be used to add new variables to the shell.
Signup and view all the answers
What does the command shift[N]
achieve in a shell script?
What does the command shift[N]
achieve in a shell script?
Signup and view all the answers
The export
command is used to make variables available to ________ executed from the shell.
The export
command is used to make variables available to ________ executed from the shell.
Signup and view all the answers
Match the following shell commands with their purposes:
Match the following shell commands with their purposes:
Signup and view all the answers
What is the primary function of command substitution?
What is the primary function of command substitution?
Signup and view all the answers
The set
command, when executed without any arguments, displays the names of all shell variables and their current values.
The set
command, when executed without any arguments, displays the names of all shell variables and their current values.
Signup and view all the answers
How can you remove a variable or function defined in the shell?
How can you remove a variable or function defined in the shell?
Signup and view all the answers
What is displayed by the variable $0 within a shell script?
What is displayed by the variable $0 within a shell script?
Signup and view all the answers
The $* variable displays the same values as the $@ variable when passing arguments to a shell script.
The $* variable displays the same values as the $@ variable when passing arguments to a shell script.
Signup and view all the answers
How many command line arguments can you access directly using positional parameters in a shell script?
How many command line arguments can you access directly using positional parameters in a shell script?
Signup and view all the answers
The command line argument that references all the parameters passed is represented by _______.
The command line argument that references all the parameters passed is represented by _______.
Signup and view all the answers
Match the shell script command with its description:
Match the shell script command with its description:
Signup and view all the answers
What happens to the arguments after executing 'shift' in a shell script?
What happens to the arguments after executing 'shift' in a shell script?
Signup and view all the answers
You can use the command 'shift 3' to remove the first three arguments from the list in a shell script.
You can use the command 'shift 3' to remove the first three arguments from the list in a shell script.
Signup and view all the answers
What command would you use to print the second command line argument in a shell script?
What command would you use to print the second command line argument in a shell script?
Signup and view all the answers
What is NOT included in a program header for a shell script?
What is NOT included in a program header for a shell script?
Signup and view all the answers
Comments in a shell script are optional and can be omitted without impact.
Comments in a shell script are optional and can be omitted without impact.
Signup and view all the answers
What type of statement allows commands to execute repeatedly in a shell script?
What type of statement allows commands to execute repeatedly in a shell script?
Signup and view all the answers
The three basic types of statements for controlling flow in a script are: two-way branching, multiway branching, and ________.
The three basic types of statements for controlling flow in a script are: two-way branching, multiway branching, and ________.
Signup and view all the answers
Match the following elements with their purposes in a program header:
Match the following elements with their purposes in a program header:
Signup and view all the answers
Which of the following best describes 'two-way branching'?
Which of the following best describes 'two-way branching'?
Signup and view all the answers
The while statement in a shell script continues executing as long as its condition is false.
The while statement in a shell script continues executing as long as its condition is false.
Signup and view all the answers
What is the purpose of putting comments in a shell script?
What is the purpose of putting comments in a shell script?
Signup and view all the answers
Study Notes
Introduction to BASH Scripting
- Shell scripting introduces the concept of shell programming.
- Shell programs are executed by a shell.
- Shell variables are used for reading and writing storage.
- Command line arguments are passed to shell programs.
- Command substitution is a concept.
- Basic coding principles are described.
- Shell scripts are written and discussed.
- Commands and primitives are covered.
Shell Script
- A shell script is a shell program.
- Shell commands are executed by the shell.
- This script is stored in a UNIX file.
- Shell variables provide read/write storage for users and programmers.
- They are scratch pads for tasks.
- Program control flow commands define sequential order of execution.
Running a Bourne Shell Script
- Make the script file executable by adding execute permission.
- Example command:
chmod u+x script_file
- Run the script using the
bash
command. - Example command:
/bin/bash script_file
- Force the current shell to execute the script in the Bourne Again Shell.
- Example command:
#!/bin/bash
Shell Variables
- CDPATH contains directory names searched by
cd
. - EDITOR is the default editor for programs like email.
- ENV contains paths to configuration files.
- HOME describes the user's home directory.
- MAIL is the name of the user's mailbox file.
- MAILCHECK specifies interval for checking the mailbox.
- PATH contains directories for finding external commands.
- PPID is the parent process ID.
- PS1 is the primary prompt, usually
$
. - PS2 is the secondary prompt, typically
>
. - PWD represents the current working directory.
- TERM is the terminal type.
Read-only Shell Variables
-
$0
: Program name. -
$1-$9
: Command line arguments (1-9). -
$*
: All command line arguments (quoted). -
$@
: All command line arguments (individually quoted). -
$#
: Total arguments. -
$$
: Process ID (PID). -
$?
: Exit status of last command. -
$!
: PID of last background process.
Display Shell Variables
- Shell variables are shown by using
set
. - Variables and their respective values are displayed.
Reading and Writing Shell Variables
- Assign values to variables using
variable1=value1[variable2=value2...variableN=valueN]
. - No spaces before or after the equals signs.
Command Substitution
- Enclose a command in back quotes to execute and substitute with the output.
- Example:
echo "$(date)"
Exporting Environment
- Use
export
to copy variable values to other commands. -
export name
copies the value ofname
to other commands. - Example:
name="John Doe"; export name
.
Resetting Variables
- Use
unset
to reset variables. - Remove the variable or function corresponding to the names in the list.
- Example command:
unset name place
Creating Read-Only Defined Variables
- Use
readonly
to prevent assignment of new values to variables. - This prevents modification.
- Example use:
readonly name place
Reading from Standard Input
- Use
read variable-list
to read a line from standard input. - Assign words in the line to variables.
Passing Arguments to Shell Scripts
-
shift [N]
: Shifts command line arguments N positions to the left. -
set [options] [argument-list]
: Sets values of positional arguments. (set
alone displays current variable values).
Special Characters for the echo Command
- Special characters to use within the
echo
command, including:\b
,\c
,\f
,\n
,\r
,\t
,\v
,\\
,\ON
.
Passing Arguments to Shell Scripts: Examples
- Examples demonstrate how to pass arguments to shell scripts.
Comments and Program Headers
- Use comments to explain sections of code.
- Include program headers for every script, including file name, author, date written, date modified, purpose and algorithm.
Program Control Flow Commands
- Used to control the order of program statements.
- Examples: branching, loops.
The if Statement (Two-way branching)
- Conditional statements based on an expression.
- Implement conditional logic in scripts.
- Syntax:
if
expressionthen
commands
Operators for the test Command
- Operators for the
test
command (or[ ]
), including file testing (e.g.,-d file
,-f file
,-r file
,-s file
,-t [filedes]
,-w file
,-x file
) and integer/string testing.
The for Statement
-
for variable [in argument-list] do command-list done
: Loops through items in a list.
The while Statement
- Repeats commands as long as an expression is true.
The until Statement
- Repeats commands while an expression is false.
The break and continue Statements
-
break
: Exits the loop immediately. -
continue
: Skips the rest of the current iteration.
The case Statement
- Used for multi-way branching.
- Example use:
case "$option" in d|D) date;; 1|L) ls;; esac.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Test your knowledge with this quiz on shell scripting essentials. From environmental variables to command-line arguments, this quiz covers key concepts and commands you'll encounter while working in the shell. Perfect for beginners and those looking to refresh their skills!