In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what Linux shell knowledge points", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "What Linux shell knowledge points"!
Shell is actually a command interpreter that interprets commands entered by the user and sends them to the kernel. Moreover, Shell has its own programming language for editing commands, which allows users to write programs composed of shell commands. Shell programming language has many features of ordinary programming languages, such as it also has loop structure and branch control structure, etc. Shell programs written in this programming language have the same effect as other applications.
introduction
SHELL has unique advantages in dealing with some problems. It is fast and convenient. If you learn it, you can also show off. Of course, the syntax of shell is a bit deceptive. There is no systematic learning. You can only accumulate it bit by bit.
Today this is in the implementation of a refresh database data script encountered some knowledge points, refresh when using regular matching, mathematical operations, comparison and so on.
Array in shell
Definition of array
arr=(1 2 3 4 5)arr=(Yan Ruitao yrt lulu yanruitao)arr=('^[0-9]+$'^yrt\. (\d+)\.log $') arr=( "Yanruitao" \ "today is a good day! ")
Use of arrays
len=${#arr[@]} #returns the number of array elements echo ${arr[0]} #the first element in the array, similar to arrays in other languages, the following table starts with 0 echo ${arr[2]} #the third element in the array
practical example
[yanruitao@boss_runtime sh]$ arr=(> "Yan Ruitao"> "http:\/\/www\.baidu\.com\/(\d+)\.html"> "yanruitao"> "lulu"> "yrt"> )[yanruitao @boss_runtime sh]$ echo ${#arr[@]}5[yanruitao@boss_runtime sh]$ echo ${arr[1]}http:\/\/www\.baidu\/(\d+)\.html[yanruitao@boss_runtime sh]$ echo ${arr[0]} Yan Ruitao [yanruitao @ boss_runtime sh]$ echo ${arr[5]}[yanruitao@boss_runtime sh]
Size comparison in shell
#first type (())if((6 8)); then echo "yes Yan Ruitao"; fiif(($a 'ab' ]]; then echo "iforever Yan Ruitao"; fi #iforever Yan Ruitao if [ 2
< 10 ]]; then echo "iforever 燕睿涛"; fi #无输出if [[ 2 -lt 10 ]]; then echo "iforever 燕睿涛"; fi #iforever 燕睿涛 可以看到上面这几种还是有些规律的: 双小括号[(())]里面是可以直接使用大于小于号进行比较(>,,, echo $a> done0123456789#++ variables, Or is it equivalent to numerical operations [yanruitao@boss_runtime ad]$ i=1[yanruitao @boss_runtime ad]$ echo $i1[yanruitao@boss_runtime ad]$ let i++[yanruitao@ boss_runtime ad]$ echo $i2[yanruitao @ boss_runtime ad]$ ((i++))[yanruitao@boss_runtime ad]$ echo $i3#mathematical operations [yanruitao @ boss_runtime ad]$ echo 1+ 21+2[yanruitao@boss_runtime ad]$ echo $((1+2))3#Inside the single parenthesis is a command group. The command in parenthesis will open a shell sequence execution, so this is equivalent to a closed space. The variables in it cannot be used by the rest of the code.[yanruitao@boss_runtime ad]$ a=1[yanruitao@boss_runtime ad]$(a=3;echo $a)3[yanruitao@boss_runtime ad]$ echo $a1#Use of and in parentheses if [[ -n "$ret" && $ret -gt 123 ]]... #[[]] Double brackets can only use &&, not-aif [ -n "$ret" -a $ret -gt 123 ]... #[] Only-a can be used in single brackets,&&if(($ret)) &&(($ret >123 )) cannot be used... #(()) double parenthesis using &&
Definition of functions in shell
function getId(){localurl =$1 #locallimits the scope of the variable url only to the function, otherwise it will pollute the global scope ereg="http:\/\/www\.baidu\/\([0-9]\+\)\.html" localret =$(expr $url : $ereg) if [[ -n "$ret" && $ret -gt 0 ]]; then #When ret is null, using [] will report an error, -n The double quotes here must be added, otherwise when $ret is null, always return true echo $ret return 0 fi return 1}[yanruitao@boss_runtime sh]$ echo $? 0[yanruitao@boss_runtime sh]$ getId "http://www.baidu.com/123.htl"[yanruitao@boss_runtime sh]$ echo $? 1[yanruitao@boss_runtime sh]$ getId "http://www.baidu.com/123.html"123[yanruitao@boss_runtime sh]$ echo $? 0
The overall form of the function is as in the above example, and there are two points to note:
The first is the return value, the return value through return can only be an integer, and after the call is completed, use echo $? You can view the return value.
To use the assignment form, echo is required, like ret=$(getId "http://www.baidu.com.1234.html"), and only the echo value is passed to the ret variable.
Miscellaneous knowledge points
String to Array
[yanruitao@boss_runtime sh]$ str="yan ruitao lulu yrt yanruitao"[yanruitao@boss_runtime sh]$ arr=($str) #This step converts the string to an array [yanruitao@boss_runtime sh]$ echo ${arr[*]} yan ruitao lulu yrt yanruitao[yanruitao@boss_runtime sh]$ echo ${#arr[@]}4
Common judgment mark
[ -z STRING ] "STRING" is true if its length is zero.
[ -n STRING ] or [ STRING ] is true if the length of "STRING" is non-zero.
[ -d FILE ] True if FILE exists and is a directory.
[ -a FILE ] True if FILE exists.
Linux background running related
& #Add this command to the end of a command to put the command in the background./ update.sh 100 500 &ctrl + z #Put a foreground command in the background and suspend it jobs #View currently running background commands jobs -l #Show PID of all background tasks [yanruitao@boss_runtime sh]$ jobs -l[1] 9681 Running ./ t.sh 100 300 &[2] 9683 Running ./ t.sh 100 300 &[3]- 9685 Running ./ t.sh 100 300 &[4]+ 9688 Running ./ t.sh 100 300 &fg #Adjust the commands in the background to the foreground to continue running. If there are multiple commands in the background, use `fg %jobnumber` to call up [yanruitao@boss_runtime sh]$ jobs -l[2] 10033 Running ./ t.sh 100 300 &[3] 10035 Running ./ t.sh 100 300 &[4]- 10037 Running ./ t.sh 100 300 &[5]+ 10039 Running ./ t.sh 100 300 &[yanruitao@boss_runtime sh]$ fg %2./ t.sh 100 300 bg #tells how a command paused in the background becomes resumed in the background. Similarly, if there are multiple commands, you can use bg %jobnumber[yanruitao@boss_runtime sh]$ jobs -l[1]-11655 Running ./ t.sh 100 300 &[2]+ 11662 Running ./ t.sh 100 300 &[yanruitao@boss_runtime sh]$ fg %1./ t.sh 100 300^Z[1]+ Stopped ./ t.sh 100 300[yanruitao@boss_runtime sh]$ jobs -l[1]+ 11655 Stopped ./ t.sh 100 300[2]- 11662 Running ./ t.sh 100 300 &[yanruitao@boss_runtime sh]$ bg %1[1]+ ./ t.sh 100 300 &[yanruitao@boss_runtime sh]$ jobs -l[1]- 11655 Running ./ t.sh 100 300 &[2]+ 11662 Running ./ t.sh 100 300 &kill #terminate process kill %num #job number viewed by jobs, kill pid #kill process ctrl + C#terminate the current foreground process This, I believe everyone has a deeper understanding of "what Linux shell knowledge points", may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
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.