[WIP]Unix / Linux Shell Programming
Created: 2022/12/17
Finished: 2022/12/20
https://www.tutorialspoint.com/unix/shell_scripting.htm
What is Shell? | |||||||||||||||||||||
A Shell provides you with an interface to the Unix system. It gathers input from you and executes programs based on that input. When a program finishes executing, it displays that program's output. Shell is an environment in which we can run our commands, programs, and shell scripts. There are different flavors of a shell, just as there are different flavors of operating systems. Each flavor of shell has its own set of recognized commands and functions. |
|||||||||||||||||||||
Shell Prompt |
$ // Bourne shell % // C shell The prompt, $, which is called the command prompt, is issued by the shell. While the prompt is displayed, you can type a command. Shell reads your input after you press Enter. It determines the command you want executed by looking at the first word of your input. A word is an unbroken set of characters. Spaces and tabs separate words. |
||||||||||||||||||||
Shell Types |
|
||||||||||||||||||||
Shell Scripts |
suffix: .sh
|
||||||||||||||||||||
Using Variables | |||||||||||||||||||||
A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data. A variable is nothing more than a pointer to the actual data. The shell enables you to create, assign, and delete variables. |
|||||||||||||||||||||
Variable Names |
The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or the underscore character ( _). By convention, Unix shell variables will have their names in UPPERCASE. |
||||||||||||||||||||
Defining Variables |
variable_name=variable_value
|
||||||||||||||||||||
Accessing Values |
To access the value stored in a variable, prefix its name with the dollar sign ($) TEST=ture echo $TEST
|
||||||||||||||||||||
Read-only Variables |
Shell provides a way to mark variables as read-only by using the read-only command. After a variable is marked read-only, its value cannot be changed. TEST=true readonly TEST
or readonly TEST=true
|
||||||||||||||||||||
Unsetting Variables |
unset variable_name
|
||||||||||||||||||||
Variable Types |
|
||||||||||||||||||||
Special Variables | |||||||||||||||||||||
|
|||||||||||||||||||||
Command-Line Arguments |
The command-line arguments $1, $2, $3, ...$9 are positional parameters, with $0 pointing to the actual command, program, shell script, or function and $1, $2, $3, ...$9 as the arguments to the command. Following script uses various special variables related to the command line − #!/bin/sh echo "File Name: $0" echo "First Parameter : $1" echo "Second Parameter : $2" echo "Quoted Values: $@" echo "Quoted Values: $*" echo "Total Number of Parameters : $#" output: $./test.sh Zara Ali File Name : ./test.sh First Parameter : Zara Second Parameter : Ali Quoted Values: Zara Ali Quoted Values: Zara Ali Total Number of Parameters : 2
|
||||||||||||||||||||
Special Parameters $* and $@ |
There are special parameters that allow accessing all the command-line arguments at once. $* and $@ both will act the same unless they are enclosed in double quotes, "". Both the parameters specify the command-line arguments. However, the "$*" special parameter takes the entire list as one argument with spaces between and the "$@" special parameter takes the entire list and separates it into separate arguments. |
||||||||||||||||||||
Exit Status | |||||||||||||||||||||
Using Arrays | |||||||||||||||||||||
Defining Array Values |
array_name[index]=value // bash array_name=(value1 ... valuen) // starting with 1
|
||||||||||||||||||||
Accessing Array Values |
${array_name[index]} // access all values ${array_name[*]} ${array_name[@]} // or $array_name
|
||||||||||||||||||||
Basic Operators | |||||||||||||||||||||
Bourne shell didn't originally have any mechanism to perform simple arithmetic operations but it uses external programs, either awk or expr. | |||||||||||||||||||||
Arithmetic Operators |
|
||||||||||||||||||||
Relational Operators |
Bourne Shell supports the following relational operators that are specific to numeric values. These operators do not work for string values unless their value is numeric.
|
||||||||||||||||||||
Boolean Operators |
|
||||||||||||||||||||
String Operators |
|
||||||||||||||||||||
File Test Operators | TODO | ||||||||||||||||||||
Decision Making | |||||||||||||||||||||
The if...else statements |
if [ expression 1 ] then Statement(s) to be executed if expression 1 is true elif [ expression 2 ] then Statement(s) to be executed if expression 2 is true elif [ expression 3 ] then Statement(s) to be executed if expression 3 is true else Statement(s) to be executed if no expression is true fi If expression is a shell command, then it will be assumed true if it returns 0 after execution. If it is a Boolean expression, then it would be true if it returns true.
|
||||||||||||||||||||
case word in pattern1) Statement(s) to be executed if pattern1 matches ;; pattern2) Statement(s) to be executed if pattern2 matches ;; pattern3) Statement(s) to be executed if pattern3 matches ;; *) Default condition to be executed ;; esac
|
|||||||||||||||||||||
Shell Loops | |||||||||||||||||||||
The while Loop |
while command do Statement(s) to be executed if command is true done
|
||||||||||||||||||||
The for Loop |
for var in word1 word2 ... wordN do Statement(s) to be executed for every word. done
a=(9 8 7 6 5 4 3 2 1) for i in ${a[*]}; do echo "i=$i" done
|
||||||||||||||||||||
The until Loop |
until command do Statement(s) to be executed until command is true done
|
||||||||||||||||||||
The select Loop |
select var in word1 word2 ... wordN do Statement(s) to be executed for every word. done
|
||||||||||||||||||||
The break statement | The break statement is used to terminate the execution of the entire loop, after completing the execution of all of the lines of code up to the break statement. It then steps down to the code following the end of the loop. | ||||||||||||||||||||
The continue statement | The continue statement is similar to the break command, except that it causes the current iteration of the loop to exit, rather than the entire loop. | ||||||||||||||||||||
Shell Substitutions | |||||||||||||||||||||
The shell performs substitution when it encounters an expression that contains one or more special characters. | |||||||||||||||||||||
The Escape sequences |
|
||||||||||||||||||||
Command Substitution |
`command`
|
||||||||||||||||||||
Variable Substitution |
|
||||||||||||||||||||
Quoting Mechanisms | |||||||||||||||||||||
The shell performs substitution when it encounters an expression that contains one or more special characters. * ? [ ] ' " \ $ ; & ( ) | ^ < > new-line space tab
|
|||||||||||||||||||||
|
|||||||||||||||||||||
IO Redirections | |||||||||||||||||||||
Output Redirection |
command > file
|
||||||||||||||||||||
Input Redirection |
command < file
|
||||||||||||||||||||
Here Document |
A here document is used to redirect input into an interactive shell script or program. command << delimiter
document
delimiter
|
||||||||||||||||||||
Discard the output |
$ command > /dev/null
discard both STDOUT and STDERR $ command > /dev/null 2>&1
0: STDIN 1: STDOUT 2: STDERR
|
||||||||||||||||||||
Shell Functions | |||||||||||||||||||||
Creating Functions |
function_name () {
list of commands
}
|
||||||||||||||||||||
Pass Parameters to a Function | You can define a function that will accept parameters while calling the function. These parameters would be represented by $1, $2 and so on. | ||||||||||||||||||||
Returning Values from Functions |
If you execute an exit command from inside a function, its effect is not only to terminate execution of the function but also of the shell program that called the function. Based on the situation you can return any value from your function using the return command whose syntax is as follows − return anything
Here code can be anything you choose here, but obviously you should choose something that is meaningful or useful in the context of your script as a whole. |
||||||||||||||||||||
Nested Functions | One of the more interesting features of functions is that they can call themselves and also other functions. A function that calls itself is known as a recursive function. | ||||||||||||||||||||
Function Call from Prompt |
|
||||||||||||||||||||
Manpage Help | |||||||||||||||||||||
$man command
|
|||||||||||||||||||||