First, what is a shell script
The shell is a programming language in addition to the command interpreter, and a program written in Shell resembles a DOS batch program.
It is an interface between the user and the operating system.
The Shell scripting language is very good at handling text-type data, because the configuration files in Linux are text files, so the Shell scripting language
Has played a huge role in managing Linux systems.
Second, why learn shell script
- Automated management
- Monitoring management
- Log data processing
- Automatic data backup
Three, basic grammar
The generic file begins with #!/bin/bash, which indicates that the file uses bash syntax and is not set, but this is a specification.
The shell script is in the behavior unit. Note # variables support string variables and integer variables
VI Edit shell script file
Call Mode:
SH script-file
You can add the-X option to view the execution of this script, which facilitates debugging.
./Script-file
Execute shell scripts in./mode you must set file permissions chmod +x filename into an executable file
The. bin file will be used./Call
Shell variables
Name=string
The name variable string is assigned a value with no spaces on either side and is treated as a command
Cases:
V=centos
echo= $v ($ flag a variable)
Date command:
Date
Date +%h:%m:%s
Shell expression:
Simple arithmetic:
b=$ ((5*5+5-3/2))
Echo $b
29
In the Linux shell, we can use $ (()) to put the expression in parentheses to achieve the function of the operation.
Or you can use $[].
Processing of strings
and user interaction:
Read command
P is the abbreviation for prompt, which is the cue message given by this switch item, such as
Read-p ' please input choice:\n ' Choice
The screen will display when you enter:
Please input choice:
Then the stuff you type will be saved in the choice variable.
Judge:
Test judgment
Cases:
Filename=/home/hello.java
Test-f $filename && echo ' Exit ' | | echo ' Not exit '
[] Judging
In particular, note the first, there must be a space between the variable constant and the symbol.
Judge
Cases:
If [-F $filename];then
Echo ' The file is exist '
Fi
Linux (vii) __shell script programming