1. Shell Introduction 1.1 Shell appearance background
The Shell is both a scripting language and a software for connecting cores and users.
For the graphical interface, the user can start a program by tapping an icon, and for the command line, the user enters the name of a program (which can be viewed as a command) to start a program. The basic process of both is similar, you need to find the installation location of the program on the hard disk, and then load them into memory to run. However, the only operating system kernel (Kernel) that really controls computer hardware (CPU, memory, display, and so on), the graphical interface and command line are just a bridge between the user and the kernel.
Because of the security, complex, cumbersome and so on, the user can not directly contact the kernel (also not necessary), need to develop another program to allow users to directly use the program, the role of the program is to receive the user's actions (click on the icon, Input command), and simple processing, and then passed to the kernel. As a result, there is a layer of "proxy" between the user and the kernel, which simplifies the operation of the user and protects the kernel. The user interface and command line is the other development of the program, that is, this layer of "agent." Under Linux, this command-line program is called the Shell.
1.2 Shell Action
In addition to explaining the commands the user has entered, the Shell passes it to the kernel and can:
- Call other programs, pass data or parameters to other programs, and get the processing result of the program;
- Passing data between multiple programs, the output of one program as input to another program;
- The Shell itself can also be called by other programs.
This shows that the Shell is the kernel, programs and users connected together.
The shell itself does not support many commands, but it can invoke other programs, each of which is a command, which allows the number of shell commands to be infinitely extended, and the result is that the shell is very powerful and fully capable of the daily management of Linux, such as text or string retrieval, Find or create files, automate deployment of large-scale software, change system settings, monitor server performance, send alert messages, crawl Web content, compress files, and more.
Common shells are sh, bash, csh, tcsh, Ash, and so on. This uses the Linux default bash shell (SH), which is not explored for other shells.
2. Shell syntax 2.1 HelloWorld and authorization
Write the shell script, output Hello World
[Email protected] testshell]# vim Myshell. SH #!/bin/bashecho"Hello World!!! "
By the above code, it can be seen that "#!" is a contract tag that tells the system what interpreter the script needs to execute, even if it uses a shell. The echo command is used to output text to a window.
After writing, running the file will prompt you for insufficient permissions
[Email protected] testshell]#./myshell. sh-bash:./myshell. sh: Insufficient permissions
Grant the file X (execute) permission
chmod 744 ./myshell. SH ls -41 June :47 Myshell. SH
Enter the relative path and absolute path of the script file to execute the script
[Email protected] testshell]#./myshell. SH HelloWorld!!! /home/testshell/myshell. SH HelloWorld!!!
2.2 Shell Variable 2.2.1 Shell variable classification
Shell variables are divided into environment variables and user-defined variables (local variables).
Local variables are defined in a script or command, only valid in the current shell instance, other shell-initiated programs cannot access local variables, and environment variables are all programs, including variables that shell-initiated programs can access, and some programs require environment variables to keep them running properly. Shell scripts can also define environment variables when necessary.
2.2.2 Variable definition
In the Bash shell, the value of each variable is a string, and the value is stored as a string, regardless of whether you use quotation marks when assigning a value to a variable.
v1=hellov2='hello' #如果 value contains a white space character, you must enclose it in quotation marks v3=" Hello " #解析变量的话需要加双引号
The assignment number cannot have spaces around it.
2.2.3 Variable Usage
address=" zhongguancun "echo $addressecho ${address} echo" Haidian ${address} Baidu Building "
The use of variables is called by "$var", "${var}", it is recommended to add curly braces to all variables { }
, which is a good programming habit.
2.2.4 the difference between single and double quotation marks
Name=" World !!! " v1='Hello ${name}' v2='Hello ${name} " #解析变量的话需要加双引号 echo"v1 is ${v1}"Echo "v2 is ${v2}"
Execute the above code, with the output you can see the difference between single and double quotation marks
[Email protected] testshell]#./myshell. SH V1 is Hello ${name}v2 was hello world!!!
2.2.5 Assigning the result of a command to a variable
variable= 'ls' # ' non 'echo ${variable}variable=$ (ls) echo ${variable}
The first way to enclose the command in anti-quotation marks, anti-quotes and single quotation marks are very similar, prone to confusion, so it is not recommended to use this method, the second way to $()
enclose the command, the distinction is more obvious, so the recommended use of this way.
2.2.6 Deleting a variable
Name="World"unset nameecho ${name} #无输出
2.2.7 read-only variables
ReadOnly name="World" #声明方式一: Direct declaration of Address=" zhongguancun " readonly Address #声明方式二: The definition is then declared echo"name is ${name} and address is ${ Address}"name="hello" #修改变量值unset Address #删除变量
By executing the output you can see that read-only variables cannot be modified or deleted.
[Email protected] testshell]#./myshell. SH Name is world and address is zhongguancun. /myshell. SH 9 : name:readonly variable. /myshell. SH Ten: Unset:address:cannot unset:readonly variable
2.3 Special variables
In general, variable names can only contain numbers, letters, and underscores, and some variables that contain other characters have special meanings, and such variables are called special variables.
The difference between 2.3.1 $* and [email protected]
$* and [email protected] All represent all parameters passed to a function or script, and are not enclosed by double quotation marks (""), with "$" and "$" ... All parameters are output in the form "$n". But when they are enclosed in double quotation marks (""), "$*" takes all the parameters as a whole and outputs all parameters in the form of "$ $ ... $n"; "[email protected]" separates the various parameters to "$" "$" ... All parameters are output in the form "$n".
Echo "\$*="$*Echo "\"\$*\"=" "$*"Echo "\[email protected]="[email protected]Echo "\ "\[email protected]\" =" "[email protected]"Echo "print each param from \$*" forVarinch$* Do Echo "$var" DoneEcho "print each param from \[email protected]" forVarinch[email protected] Do Echo "$var" DoneEcho "print each param from \ "\$*\"" forVarinch "$*" Do Echo "$var" DoneEcho "print each param from \ "\[email protected]\"" forVarinch "[email protected]" Do Echo "$var" Done
Output
[Email protected] testshell]#./TSBL.SHa b C $*=a b c"$*"=a b c[email protected]=a b c"[email protected]"=a b cprint each param from $*abcprint each param from [email protected]abcprint to Param from"$*"a b cprint each param from"[email protected]"ABC
2.3.2 Exit Status
$? You can get the exit status of the previous command. The so-called exit status is the return result after the last command was executed. Exit status is a number, in general, most of the command execution succeeds returns 0, and the failure returns 1.
2.4 Shell replaces 2.4.1 escape character
If the expression contains special characters, the Shell will replace it. For example, using a variable in double quotes is a substitution, and an escape character is also a substitution.
A=echo"value of a is $a \ n"echo"value of a is $a \ n "
Output
[Email protected] testshell]#./myshell. SH ten \ n
Common escape characters are as follows
2.4.2 variable Substitution
Variable substitution can change its value depending on the state of the variable (whether it is empty, whether it is defined, and so on).
Echo${var:-"Variable is not set"}Echo "1-value of Var is ${var}"Echo${var:="Variable is not set"}Echo "2-value of Var is ${var}"unset varEcho${var:+"The is default value"}Echo "3-value of Var is $var"var="Prefix"Echo${var:+"The is default value"}Echo "4-value of Var is $var"Echo${var:?"Print This message"}Echo "5-value of Var is ${var}"
Results
[Email protected] testshell]#./myshell. SH Variable is not set 1 - value of Var is Variable was not set2 - value of Var was Variableis not set3-Value of Var is this is the default value4 - value of Var is prefixprefix5 -Va Lue of Var is Prefix
(a) Linux shell programming--Introduction, variables