Linux Shell Knowledge Point Rollup _linux shell

Source: Internet
Author: User
Tags arrays ereg linux shell commands

The shell is actually a command interpreter that interprets the commands entered by the user and sends them to the kernel. Not only that, the shell has its own programming language for editing commands, which allows the user to write a program composed of shell commands. Shell programming language has many features of ordinary programming language, such as it also has the loop structure and the branch control structure, and so on, the shell program written in this programming language has the same effect as other applications.

Introduction

Shell in dealing with a number of problems when there is a unique advantage, fast and convenient, learned to show off, of course, the Shell's grammar a little pit dad, no systematic study, can only 1.1 points of accumulation.

Today this is in the implementation of a script to refresh the database data encountered some of the knowledge points, the time to refresh the use of regular matching, mathematical operations, comparisons and so on.

Arrays in the shell

The definition of an array

Arr= (1 2 3 4 5)
arr= (Yan yrt Lulu Yanruitao)
arr= (' ^[0-9]+$ ' ^yrt\. \d+) \.log$ ')
arr= (
 "Yan" \
  "Yanruitao" \
  "is a good day!"
)

Use of arrays

len=${#arr [@]} #返回的是数组元素的个数
echo ${arr[0]} #数组中的第一个元素, which is similar to an array in other languages, the following table starts with
echo ${arr[2]} #数组中的第3个元素

The actual example

[Yanruitao@boss_runtime sh]$ arr= (
> "Yan"
> "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\.com\/(\d+) \.html
[Yanruitao@boss_ Runtime sh]$ echo ${arr[0]}
Yan
[Yanruitao@boss_runtime sh]$ echo ${arr[5]}

[Yanruitao@boss_runtime sh ]$

Size comparisons in the shell

#第一种 (())
if ((6 <8)); then echo "Yes Yan"; Fi #输出--yes Yan
if (($a >8); then echo "Yes Yan"; fi
if ($a & lt;= $b)); Then echo "Yes Yan"; Fi
#第二种 [] [[]]
if [2-GT 1]; then echo "Iforever Yan"; fi
If [[' ABC ' > ' AB ']]; then echo "Iforever Yan Rui Tao "; Fi #iforever Yan
if [[2 <]]; then echo "Iforever Yan"; fi #无输出
if [[2-lt]]; then echo "Iforever Yan Tao "; Fi #iforever Yan

You can see that these kinds of things are still some rules:

The double brackets [(())] inside can be compared directly using greater than less than (>, <, <=, >=), and do not need "pit dad" space, for mathematical calculations
In single brackets ([]) comparisons must use-GT,-lt,-ne,-eq these operators, and must have strict space requirements
The double bracket ([[]]) can be compared with >, <,-GT,-lt ... Both formats, but there must be strict space requirements, and the >, < in double brackets is similar to string comparisons, so you need to be careful when using

Parentheses in the shell

#看看小括号的用法, first in the For loop, equivalent to or mathematically computed
[yanruitao@boss_runtime ad]$ for (a=0;a<10;a++)) > Do
> Echo $ A
> Done
0
1 2 3 4 5 6 7 8 9

#对变量进行 + +, or equivalent to a sequence of operations
[ Yanruitao@boss_runtime ad]$ i=1
[Yanruitao@boss_runtime ad]$ echo $i
1
[Yanruitao@boss_runtime ad]$ Let i++
[Yanruitao@boss_runtime ad]$ echo $i
2
[Yanruitao@boss_runtime ad]$ ((i++))
[ Yanruitao@boss_runtime ad]$ Echo $i
3

#数学运算
[Yanruitao@boss_runtime ad]$ echo 1+2
1+2
[ Yanruitao@boss_runtime ad]$ echo $ (1+2))
3

#单括号里面是一个命令组, the command in parentheses will be executed in a new shell order, so this inside is equivalent to a closed space, The variables inside can not be used by the remaining code
[yanruitao@boss_runtime ad]$ a=1
[yanruitao@boss_runtime ad]$ (A=3;echo $a)
3
[Yanruitao@boss_runtime ad]$ echo $a
1

#括号中and的使用
if [[-N "$ret" && $ret-gt 123]] ... #[[] You can only use && in double brackets, and you cannot use the-a
if [-N "$ret"-a $ret-gt 123] ...  #[] Only-a can be used in parentheses, &&
if (($ret)) && (($ret >123) can not be used ...  # (()) Double parenthesis Use && 

Definition of functions in the shell

function GetId ()
{
 local url=$1 #local限定了变量url的作用域只在函数里面, otherwise polluting the global scope
  ereg= "http:\/\/www\.baidu\.com\/ \ ([0-9]\+\) \.html "local
  ret=$ (expr $url: $ereg)
  if [[-N" $ret "&& $ret-GT 0]]; then #当ret为null时使用 [] will report Wrong,-n the double quotes here must be added, otherwise, when $ret is null, it always returns True
   Echo $ret return
    0
  fi
  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, as in the example above, notes two points:

The first is the return value, which can only be an integer by return, and the echo $ is used after the call is complete? You can view the return value.
To use the form of an assignment, ECHO is required, like ret=$ (getId "http://www.baidu.com.1234.html"), and only the value of ECHO is passed to the RET variable.

Miscellaneous Knowledge points

Array of strings

[Yanruitao@boss_runtime sh]$ str= "Yan Lulu yrt Yanruitao"
[yanruitao@boss_runtime sh]$ arr= ($str)  # This step converts the string to an array
[Yanruitao@boss_runtime sh]$ echo ${arr[*]}
Yan Lulu yrt Yanruitao
[yanruitao@boss_runtime sh]$ echo ${#arr [@]}
4

Common judgment Mark
[-Z STRING] The length of "STRING" is zero is true.
The length of [-N string] or [string] "string" is not 0 Non-zero is true.
[-D file] is true if FILE exists and is a directory.
[-A file] if FILE existence is true.

Linux background Run related

& #在一个命令的最后加上这个命令, you can place the command in the background./update.sh & Ctrl + Z #讲一个正在前台执行的命令放到后台 and in a suspended state jobs #查看当前后台运行的命令 jobs         -L #可以显示所有后台任务的PID [Yanruitao@boss_runtime sh]$ jobs-l [1] 9681 Running./t.sh & [2] 9683 Running 

/t.sh & [3]-9685 Running/t.sh & [4]+ 9688 Running./t.sh &         FG #把后台中的命令调至前台继续运行, if there are multiple commands in the background, you can use ' FG%jobnumber ' to call out the selected command [Yanruitao@boss_runtime sh]$ jobs-l [2] 10033 Running /t.sh & [3] 10035 Running./t.sh & [4]--10037 Running./t.sh & [5]+ 1 0039 Running./t.sh & [Yanruitao@boss_runtime sh]$ FG%2./t.sh-BG #讲一个在后台暂停的命令变成在后台继续执行. Similarly, if you have more than one command, you can use the BG%jobnumber [yanruitao@boss_runtime sh]$ jobs-l 1]-11655. Running & [/t.sh 116 Running/t.sh & [Yanruitao@boss_runtime sh]$ FG%1./t.sh ^z [1]+ Stopped./t.sh 1 [Yanruitao@boss_rUntime sh]$ jobs-l [1]+ 11655 Stopped./t.sh 2]-. 11662 & [Running Oss_runtime sh]$ bg%1 [1]+./t.sh & [Yanruitao@boss_runtime sh]$ jobs-l [1]-11655 Running./t.sh 10
0 & [2]+ 11662 Running./t.sh & Kill #终止进程 kill%num #通过jobs查看的job号 to kill PID #通过进程号杀掉进程
 CTRL + C #终止当前前台的进程

The above is the entire contents of this article, I hope to be familiar with the Linux shell commands help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.