I. Script structure
The script for the shell under Linux is to write a series of command sequences in a text file, and this text file can be executed. Development efficiency increases relative to the command line. So his architecture has 2 parts that make up #! and command sequences. where #! Indicates which parser is used by this script to parse. For example:
Two. Basic knowledge
2.1 Variables
The shell allows the user to set and use their own variables, which can be numbers or strings, and the user does not need to specify their type or be defined before use.
For example:
1 #!/bin/bash23 #2.24 a="Hello word" 5 b=56"Ais" $a7" Bis" $b
2.2 Parameters
As with C programs, command-line arguments can also be used in shell
$#: The number of command-line arguments passed in the script
$*: All command line parameter values, with spaces between individual parameter values
$: Command itself (shell file name)
$: Command itself (shell file name)
$: First command-line argument
$: Second command-line argument
For example:
#!/bin/bash#2.3Echo"Number of var:"$ #echo"Value of VARs"$*Echo"Name of script"$0Echo"Value of Var1"$1Echo"Value of Var2"$2
Terminal:./bash.sh a 3, the result can be verified
2.3 Mathematical calculations
The shell provides a specialized command expr to calculate mathematical expressions, such as expr 5 +1, but you must also use the inverse when you want to assign the results of the calculation to other variables. var= ' Expr 20/10 '. For example:
#!/bin/bash#2.4var1=tenvar2=var3= ' expr $var 2/ $var 1 ' var4= ' expr $var 2 + $var 1 ' echo $var 3echo $var 4
2.5 Flow Control Statements
2.5.1
The most basic Process Control statement in a shell script is if-then, using the format:
if [condition] (special reminder: the left and right sides of square brackets must have spaces)
Then
Commands
Else
Commands
Fi
For example:
#!/bin/bash#2.5. 1 var=Tenif [$var5 ]then "Thevalue is Greater than 5"fi
2.5.2
Shell provides a for command to create a C language program similar to the for
Statement-like loops. Use format:
for Var in list
Do
Commands
Done
For example:
#!/bin/bash#2.5. 2 list="Sun Mon Tue Wed Thur Fri Sat" for in $listdo< /c10> Echo $daydone
2.5.3
The shell provides a while command to create a language similar to that in C programming
The same loop as the while statement. Use format:
While condition
Do
Commands
Done
For example:
#!/bin/bash while [$var0 ]do echo $var var= ' expr $var1' done
Shell Programming Technology-fundamentals