I. Overview
The shell is a program written in C that provides access to the operating system kernel services through a shell user. It is similar to DOS command and later cmd.exe. The shell is both a command language and a programming language.
Shell script is a scripting program written for the shell.
Shell programming generally refers to Shell scripting, not the development of the shell itself.
Shell programming is like Java and PHP programming, as long as there is a text editor that can write code and a script interpreter that can explain execution .
There are many shell types in Linux, one system can have multiple shells, and you can view the shell installed in the system with the Cat/etc/shells command.
Bash is widely used in daily work due to its ease of use and free. At the same time,Bash is the default Shell for most Linux systems.
Two. In Linux, make
Create a new file hello.sh using the VI editor. The extension does not affect script execution, which is known as the name. For example, using PHP to write shell scripts, the extension is used. php.
#!/bin/"Hello world! " "
#! 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. Execution of Shell scripts:
chmod +x./hello.sh #使脚本具有执行权限. /hello.sh #执行脚
Direct write Hello.sh,linux system will go to the PATH to find there is no call hello.sh. Use the./hello.sh to tell the system that it is looking in the current directory. You can also run as an interpreter parameter. Run the interpreter directly, whose parameters are the file names of the shell scripts, such as:
/bin/sh/root/hello.sh/bin/php test.php
This way you run the script without specifying the interpreter information in the first line, which is neither written nor effective.
Three. Shell variables
1. Syntax format
variable = value, such as: Your_name= "itcast.cn" NOTE: There can be no spaces between the variable name and the equals sign, and the name of the variable names should follow the following rules:
A. The first character must be a letter (a-z,a-z)
B. Cannot have spaces in the middle, you can use the underscore (_)
C. Punctuation cannot be used
D. You cannot use keywords in bash (you can view reserved keywords with the help command)
2. Use of variables
Use a defined variable, as long as you precede the variable name with $.
Your_name="itcast.cn"echo $your _nameecho ${your_name}
Curly braces are optional and add no lines, and curly braces are used to help the interpreter identify the bounds of the variable. A defined variable that can be redefined.
Use the readonly command to define a variable as a read-only variable, and the value of a read-only variable cannot be changed. Use the unset command to delete a variable. You cannot delete a read-only variable.
ReadOnly Variable_nameunset variable_name
3. Variable type
local variable local variables are defined in a script or command and are valid only in the current shell instance, and other shell-initiated programs cannot access local variables.
Environment variables All programs, including shell-initiated programs, can access environment variables, and some programs require environment variables to keep them running properly. You can use the SET command to view the current environment variables.
Shell variables shell variables are special variables that are set by the shell program. Some of the shell variables are environment variables, some of which are local variables that guarantee the shell's normal operation.
4.shell parameter Passing
When you execute a Shell script, you can pass parameters to the script. The format for getting parameters within a script is:
$n. n represents a number, 1 is the first argument to execute the script, 2 is the second argument to execute the script, and so on ... $ A represents the current script name.
5. Special characters
$* and [email protected] Difference:
The same point: all parameters that are passed to the script.
Different points: when not included, $* and [email protected] are all in the form of a list of parameters in "$ $" $n. When "" is included, "$*" takes all parameters as a whole, forming an entire string in the form of "$ $ ... $n"; "[email protected]" will separate the various parameters to "$" "$" ... The form "$n" forms a list of parameters.
6.shell operator
The Shell, like other programming voices, supports arithmetic, relational, Boolean, string, and other operators. Native bash does not support simple math operations, but can be implemented by other commands, such as expr. Expr is an expression evaluation tool that uses it to perform evaluation operations on expressions.
For example, two numbers are added:
2 2 ' echo $val
Note: There is a space between the expression and the operator, for example, the 2 + 2 must be written. The complete expression is to be contained, note that it is not a single quotation mark, below the ESC key. For details, please refer to the shell operator for the attached data.
In addition, arithmetic operations can be performed by (()), $[].
count=1((count++)) echo $counta=$ ((1+2)) a=$[1+ 2]
7. Process Control
If Else
if condition1then command1elif condition2then command2 Else Commandnfi
For
//mode one
for N in 1 2 3 do echo $Ndone or for N in 1 2 3 ; do echo $N; done or for N in {1 ... 3 }; do echo $N; Done
//Mode two
for ((i = 0; I <= 5, i++)) doecho "Welcome $i times"
Done
while
#方式一 while expression Do command...done# Mode two I=1 while ((i<=3)) do echo $i Let I+ + done
The #let command is a tool for calculations in BASH that executes one or more expressions and does not need to be added to a variable in the calculation of variables. Self-add operation: let no++ self-subtraction operation: let no--
#方式三
Done
Case
Case inch 12) Command1 command2 ... commandn;; Esac
8. Use of functions
All functions must be defined before they are used. This means that the function must be placed at the beginning of the script until the shell interpreter discovers it for the first time before it can be used. The calling function uses only its name of functions.
[function] funname [()]{action; [returnint;]}
1, can be with function fun () definition, you can also directly fun () definition, without any parameters.
2, the parameter returns, can display the plus return, if not added, will run the result as the last command, as the return value. Return followed by the value n (0-255).
In the Shell, arguments can be passed to a function when it is called. Inside the function body, the value of the parameter is obtained in the form of a $n, for example, $ $ for the first argument, and $ = for the second argument ...
Note that when n>=10, you need to use ${n} to get the parameters.
Funwithparam () {echo"The first parameter is $!"Echo"The second parameter is a $!"Echo"the tenth parameter is $ A!"Echo"the tenth parameter is ${10}!"Echo"the 11th parameter is ${11}!"Echo"The total number of parameters is $#!"Echo"output all parameters as a string $*!"}funwithparam1 2 3 4 5 6 7 8 9 the the
Shell programming (handwritten notes)