In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Scripting languages and compiled languages:
Scripting language: (Bash)
The script programming language is usually interpreted, which is mainly read into the program code by the interpreter (interpreter) and converted into an internal form for execution.
Advantages:
Can easily deal with objects such as files and directories.
Disadvantages:
It is usually not as efficient as compiled languages.
Compiled languages: (C, C++, Java, Fortran, Ada, Pascal)
Compiled languages mostly operate at the bottom, dealing with bytes, integers, floating-point numbers, or other objects that pass through the machine layer.
The basic syntax format of SHELL scripts:
The script must be #! Beginning: (# cat / etc/shells)
For example, #! / bin/bash (interpreter)
# you can add some comment information in it, such as how the script is used, the function of the script, the date of creation, the author and so on
Then give the script execute permission, # chmod + x scripts.sh
For execution, you can also add the path of this script to the PATH variable using. / scripts.sh # # and run it directly using the name of the script later.
The test tool for the script bash:
-n: check if the script has syntax errors, and if so, it displays an error message, otherwise there is no information (no message is the best news)
-x: check the detailed process of script execution (often used when troubleshooting)
Exit: exit script (its value is 0-255)
If the script does not clearly define the exit code, the exit code before the end of the execution script is the exit code of the script.
# echo $? # # View the status code displayed in the execution result of the previous command
Summary of the logical relationship of the SHELL script:
Logic and: the symbol is & &:
If one of them is false, the result must be false
If the result of the first condition is false, there is no need to judge the second condition, and the final result has been shown.
If the result of the first condition is true, the second condition must be judged.
Example:
# useradd redhat & & echo "redhat" | passwd-- stdin redhat
Explanation: if the useradd redhat executes successfully, proceed to the next command, otherwise terminate.
Logical or | |:
If one of the conditional results is true, the result must be true and there is no need to check the following statements
If one of the conditional results is false, check the next conditional statement
Example:
# id redhat | | useradd redhat
Explanation: if the redhat user exists, the information about the redhat user is displayed, otherwise the account is added.
Examples of the use of logic and and logic or in combination:
# id redhat & & echo "redhat already existing" | | useradd redhat
Explanation: if the redhat user exists, it shows that the redhat user already exists, otherwise add this account.
Summary of conditional judgment statements:
Single-branch if statement double-branch if statement multi-branch if statement case select statement
If judgment condition; then if judgment condition; then if judgment condition; thencase $1 in
Statement statement statement string)
. . . Statement
Fi else elif judgment condition; then string2)
Statement statement statement
. . .)
Fi elif judgment condition; then statement
Statement esac
.
Else
Statement
Fi
Example:
Script analysis:
Main function: pass a different parameter to complete the creation of the user, add the password, delete the user.
Details:
When we pass the-- add parameter to this script, the script completes the addition of the specified user, prompts the user to exist if the added user exists, otherwise adds the specified user and creates a password with the user as its own
When we pass the-- del parameter to this script, the script deletes the user we specified, and if it exists, deletes the user, otherwise prompts the user that it does not exist.
When we pass the-- help parameter to this script, the script gives us a realistic way to use the script.
When we pass other parameters, we prompt for unrecognized options.
Summary of the end status of POSIX:
0: # # status displayed by a successful command
> 0: # # failed during redirection or word expansion (~, variables, commands, arithmetic expansion, and word cutting)
The status displayed by the 1-125 # # command was not successful.
The 126 # # command was found, but the file could not execute the status shown
127 # # the status displayed when the command cannot be found
> 128 # # command died of receiving a signal
Summary of replacement operators (assignment of variables):
${varname:-word}
Returns the value of varname if it exists and is not null; otherwise, returns word
Purpose: if the variable is not defined, return the default value
Example: if count is not defined, the value of ${count:-0} is 0
${varname:=word}
Returns the value of varname if it exists and is not null; otherwise, set varname to word and return its value
Purpose: if the variable is not defined, set the variable to the default value
Fan lie: if count is not defined, the value of ${count:=0} is 0
${varname:+word}
Returns word; if varname exists and is not null; otherwise, returns null
Purpose: for testing the existence of variables
Example: if count is defined, the value of ${count:+1} is 1
${varname:?message}
Returns the value of varname if it exists and is not null; otherwise, displays varname:message and exits the current command or script
Purpose: to catch errors caused by undefined variables.
Example: if count is not defined, ${count:? "undefined!"} displays count:undefined!
Summary of pattern matching operators:
Suppose the value of the path variable is: / etc/sysconfig/network-scripts/ifcfg-eth0.text.bak
${variable#pattern}:
If the pattern matches at the beginning of the variable value, the shortest part of the match is deleted and the rest is returned.
Example: the value of echo ${path#/*/} is etc/sysconfig/network-scripts/ifcfg-eth0.text.bak
${variable##pattern}
If the pattern matches at the beginning of the variable value, the longest part of the match is deleted and the rest is returned.
Example: the value of echo ${path##/*/} is ifcfg-eth0.text.bake
${variable%pattern}
If the pattern matches at the end of the variable, the shortest part of the match is deleted and the rest is returned.
Example: the value of echo ${path%.*} is / etc/sysconfig/network-scripts/ifcfg-eth0.text
${variable%%pattern}
If the pattern matches at the end of the variable, the longest part of the match is deleted and the rest is returned.
Example: the value of echo ${path%%.*} is / etc/sysconfig/network-scripts/ifcfg-eth0
${# variable}
Displays the character length of the value of the variable variable
Summary of loop statements commonly used in Shell scripts:
For cycle while cycle until cycle
For variable in list; do while condition (condition); do until condition; do
Command... Statements statements
Done done done
While loop: as long as condition satisfies the condition, while will loop
Until loop: as long as condition does not meet the condition, until will loop
Test command
The test command can handle all kinds of work in shell scripts. Instead of producing normal output, it can use exit status. Test accepts a variety of different parameters to control which kind of test it wants to perform.
Syntax:
Test [expression]
Test [[expression]]
Purpose:
To test the condition in the shell script, the result is returned through the exit status.
Behavior patterns:
Test is used to test file properties, compare strings, and compare numbers.
Main options and expressions:
String string is not null
-b file file is a block device file (- b)
-c file file is a character device file (- c)
-d file file is the directory (- d)
-whether e file file exists
-f file file is a general file (-)
-g file file has its setgid bit set
-h file file is a symbolic link
-r file file is readable
-s file file is socket.
-w file file is writable
-x file file is executable, or file is a directory that can be found
S1 = S2 S1 is the same as S2 string
S1! = S2 S1 is not the same as S2 string
N1-eq N2 integers N1 is equal to N2
N1-ne N2 integers N1 and N2 are not equal
N1-lt N2 Integer N1 is less than N2
N1-gt N2 Integer N1 is greater than N2
N1-le N2 Integer N1 is less than or equal to N2
N1-ge N2 Integer N1 is greater than or equal to N2
-n string string is non-null
-z string string is a null special parameter variable
There are special variables in bash shell that record the number of command line arguments. Such as $#
You can just count how many parameters are entered on the command line and test each parameter differently. Bash provides a special variable for this, which is the $# mentioned above
Description of $
The $# special variable contains the number of command line arguments that are available when the script is running. You can call this special variable anywhere in the script to call $# to calculate the number of parameters
Example:
Vim Count.sh
#! / bin/bash
# Script Name: Count.sh
# Count Parameters number
Echo There were $# parameters.
Chmod + x Count.sh
. / Count.sh 1 2 3
There were 3 parameters.
Let's talk about the role of ${! #}.
Since the $# variable contains the total number of parameters, ${! #} can call the variable name of the last parameter.
Example:
Vim Count-1.sh
#! / bin/bash
# Script Name: Count-1.sh
# Print last parameter
Params=$#
Echo "The last parameter is" $params "
Echo "The last parameter is" ${! #} "
: wq
Chmod + x Count-1.sh
. / Count-1.sh 1 2 3
The last parameter is 3
The last parameter is 3
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.