1/45
Flashcards covering key vocabulary and concepts from the Unix - Shell Programming lecture, including shell basics, variables, command-line parameters, arithmetic, conditional statements (if, case), and looping constructs (for, while, until).
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Shell Programming
The process of writing scripts, which are sequential lists of commands, using the shell as a programming language to automate tasks.
Shell Program
A program consisting of a sequential list of commands, effectively a listing of other programs to run in order.
Comment (Shell Script)
A line in a shell script starting with the '#' symbol, which is not processed by the shell and serves only to explain the code to humans.
Shebang Line
The first line of a shell executable, starting with '#!', which specifies which interpreter (e.g., /bin/bash, /usr/bin/perl) should be used to run the script.
Return Value (Exit Code)
An integer value returned by every program and shell to indicate success (0) or failure (non-zero, typically 1) of its execution.
Variable (Shell)
A container used to hold values in a shell script, supporting combinations of letters, numbers, and '', must start with a letter or '', and is case-sensitive.
Setting Variables (Shell)
Done using the '=' operator (e.g., d=date
), does not require prior declaration, and all variables are treated as strings of characters.
Reading Variables (Shell)
Done by prefixing the variable name with '$' (e.g., $d
or ${d}
), which replaces the variable with its stored value.
Braced Variable Expansion (${VAR})
Used to explicitly define the boundaries of a variable name when it is directly followed by other characters, preventing the shell from looking for a longer variable name (e.g., ${Color}Sky
).
read command
Reads values from standard input and assigns them to one or more specified variables.
Single Quotes ('')
Quotation marks that prevent the shell from interpreting any characters or variables within them; literally prints the enclosed content.
Double Quotes ("")
Quotation marks that allow for variable interpretation (expansion) within them, but generally prevent other special character interpretation.
Backticks (``)
Quotation marks that interpret variables and treat the enclosed content as a command to run, returning the output of that command (command substitution).
Command Line Parameters
Values passed to a shell script at the time of its execution, known before the script starts running, unlike user input.
$0 (Special Variable)
A special shell variable that holds the name of the running program (script).
$1-$9 (Special Variable)
Special shell variables that hold the first nine command-line arguments passed to the program.
$* (Special Variable)
A special shell variable that holds all of the command-line arguments as a single string.
$# (Special Variable)
A special shell variable that holds the total number of command-line arguments passed to the program.
Arithmetic in Shell
Variables containing numbers can be treated as numerical values and subjected to operations like addition, subtraction, multiplication, division, and modulo.
$(( )) Command
A shell construct used for performing integer arithmetic operations like +
, -
, *
, /
, and %
(e.g., $(( 1 + 6 ))
).
Control Flow Statements
Programming constructs in shell scripts that determine the order in which statements are executed, based on conditions or for repetitive tasks.
Conditional Statements
Statements in shell scripts that allow certain commands to be performed only if specific conditions are true.
test Command
A shell command used to check if certain conditions are true, often for comparisons or file system checks.
-eq (test condition)
A condition used with test
for numerical equality comparison.
-ne (test condition)
A condition used with test
for numerical inequality comparison.
-lt (test condition)
A condition used with test
for numerical 'less than' comparison.
-le (test condition)
A condition used with test
for numerical 'less than or equal to' comparison.
-gt (test condition)
A condition used with test
for numerical 'greater than' comparison.
-ge (test condition)
A condition used with test
for numerical 'greater than or equal to' comparison.
-d (test condition)
A condition used with test
to check if a file is a directory.
-e (test condition)
A condition used with test
to check if a file exists.
-f (test condition)
A condition used with test
to check if a file is a normal file.
-s (test condition)
A condition used with test
to check if a file is non-empty.
! (test condition)
A logical NOT operator used with test
to negate the next condition check.
-a (test condition)
A logical AND operator used with test
to combine two conditions, both of which must be true.
-o (test condition)
A logical OR operator used with test
to combine two conditions, where at least one must be true.
[] (Shortcut for test)
A common shortcut syntax for the test
command, enclosed in square brackets (e.g., [ -f tFile ]
is equivalent to test -f tFile
).
if then fi (Conditional Statement)
A basic conditional structure that executes a block of statements only if a given condition is true.
if then else fi (Conditional Statement)
A conditional structure that executes one block of statements if a condition is true, and a different block if the condition is false.
if then elif else fi (Conditional Statement)
A conditional structure that allows for multiple conditions to be checked sequentially. If the first condition is false, the next elif
condition is checked, and so on, with an optional else
for all other cases.
case esac (Conditional Statement)
A multi-way branch conditional statement that compares a string against multiple patterns and executes the statements corresponding to the first matching pattern.
Loops (Control Flow)
Programming constructs used to repeatedly execute a block of statements, either for a set number of times, while a condition is true, or until a condition is false.
for Loop (Shell)
A loop construct that iterates through a list of items, assigning each item to a variable and executing a block of statements for each item.
for Loop (Special Form)
A specific form of the for
loop, without an explicit list, which iterates through all of the command-line arguments by default.
while Loop (Shell)
A loop construct that repeatedly executes a block of statements as long as a given condition remains true.
until Loop (Shell)
A loop construct that repeatedly executes a block of statements until a given condition becomes true.