Directory
Introduction
Shell in the handling of some problems when there is a unique advantage, fast and convenient, learned can also show off, of course, the shell of the grammar a bit of the father, no systematic study, can only 1.1 points of accumulation.
Today this is in the implementation of a database to refresh the script to meet some of the knowledge points, when refreshed with the use of regular matching, mathematical operations, comparisons and so on.
Arrays in the shell
Definition of an array
arr=(1 2 3 4 5)arr=(燕睿涛 yrt lulu yanruitao)arr=('^[0-9]+$' '^yrt\.(\d+)\.log$')arr=("燕睿涛" \ "yanruitao" \ "today is a good day!")
Use of arrays
len=${#arr[@]}#返回的是数组元素的个数echo ${arr[0]}#数组中的第一个元素,这个和其他语言的数组类似,下表从0开始echo ${arr[2]}#数组中的第3个元素
The actual example
[yanruitao@boss_runtime sh]$ arr=(> "燕睿涛"> "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]}燕睿涛[yanruitao@boss_runtime sh]$ echo ${arr[5]}[yanruitao@boss_runtime sh]$
Size comparison in the shell
#第一种(())if((6 <8)); then echo "yes 燕睿涛"; fi#输出——yes 燕睿涛if(($a>8)); then echo "yes 燕睿涛"; fiif(($a<=$b)); then echo "yes 燕睿涛"; fi#第二种[] [[]]if [ 2 -gt 1 ]; then echo "iforever 燕睿涛"; fiif [[ 'abc' > 'ab' ]]; then echo "iforever 燕睿涛"; fi#iforever 燕睿涛if [[ 2 < 10 ]]; then echo "iforever 燕睿涛"; fi#无输出if [[ 2 -lt 10 ]]; then echo "iforever 燕睿涛"; fi#iforever 燕睿涛
You can see that these are some of the rules:
- The double brackets [()]) are used to compare (>, <, <=, >=) and do not require "pit daddy" spaces for mathematical calculations.
- The-GT,-lt,-ne,-eq operators must be used in single brackets ([]), and must have strict space requirements
- A pair of brackets ([[]]) can be used to compare >, <,-GT,-lt ... These two formats, but still must have strict space requirements, and the double brackets in the >,<>< li=""><>
Parentheses in the shell
#看看小括号的用法,首先是在for循环里面,相当于还是数学计算[yanruitao@boss_runtime ad]$ for((a=0;a<10;a++))> do> echo $a> done0123456789#对变量进行++,还是相当于数序运算[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#数学运算[yanruitao@boss_runtime ad]$ echo 1+21+2[yanruitao@boss_runtime ad]$ echo $((1+2))3#单括号里面是一个命令组,括号中的命令将会新开一个shell顺序执行,所以这个里面相当于一个封闭的空间,里面的变量什么的不能被剩余代码使用[yanruitao@boss_runtime ad]$ a=1[yanruitao@boss_runtime ad]$ (a=3;echo $a)3[yanruitao@boss_runtime ad]$ echo $a1#括号中and的使用if [[ -n "$ret" && $ret -gt 123 ]]...#[[]]双中括号中只能使用&&,不能使用-aif [ -n "$ret" -a $ret -gt 123 ]...#[]单中括号中只能使用-a,不能使用&&if(($ret)) && (($ret >123 ))...#(())双小括号使用&&
Definition of functions in the shell
function getId(){local url=$1#local限定了变量url的作用域只在函数里面,不然会污染全局的作用域 ereg="http:\/\/www\.baidu\.com\/\([0-9]\+\)\.html" local ret=$(expr $url : $ereg) if [[ -n "$ret" && $ret -gt 0 ]]; then#当ret为null时使用[]会报错,-n这里的双引号一定要加上,不然当$ret为null时,一直返回真 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 as in the above example, note that two points:
- The first is the return value, the return value can only be an integer, and can be used
echo $?
to view the return value after the call is complete.
- The form to use the assignment needs to be there
echo
, as ret=$(getId "http://www.baidu.com.1234.html")
if only echo
the value is passed to the ret
variable.
Miscellaneous Knowledge points
Array of string turns
[yanruitao@boss_runtime sh]$ str="燕睿涛 lulu yrt yanruitao"[yanruitao@boss_runtime sh]$ arr=($str)#这一步将字符串转化为了数组[yanruitao@boss_runtime sh]$ echo ${arr[*]}燕睿涛 lulu yrt yanruitao[yanruitao@boss_runtime sh]$ echo ${#arr[@]}4
Common judgment Mark
[ -z STRING ] “STRING” 的长度为零则为真。 [ -n STRING ] or [ STRING ] “STRING” 的长度为非零 non-zero则为真。[ -d FILE ] 如果 FILE 存在且是一个目录则为真。[ -a FILE ] 如果 FILE 存在则为真。
Reference documents
- Various parentheses in the Bash Shell
- The role of various parentheses in the shell (), (()), [], [[]], {}
- Linux shell array creation and usage tips
- Shell script----if (numeric condition, string condition, string null)
- Shell For&while Loop Detailed summary
Number: Love_skills
The more effort, the luckier! The more fortunate, the harder!
Being CEO isn't a dream.
Winning mating is not a dream
Cock Silk is not a dream
is now!! Come on
The above describes the shell knowledge point summary, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.