In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
Shell script programming
Before we talk about shell scripting, let's take a look at the color display rules of bash:
Bash's color display rules are set according to ASCII coding; some of its commonly used codes are:
\ 033: Ctrl key
[: control color attribute command
0m: turn off the color attribute command
1m: bold text characters
4m: underline text characters
5m: make text characters blink
7m: swap the background color and foreground color of text characters
8m: sets the background color and foreground color of text characters to the same color
30m-39m: sets the foreground color of text characters; 38m and 39m are temporarily reserved
40m-49m: sets the native color of text characters; 48m and 49m are temporarily reserved
Example: echo-e "\ 033 [5x 37m hello world\ 033 [0m" # display × × flashing hello world
In Linux, a complete Linux program generally includes four types of files:
Binaries (executable), header and library files, help files, configuration files
One of the bash--CLI (Command Line Interface) is a complete application, and there are also these four types of files.
The configuration files for bash fall into three categories:
Profile class: a configuration file that implements an accident for interactive login shell processes
Bashrc class: a file that implements functional configuration for non-interactive login shell processes
Logout class: a configuration file that provides termination and cleanup functions for interactive login shell processes
Type of shell:
Shell for interactive login:
1. Log in to the open shell process after entering the account number and password directly through a terminal
two。 Use su-USERNAME or su-USERNAME to perform the shell process opened by switching login
Shell for non-interactive login:
1. The shell process of a terminal opened through a menu or right-click menu under a graphical interface
two。 Use su USERNAME to perform the switch login opened shell process
Configuration file for bash:
Profile class:
Global: a configuration that takes effect for all users; its directories are / etc/profile, / etc/profile.d/*.sh
Note: in the operating system of RHEL or CentOS series, usually, if a configuration file has many contents and complex format, we will cut it into multiple fragments and store the cut fragments uniformly in the "program name .d" directory. Most of the fragment files saved in this directory will be ordered with a unified file suffix.
User personal: only a profile valid for a user; its directory: ~ / .bash_profile
The purpose of the file configured by the profile class:
1. Environment variables used to define the user
two。 Used to run scripts or execute commands
Bashrc class:
Global: / etc/bashrc
Individual user: / .bashrc
The purpose of the file configured by the bashlrc class:
1. User-defined local variable
two。 Alias for user-defined command
3. Define umask
Note: only superuser root can modify the profile of the global class; ordinary users can only modify the personal profile in their home directory
The interactive login shell process loads the following configuration files sequentially:
/ etc/profile-- > / etc/profile.d/*.sh-- > ~ / .bash_profile-- > ~ / .bashrc-> / etc/bashrc
For non-interactive login shell processes, the following configuration files are loaded sequentially:
~ / .bashrc-- > / etc/bashrc-- > / etc/profile.d/*.sh
All command operations executed on the command line, as long as they do not involve file modifications, are generally valid only for the current shell life cycle; as long as the shell process ends, all settings are invalid
The role of the configuration file; so that the configuration information we rely on can be valid for a long time, as long as we do not modify the contents of the configuration file, every time we open shell will make the previous configuration effective
How to make the newly defined configuration in the configuration file take effect immediately:
1.source command:
Format: source / PATH/TO/SOME_CONF_FILES
. / PATH/TO/SOME_CONF_FILES
2.exec command:
Format: exec / PATH/TO/SIME_CONF_FILES
How to handle strings stored in variables in bash:
Weak variables:
1. Can be used without prior definition
two。 There is no rigid requirement for variable data types, and the default is character lines.
1. String slicing:
${# VAR}: returns the length of the variable VAR of type string
${VAR:offset}: returns the content after the offset character in the string variable VAR, excluding the offset character; the value range of offset is: 0 VAR $[${# VAR}-1]
${VAR:offset:number}: returns the character part of the string variable VAR that starts after the offset and has a length of number
${VAR:-length}: take the length characters tested at the right of the string
two。 Take a string based on the pattern:
${VAR#*PATTERN}: from left to right, find the characters stored in the VAR variable that are matched by PATTERN for the first time, and delete all characters from the beginning of the string to the characters matched by PATTERN
${VAR##*PATTERN}: from left to right, find all characters matched by PATTERN in the string stored in the VAR variable, and delete all characters from the beginning of the string to the last PATTERN matching character
${VAR%PATTERN*}: from right to left, find the characters stored in the VAR variable that are matched by PATTERN for the first time, and delete all characters from the end of the string to the characters matched by PATTERN
${VAR%%PATTERN*}: from right to left, find the character in the string stored in the VAR variable that is matched by PATTERN for the first time, and delete all characters between the end of the string and the last PATTERN matching character
3. Find and replace:
${VAR/PATTERN/SUBSTRING}: find the content that matches PATTERN in the VAR variable, and replace the first matching result with SUBSTRING
${VAR//PATTERN/SUBSTRING}: find the content that matches PATTERN in the VAR variable, and replace all the matching results with SUBSTRING
${VAR/#PATTERN/SUBSTRING}: find the content of the line beginning matching PATTERN in the VAR variable, and replace the matching result with SUBSTRING
${VAR/%PATTERN/SUBSTRING}: find the content of the line end matching PATTERN in the VAR variable, and replace the matching result with SUBSTRING
4. Find and delete:
${VAR/PATTERN}: find the content that matches PATTERN in the VAR variable and delete the result to which the first match is made
${VAR//PATTERN}: find the content that matches the PATTERN in the VAR variable and delete all its matching results
${VAR/#PATTERN}: find the content that matches the PATTERN in the VAR variable and delete the result to which the line header matches
${VAR/%PATTERN}: find the content that matches the PATTERN in the VAR variable and delete the result to which the end of the line matches
5. Case conversion of characters:
${VAR ^ ^}: converts all lowercase letters in the VAR variable to uppercase letters
${VAR,}: converts all uppercase letters in the VAR variable to lowercase letters
6. Variable assignment:
${VAR:-value}: if the variable VAR is empty or not set, the value of value is returned directly, otherwise the value of variable VAR is returned
${VAR:+value}: returns value if the variable VAR is not empty
${VAR:=value}: if the variable VAR is empty or set, then directly return the value of value and assign the value of value to the variable VAR; otherwise return the value of variable VAR
7. Indirect references to variables:
If the value of the first variable happens to be the variable name of the second variable, the method of referencing the value of the second variable from the first variable is called an indirect variable reference, also known as an indirect variable reference.
Array
Before we look at the array, let's talk about variables, which are the storage space of memory. Its characteristic is that only one data can be stored in each variable, and the variable can only be assigned once. But when we store the names and variables of everyone in our class, we have the following ways:
1. One-time assignment: NAME= "name1 name2 name3..."
two。 Use multiple variables to assign values: NAME1=xu, NAME2=shen
3. Array variable
Array: a contiguous memory space for one or more elements, equivalent to a collection of variables.
Array element: any storage unit in an array that stores data
Index of the array:
1. Numerator: indexed array (Index ARRAY)
two。 Name (string): associative array (Related ARRAY) is only supported by versions above bash5.0
There are two types of arrays:
Dense array: index numbers must be continuous
Sparse arrays: index numbers can be discontiguous, and bash arrays fall into this category
The declaration method of the array:
1.declare command:
Declare-I NAME: declare NAME as an integer variable
Declare-x NAME: declare NAME as an environment variable
Declare-a NAME: declare NAME as an indexed array
Declare-A NAME: declare NAME as an associative array
two。 Declare the array directly:
Assign values directly to the array:
ARRAY_NAME= ("value1", "value2", "value3"...) Declare dense array
ARRAY_NAME= ([0] = "value1" [1] = "value2" [4] = "value3"...) Declare sparse array
3. Define the elements of an array and wear an array of pieces:
ARRAY_NAME [0] = value1
ARRAY_NAME [1] = value2
...
Reference elements in an array:
Method of referencing variables: ${NAME}
Method of referencing array elements: ${ARRAY_ name [index]}
Note: if INDEX is not given, it represents the first element of the reference array, that is, the element of INDEX=0
Reference all elements of the entire array: ${ARRAY_NAME [*]} or ${ARRAY_NAME [@]}
Index that references the array: ${! ARRAY_NAME [*]} or ${! ARRAY_NAME [@]}
View the length of the array (the number of valid elements in the array) ${# ARRAY_NAME [*]} or ${# ARRAY_NAME [@]}
Array slices:
${ARRAY_NAME:offset}: displays the index position represented by the offset number and all subsequent elements
${ARRAY_NAME:offset:number}: displays the index location represented by the offset number and subsequent number elements
Append elements to the array:
1. Dense array: ARRAY_NAME [${ARRAY_NAME [*]}] = valueN
two。 Sparse array: ARRAY_ name [index] = value Note: INDEX must index the unused array elements
Undo array: usnet ARRAY_NAME
Delete the element in the array: unset ARRAY_ name [index]
Shell script programming
Features: procedural programming, script programming, interpretive programming
Procedural programming language:
Sequential execution results:
Execute all statements (commands) from left to right and from top to bottom
The body structure of the shell script
Select the execution structure:
Judge the result according to the logic of the given condition or according to the optional range of values, and then select two statements in a branch to execute.
If: branch selection criteria, the result of logical judgment
Case: branch selection criteria, based on optional values
Loop execution structure:
Repeat 0, 1 or more times for a particular statement
For: traverses the specified list
While: the result of logical judgment
Until: the result of logical judgment
Select: an endless loop that uses the loop mechanism to provide a selection list
Select the execution structure:
If statement: if command; then command; [elif command; then command;]. [else command;] fi
If statement single branch structure: if the condition is true, execute the command after then, otherwise, do nothing
Format:
If CONDITOINthen STATEMETfiif CONDITION; then STATEMENT1 STATEMENT2... fi
Note: if you want to execute the STATEMENTS after then, the prerequisite is that the CONDITION part is true.
The double branch structure of the if statement: if the condition is true, execute the command after then, otherwise execute the command after else
If CONDITION; then STATEMENT... else STATEMENT... fi
The multi-branch structure of the if statement: first determine whether CONDITION1 is true, if true, execute the statement after the first then; otherwise, determine whether the CONDITION2 is true, if true, execute the statement after the second then; otherwise, determine whether the CONDITION3 is true, if true, execute the statement after the third then. If all CONDITION is false, execute the statement after else
If CONDITION1; then STATEMENT... elif CONDITION2; then STATEMENT... elif CONDITION3; then STATEMENT. Else STATEMENT... fi
Suggestion: if multi-branch structure, you don't have to use it.
Loop execution structure:
Repeat a piece of code 0 times, 1 or more times
A good circular structure must include a series of important links:
Conditions for entering the cycle:
Conditions met at the beginning of the loop
Conditions for exiting the loop:
The conditions satisfied by the end of the loop
For loop:
1. Traverse the list
For VAR_NAME in LIST; do cycle body; donefor VAR_NAME in LIST; do cycle body done
VAR_NAME: any specified variable name whose value is taken and assigned from LIST
Loop body: generally speaking, it is a combination of commands or commands that can use VAR_NAME; if VAR_NAME is not included in the loop body, there may be an endless loop
How to generate LIST:
1) give directly
2) list of pure integers
3) expansion of curly braces
4) the return value of the execution result of the command
5) GLOBBING
6) references to some variables: $@, $*
two。 Control variable
For ((expression 1; expression 2; expression 3)); do command; donefor ((expression 1; expression 2; expression 3)); do loop body done
Expression 1: assign an initial value to a variable
Expression 2: exit condition of the loop
Expression 3: the changing law of the value of variables
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.