Linux (7) _ shell script programming, __shell Script Programming
I. What is a shell script?
In addition to a command interpreter, shell is also a programming language. A program written in shell is similar to a batch processing program in DOS.
It is an interface between the user and the operating system.
The shell script language is very good at processing text data. Because the configuration files in linux are all text files, the shell script language
It has played a huge role in linux system management.
Ii. Why do I learn shell scripts?
- Automated Management
- Monitoring Management
- Log Data Processing
- Automatic Data Backup
III. Basic syntax
The general file is #! It starts with/bin/bash, which indicates that the file uses the bash syntax and does not have to be set. However, this is a standard.
Shell scripts are in the unit of action. Note # variable supports string and Integer Variables
Vi editingShell script file
Call method:
Sh script-file
You can add the-x option to view the script execution process, which is conducive to debugging.
./Script-file
To execute a shell script in A./way, you must set the File Permission chmod + x filename to an executable file.
The. binfile must be called ./.
Shell variable
Name = string
The string value of the name variable is assigned with no space on either side. It is considered as a command.
Example:
V = centos
Echo = $ v ($ indicates a variable)
Date command:
Date
Date + % H: % M: % S
Shell expression:
Simple arithmetic operation:
B = $(5*5 + 5-3/2 ))
Echo $ B
29
In linux shell, we can use $ () to put the expression in brackets to achieve the operation function.
Or use $.
String processing
Interaction with users:
Read command
P is the abbreviation of prompt, that is, the prompt information is provided by this toggle, for example
Read-p 'Please input you choice: \ n' choice
After you press enter, the screen will be displayed:
Please input you choice:
Then the input will be saved to the choice variable.
Judgment:
Test judgment
Example:
Filename =/home/hello. java
Test-f $ filename & echo 'exit '| echo 'not exit'
[] Judgment
In particular, note that there must be spaces between variable constants and symbols.
Judgment
Example:
If [-f $ filename]; then
Echo 'The file is exist'
Fi