In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Overview of Shell script
With the more and more applications of Linux system in enterprises, the automatic management of servers is becoming more and more important. In some complex maintenance work, a proper Shell script can batch process and automate a series of maintenance tasks, thus reducing the burden of administrators.
The concept of Shell script
Save the commands to be executed to a text file in order
If the file is given executable permission, it can be run
Various Shell control statements can be combined to accomplish more complex operations
Shell script application scenario
Repetitive operation
Batch transaction processing
Automatic operation and maintenance
Service running status monitoring
Scheduled task execution
The role of SHell script-command interpreter
Shell is equivalent to a "translator", between the system kernel and the user, and is responsible for interpreting the command line.
Login Shell of the user
The default shell program used after login, usually / bin/bash
The internal instructions and running environment of different shell will be different.
Write script code
Use the vim text editor
One Linux command per line, written in order of execution
[root@localhost ~] # vim first.sh write the first script
#! / bin/bash#this is fiest scriptcd / bootpwdls-lh vml gives executable permission
Make the script executable
[root@localhost ~] # chmod + x first.sh execution script file [root@localhost ~] # / first.sh (must have execution permission and do not change the directory location)
[root@localhost] #. First.sh (no execution permission is required, but change the directory location)
[root@localhost ~] # source first.sh (no execution permission is required, but change the directory location)
[root@localhost ~] # sh first.sh (no execution permission and no change of directory location)
Better script composition
Script declaration
Comment information
Executable statement
#! / bin/bash#this is fiest scriptcd / bootecho "current path"pwdecho" file information beginning with vml "ls-lh vml"
Redirection and pipe operation
Interactive hardware equipment
Standard input: receives data entered by the user from this device
Standard output: output data to the user through this device
Standard error: an execution error message is reported through this device
Redirect operation
* * Pipeline operation symbol "|"
Awk Vertical recognition-F separator (separated by colon,-F followed by colon) Print output $1 call the first variable $7 call the seventh variable shell variable function, type
The role of variables
Providing specific parameters for flexible management of Linux systems has two meanings
Variable name: use a fixed name, preset by the system or user-defined.
Variable value: can change according to user settings and changes in the system environment
Type of variable
Custom variables: defined, modified, and used by the user
Environment variables: maintained by the system and used to set up the work environment
Position variables: passing parameters to the script from the command line
Predefined variables: a class of variables built into Bash that cannot be modified directly
Custom variable
Define a new variable
Variable names begin with a letter or underscore and are case-sensitive. All uppercase is recommended.
Variable name = variable value
View the value of a variable
Echo $variable name
Use quotation marks when assigning values
Double quotation marks: allows other variable values to be referenced by the $symbol
Single quotation marks: reference to other variable values is prohibited, and $is regarded as a normal character
Reverse apostrophe (key under Esc): replace the command and extract the output of the command.
Assign a value to a variable from keyboard input
Read [- p "prompt message"] variable name
Set the scope of the variable
Format 1:export variable name format 1:export variable name = variable value (the two formats can be mixed)
Operation of integer variables
Expr variable 1 operator variable 2 [operator variable 3]
Common operators
(1) addition operation: +
(2) subtraction:-
(3) multiplication operation:
(4) Division operation: /
(5) Modulus (remainder) operation:%
Example demonstration: calculating the sum of any two integers
#! / bin/bashread-p "Please enter the first integer:" num1read-p "Please enter the second integer:" num2# performs the addition operation sum= `expr $num1 + $num2`echo "the sum is: $sum"
Special Shell variable
Environment variable
Created in advance by the system to set up the user's working environment
Configuration file: / etc/profile, ~ / .bashprofile
Common environmental variables
PWD 、 PATH 、 USER 、 HOME 、 SHELL
Location variable
Expressed as a number between $n and n = 1 to 9
#! / bin/bashecho "value of the first location variable: $1" echo "the value of the second location variable: $2" sum= `expr $1 + $2`echo "summed as: $sum"
Predefined variable
$#: the number of location variables on the command line
$*: contents of all location variables
$? The status returned after the last command is executed. A return value of 0 indicates normal execution, while a non-zero value only indicates an error in execution.
$0: currently executed process / program name
#! / bin/bashecho "first location variable value: $1" echo "second location variable value: $2" sum= `expr $1 + $2`echo "summation is: $sum" echo "script name: $0" echo "detailed number: $* number of" echo "parameters: $#"
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.