First, what is the shell
The shell is both an executable program and a programming language. as an executing program, it interactively interprets and executes user-entered commands , and is a bridge between the user and the Unix/linux; as a programming language, it can define variables and parameters to a certain extent with the ability to program . Although not part of the system kernel, it invokes most of the system's core functions to execute programs, create files, and coordinate the operation of individual programs in parallel. Therefore, the shell is the most important utility for the user.
Second, Shell type
Iii. classification of shell scripts in programming languages
Weak types in bash bring three changes relative to strongly typed variables
1. No declaration of variables
2. Declaration and assignment can be performed simultaneously
3. Variables need to refer to $
For string assignments, because the variables in bash do not need to be declared, they are assigned directly, and C needs to be declared. Bash wins
For numeric calculations, it is necessary to assign values, except that bash can assign values to declarations and assignments at the same time. Win bash a little bit.
int a intb int c c=a+b print C
Declare-i C
Declare-i A
Declare-i b
C=a+b echo $c or c=$[a+b]
It is only usually written as c=$[$a + $b] in order to recognize other string variable references. Variable reference this may be another weakness of the weakly typed language.
But Bash's support for numerical calculations is far less than C
[Email protected]/tmp] #a =1[[email protected]/tmp] #b =2[[email protected]/tmp] #c = $a + $b [[email protected]/tmp] #echo $ C1+2[[email protected]/tmp] #declare-i a=1[[email protected]/tmp] #declare-i b=2[[email protected]/tmp] #c = $a + $b [[ Email protected]/tmp] #echo $c 1+2[[email protected]/tmp] #declare-i c[[email protected]/tmp] #c = $a + $b [email protected] /tmp] #echo $c 3----------------------C does not convert numeric type, unrecognized 1+2
Process oriented Object oriented
Process-oriented programming language
Program: command-centric to organize code, data Services to code
Compiled language explanatory language
Explain execution, and do not build. class files like Java, C, and then compile
four, shell script characteristics and application
Features: Although Shell programming is not as powerful as Java, they can easily handle objects such as file directories, such as: printing a string requires only one echo, rather than needing to define a framework such as classes and objects like Java. This is also making up for the speed flaws of the explanatory language.
The uses of shell scripts are:
Automating common commands
Perform system administration and troubleshooting
Create a simple application
Working with text or files
v. Syntax formatting and execution conditions for shell scripts
Syntax format
The first line must declare the implicit interpretation environment (Shabang mechanism): #!/bin/bash, then it executes by default with the current shell to interpret the script (ie: $SHELL environment variable)
Execution mode:
Run as executable (give execute permission, specify the execution path of the script on the command line)
Note: Any executable program, if it is to be executed, must be in the PATH environment variable to ensure that the root can find the program, otherwise it cannot execute
Run as a parameter specifying a specific interpreter
Vi. syntax basics in shell scripts
Variable
Of course it's not about strings, integers or anything, but the variables in bash can be understood as strings. There are four types of variables in bash, which are environment variables, local variables, positional variables, and special variables.
Local variables
Environment variables PATH PWD HOSTNAME history
Definition method: Export VALUE
Declare-x VALUE
Local variables
Position variable $1,$2
Special variable $# $* [email protected] $? $
Operation
Arithmetic operations
Let a= $b + $c
Let A+=1
a=$[$a + $b]
Logical operations
&& | | !
Note: If more than one command appears in a logical relationship, it is recommended that you add parentheses
Condition test
String test
~=: General use of [[]] matching mode
==
>
<
! =: The above comparison is the size of the ASCII code corresponding to the string
- Z: string is empty, empty is true
-N:
Numerical test
-eq
-ne
-gt
-ge
-lt
-le
File test
-E: Whether the file exists
-F: Exists and is normal file
-S: Whether the file is empty
Note that the test is to be added []
Combination Test
-a-o-!
[!-r/tmp/file1-a!-w/tmp/file1]
Note: For string tests and numeric tests, the same applies to this format
1, if you want to use globbing or regular expressions to use [[]], generally used in the string ~=.
2, can not find error when the change [] or [[]]
Note: The-A in the combination test,-o,! The logic commonly used between test conditions, and &&,| in logical operations | General use of judgment in command execution results
Command Reference
$() ``
Variable substitution
$VALUE
[[Email protected] ~] #a =1[[email protected] ~] #b =2[[email protected] ~] #ab =3[[email protected] ~] #echo $ab 3[[email Protected] ~] #echo ${a}b1b[[email protected] ~]#
This article is from the "dmwing" blog, make sure to keep this source http://dmwing.blog.51cto.com/11607397/1839019
Shell Script Basics