1. Piping and redirection
File descriptor 0: standard input for a program
File descriptor 1: Standard output
File descriptor 2: standard error output
> Operator: Redirects the standard output to a file.
>> operator: Attaches the output to a file.
| operator: the connection process.
2. Shell as a programming language
2.1 Creating a Script
Comments begin with a # symbol.
#! The character that tells the system to follow it on the same line is the procedure used to execute this file.
2.2 Set the script to execute
chmod +x First
3. Shell syntax
3.1 Variables
By default, all variables are treated as strings and stored as strings. If the strings contain spaces, they must be enclosed in quotation marks.
In the shell, access its contents by adding a $ sign to the variable name. When assigning a value to a variable, only the variable name is required.
[Email protected]:~/mypro$ salutation=Hello[email protected]-virtualbox:~/mypro$ echo $salutationhello [email protected]-virtualbox:~/mypro$ salutation ='Hello World'Salutation: No command found [email protected]-virtualbox:~/mypro$ salutation='Hello World'[email protected]-virtualbox:~/mypro$ Echo $salutationhello world[email protected]-virtualbox:~/mypro$ salutation=7+5[email protected]-virtualbox:~/mypro$ Echo $salutation7+5
Use the Read command to assign the user's input to a variable.
[Email protected]:~/mypro$ read Salutation Ni hao, Xiao Ming[email protected]-virtualbox:~/mypro$ Echo $sal Utationni Hao, Xiao Ming
Similar to PHP syntax, a variable with a $ character is placed in double quotation marks, and the program executes to this line by replacing the variable with its value;
If you put it in single quotation marks, there is no substitution phenomenon.
Adding a \ Character before the $ character cancels its special meaning.
#!/bin/SHMyVar="Hi There"Echo$myvarEcho "$myvar"Echo '$myvar'Echo\ $myvarEchoEnter some textread myvarEcho '$myvar'Now equals $myvarexit0~
Execute this script:
[Email protected]:~/mypro$./myvar. SH Hi Therehi there$myvar$myvarenter some textgood bye $myvar now equals good bye
Linux Programming (ii) Shell programming