In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail what are the basics of shell scripts, and the editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
First move, HelloWorld.
The first type: echo
Echo "Hello World"
Echo-n "Hello World" # without line feeds
Echo-e'\ e [0politics 33th 1mHello\ e [0m World' # play with color
Echo-e'\ e [0politics 33ter4mHello\ e [0m World' # with color + underscore
Echo-e'\ e [0bot 33ter5mHello\ e [0m World' # with color + flicker
The format is\ e [background color; foreground color; highlight format m, please read the detailed documentation and use the correct posture to press.
The second way to judge
The first type: if
If true
Then
Echo "Hello World"
Else
Echo "Bug"
Fi
If false
Then
Echo "Hello World"
Elif true
Then
Echo "Bug"
Else
Echo "Bee"
Fi
Principle of judgment
If, elif will execute the command that follows it, and then see if the return value is 0. If it is 0, execute the statement block under then.
Otherwise, execute the block of statements under else.
[casheywen@ubuntu:~] $true
[casheywen@ubuntu:~] $echo $?
0
[casheywen@ubuntu:~] $false
[casheywen@ubuntu:~] $echo $?
one
Note:
In fact, 1.true and false are also commands. The return code of true must be 0, and the return code of false must be 1.
2. What? A built-in variable for shell, which is used to store the return code of the previous command
The second formula: test, [] and ``
Test, [], ``are actually commands in shell, which will return 1 or 0 after execution, and these commands can be combined with if to achieve our
Many of the required judgments, such as three ways to test whether a string is empty:
S = ""
If [- z ${s}]
Then
Echo "empty"
Fi
If [[- z ${s}]]
Then
Echo "empty"
Fi
If test-z ${s}
Then
Echo "empty"
Fi
In fact, the [], ``and test commands after if can be executed separately, and according to the principle of if, which branch is also executed by
It is determined by the return values of [], ``and test. The following is the effect of executing them separately:
[casheywen@ubuntu:~] $s = ""
[casheywen@ubuntu:~] $[- z "${s}"]
[casheywen@ubuntu:~] $echo $?
0
[casheywen@ubuntu:~] $s = "abc"
[casheywen@ubuntu:~] $test-z "${s}"
[casheywen@ubuntu:~] $echo $?
one
[casheywen@ubuntu:~] $s = "123"
[casheywen@ubuntu:~] $[[100-lt ${s}]]
[casheywen@ubuntu:~] $echo $?
0
In terms of performance, [] and test performance are basically the same, and ``performance is the highest, about 5 times that of the former two (tested with the-d operator), so it is recommended
Try to use ``to improve script performance.
File testing
The following is the "introduction" of this tutorial, which is suitable for beginners and veterans to check and fill in gaps.
First move, HelloWorld.
The first type: echo
Echo "Hello World"
Echo-n "Hello World" # without line feeds
Echo-e'\ e [0politics 33th 1mHello\ e [0m World' # play with color
Echo-e'\ e [0politics 33ter4mHello\ e [0m World' # with color + underscore
Echo-e'\ e [0bot 33ter5mHello\ e [0m World' # with color + flicker
The format is\ e [background color; foreground color; highlight format m, please read the detailed documentation and use the correct posture to press.
The second way to judge
The first type: if
If true
Then
Echo "Hello World"
Else
Echo "Bug"
Fi
If false
Then
Echo "Hello World"
Elif true
Then
Echo "Bug"
Else
Echo "Bee"
Fi
Principle of judgment
If, elif will execute the command that follows it, and then see if the return value is 0. If it is 0, execute the statement block under then.
Otherwise, execute the block of statements under else.
[casheywen@ubuntu:~] $true
[casheywen@ubuntu:~] $echo $?
0
[casheywen@ubuntu:~] $false
[casheywen@ubuntu:~] $echo $?
one
Note:
In fact, 1.true and false are also commands. The return code of true must be 0, and the return code of false must be 1.
2. What? A built-in variable for shell, which is used to store the return code of the previous command
The second formula: test, [] and ``
Test, [], ``are actually commands in shell, which will return 1 or 0 after execution, and these commands can be combined with if to achieve our
Many of the required judgments, such as three ways to test whether a string is empty:
S = ""
If [- z ${s}]
Then
Echo "empty"
Fi
If [[- z ${s}]]
Then
Echo "empty"
Fi
If test-z ${s}
Then
Echo "empty"
Fi
In fact, the [], ``and test commands after if can be executed separately, and according to the principle of if, which branch is also executed by
It is determined by the return values of [], ``and test. The following is the effect of executing them separately:
[casheywen@ubuntu:~] $s = ""
[casheywen@ubuntu:~] $[- z "${s}"]
[casheywen@ubuntu:~] $echo $?
0
[casheywen@ubuntu:~] $s = "abc"
[casheywen@ubuntu:~] $test-z "${s}"
[casheywen@ubuntu:~] $echo $?
one
[casheywen@ubuntu:~] $s = "123"
[casheywen@ubuntu:~] $[[100-lt ${s}]]
[casheywen@ubuntu:~] $echo $?
0
In terms of performance, [] and test performance are basically the same, and ``performance is the highest, about 5 times that of the former two (tested with the-d operator), so it is recommended
Try to use ``to improve script performance.
File testing:
String comparison:
Note: 1. Add "" to both sides of the string to prevent errors
two。 It is a string comparison, do not misuse it as an integer comparison
3. If you use them in [], you need to write them as\.
Integer comparison:
Type 3: & &, | |:
Note: only ``is allowed to write & & in it.
| |\ |\ | it can be used to sum two judgment statements |-- | | if [- n "abc"]\ |\ | [- n "aa"] | | if [[- n "abc"]]\ | [[- n "aa"]] | | if test-n "abc"\ |\ | test-n "aa" | | if [- n "abc"\ |\ |-n "aa"] | |
Note: only ``is allowed to write | | in it.
Tips
& &, | | can also be used to concatenate commands to determine whether to execute the latter command according to the success of the previous command.
Cd / data & & ls # execute the following `ls` cd / data when the `cd / data` returns 0 (that is, successful) | | cd / root # execute the following `cd / root` when the `cd / data` returns non-0 (that is, failure)
The third trick: circulation
The first type: for
For i in {1..100}
Do
Echo ${I}
Done
Note:
1. {1. 100} is a kind of wildcard writing, and the expansion will be 1, 2, 3. A string like 100 (separated by a space).
two。 For example, in a statement such as for i in 1 / 2 / 3, for will loop 1, 2, and 3 to I in turn, while in the case of a wildcard, for will unfold the wildcard and assign each item in turn to I to loop.
For i in `seq 100`
Do
Echo ${I}
Done
For i in `seq 1 2 100`
Do
Echo ${I}
Done
Note:
1.seq itself is a command that outputs a sequence of numbers, such as seq 100 will generate and output a sequence of 1 2 3... 100 (separated by a newline character), while seq 1 2 100 will generate and output 1 3 5. 99 (items less than 100 in the arithmetic sequence starting with 1 and tolerated by 2, separated by a line feed).
two。 The command between the backquotes (') will be executed and the output will be converted to a variable, so the above for in will in turn take out the execution result of the seq and assign it to I for a loop.
For ((I = 0; I)
< 100; i++)) do echo ${i} done for ((i = 0; i < 100; i+= 2)) do echo ${i} done 注: 以上与C语言式的for循环语法基本相同,区别在于双重括号:(( )) 第二式:while、until i=0 while [[ ${i} -lt 100 ]] do echo ${i} ((i++)) done i=0 until [[ ${i} -ge 100 ]] do echo ${i} ((i++)) done 注: while和until的判断原理与if是类似的,它会执行并它后面跟着的命令,不同点在于: while是后面语句返回值为0,则执行循环中的语句块,否则跳出循环; until则是后面语句返回值非0,则执行循环中的语句块,否则跳出循环。 第四招:变量 第一式:整数 整数的运算 方法较多,此处只列举最浅显易懂,并且效率最高的办法--直接将符合C语言语法的表达式放到(( ))中即可达到对整数的计算目的: echo $(( 1+1 )) # 最简单的1+1 echo $(( (1+2)*3/4 )) # 表达式中还可以带括号 echo $(( 1 hello.txt # hello.txt原有的将被覆盖 echo "Hello World" >> hello.txt # hello.txt appends `World` to the original content
Formula 2: redirect standard error stream (stderr)
Output the error message of the program to a file
For example, the file + + does not exist in the file path:
[casheywen@ubuntu:~] # ls + +
Ls: cannot access + +: No such file or directory
[casheywen@ubuntu:~] # ls + + > out.txt
Ls: cannot access + +: No such file or directory
The output after ls + + above is the error message in the standard error stream, so even if you redirect the standard input with > out.txt, the error message is still printed to the screen.
# both of the following methods will output the error message output from `err.txt` to `err.txt` (create if it does not exist)
Ls + + 2 > err.txt # err.txt original content will be overwritten
Ls + + 2 > > err.txt # err.txt append the original content
The third formula: redirect standard input stream (stdin)
1. Let the program read input from the file
Take, for example, the command bc that reads expressions from standard input by default and performs mathematical calculations:
[casheywen@ubuntu:~] # bc-Q
1-1
two
Note:
1. 1 is the keyboard input, 2 is the calculation result printed by the bc command
The-Q parameter after 2.bc is used to disable the output of welcome information.
3. The format of the above redirection method is command < file path
If I need to input an expression that already exists in the file exp.txt into bc for evaluation, I can redirect the standard input stream:
Bc-Q < exp.txt
Note:
1. When the content in exp.txt is 1: 1, the above statement outputs 2.
two。 Since the bc command itself supports input from a file, you can use bc exp.txt to achieve the same effect if redirection is not used
two。 Input the contents of the variable as a program
EXP= "1x 1"
Bc-Q
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.