Recently saw the shell script. The shell script has been used many times in a lot of ready-made kits. In short, the effect of this thing is to integrate a series of operations.
• Integration makes a set of work more modular and standardized.
• Batch processing is much faster than manual operation.
The shell script is a script, but it is quite different from some regular scripts. The main reason is that shell scripts require consolidation commands.
To see a paragraph:
#!/bin/Bashpath= 'pwd'echo'. /.. /"path= 'pwd'echo $path
Output Result:
/home/rockderia/Desktop/shells/git/rock_shells/home/rockderia/Desktop/shells
As seen in the code above, you can directly attach the CD ". /.. /"Such command-line statements are directly embedded in the script. So the shell script interpreter is doomed to be more strange.
Let's go to 1.1 to learn some simple shell syntax, not to say that you can write a beautiful shell, or at least see it again. SH Don't be cute.
Specification Comments Variable Print [Reference] Input Array File [REDIRECT] [Logic Control] [Shake pot]
1. Specification
In general, the shell script we use is the bash shell, the script file extension is. sh, the first line needs to write #!/bin/bash.
Executed, it should be done in a manner such as./xxx.sh
2. Notes
A line beginning with #
# This line is a note
3. Variables
Declaration of a variable
When declaring, the format is the variable name =
Note that there can be no spaces between the variable name and the equals sign, and the variable name is not preceded by $
Use of variables
This is similar to PHP, for example
Name="rockderia"Echo
Types of variables
There are only two types of generic variables that are not distinguished by the general declaration of the string and the file, because their operators are different, so the interpreter will revert to the type based on which operator is used.
4. Printing
echo can be used to print directly
There is also a way to print the printf function, where the edge supports formatting operations such as placeholders.
5. Transfer of parameters
When you execute a script, you can send arguments like
./one. SH Ten
So the value of "one" is $10.
6. Enter
Read can accept console input data
Read number
7. Arrays
Declaration of the array:
Array_list= (1,2,3,4,5)
Take value
${ARRAY_LIST[1]}
The length of the array
${#arrat_list [*]}
8. Documents
Assign the address of a file directly to a variable, such as
file="./one.txt"
9. Redirection
You can redirect the contents of Echo directly to a file variable
Echo " into file " > $file
10. Logic Control
Because there is no concept of type in the shell for files, strings, and numbers, it is the type of the variable that you will default on when you manipulate it according to your operation interpreter. Like you do.
" sum= $sum +1 "
strings and files are not possible to perform + such operations, so the default is the number
Another example
If [-e $file]
This is the logical operator of the file-e so the default $file variable is a file.
To write an instance:
If logic
Read number_1
Read number_2
if test $number _1= $number _2
Then
echo "Two numbers are equal";
Else
echo "two numbers unequal";
Fi
If Test-e./files/rock.txt
Then
echo "File exists";
Else
echo "file does not exist";
Fi
Loop structure
sum=0; Temp=0; I=1; for((i=1; i<= -; i++)) Do Let"temp= $i * $i"; Let"sum+= $temp"; DoneEcho "The result is"$sum; Filearray=`ls./files/*. txt '; for file in $filearraydoecho $filedone
11. Shake the Pan
Finally, I wish you ...
Because other execution statements can be arbitrarily inserted into the shell script, we can sometimes ask the high-level language or other familiar script to perform some operations. This is where the shell is powerful and flexible.
For example, we can write a C + + file
#include <stdio.h>typedefChar*str;intMainintargcChar*argv[]) {STR*Head= &argv[0]; if(ARGC <2) return-1; Head++; for(inti =1; i < argc; i++) {printf ("%d-", i); printf (*Head); printf ("\ n"); Head++; }}
The above-mentioned documents print out all the accepted parameters, of course, we can use C + + to perform more functions, after all, parameters have been passed in. We compile it into an executable file. Out. Next we write the shell script
#!/bin/bashparam1="home"param2="my" . /CPP/a.out ${param1} ${param2}echo"end"
Just like this, we execute this shell script to get the following result:
1-home2-myend
Blogger's Test code entry: Https://github.com/RockDeria/rock_shells.git
Shell Scripting Quick Start