Home  >  Article  >  Backend Development  >  Summary of shell knowledge points

Summary of shell knowledge points

WBOY
WBOYOriginal
2016-08-08 09:19:04954browse
目录
  • 引言
  • shell中的数组

    • 数组的定义
    • 数组的使用
    • 实际的例子
  • shell中大小的比较
  • shell中的括号
  • shell中函数的定义
  • 杂项知识点

    • 字符串转数组
    • 常用判断标志
  • 参考文献

引言

SHELL在处理一些问题的时候有得天独厚的优势,快捷方便,学会了还可以显摆显摆,当然了,shell的语法有点坑爹,没有系统的学过,只能一点一点的积累。

今天这个是在实现一个刷新数据库数据的脚本的时候碰到的一些知识点,刷新的时候用到了正则匹配、数学运算、比较等等。


shell中的数组

数组的定义
<code>arr=(1 2 3 4 5)
arr=(燕睿涛 yrt lulu yanruitao)
arr=('^[0-9]+$' '^yrt\.(\d+)\.log$')
arr=(
	"燕睿涛" \
    "yanruitao" \
    "today is a good day!"
)</code>
数组的使用
<code>len=${#arr[@]}	#返回的是数组元素的个数
echo ${arr[0]}	#数组中的第一个元素,这个和其他语言的数组类似,下表从0开始
echo ${arr[2]}	#数组中的第3个元素</code>
实际的例子
<code>[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]$</code>

shell中的大小比较

<code>#第一种(())
if((6 <8)); then echo "yes 燕睿涛"; fi	#输出——yes 燕睿涛
if(($a>8)); then echo "yes 燕睿涛"; fi
if(($a<=$b)); then echo "yes 燕睿涛"; fi
#第二种[] [[]]
if [ 2 -gt 1 ]; then echo "iforever 燕睿涛"; fi
if [[ &#39;abc&#39; > 'ab' ]]; then echo "iforever 燕睿涛"; fi	#iforever 燕睿涛
if [[ 2 < 10 ]]; then echo "iforever 燕睿涛"; fi	#无输出
if [[ 2 -lt 10 ]]; then echo "iforever 燕睿涛"; fi	#iforever 燕睿涛</code>

可以看到上面这几种还是有些规律的:

  • 双小括号[(())]里面是可以直接使用大于小于号进行比较(>、<、<=、>=),而且不需要“坑爹”的空格,用于数学计算
  • 单中括号([])里面比较必须使用-gt、-lt、-ne、-eq这些运算符,而且必须要有严格的空格要求
  • 双中括号([[]])里面比较可以使用>、<、-gt、-lt......这两种格式,但是还是必须要有严格的空格要求,而且双中括号中的>、<对类似于字符串的比较,所以在使用的时候需要注意

shell中的括号

<code>#看看小括号的用法,首先是在for循环里面,相当于还是数学计算
[yanruitao@boss_runtime ad]$ for((a=0;a<10;a++))
> do
> echo $a
> done
0
1
2
3
4
5
6
7
8
9

#对变量进行++,还是相当于数序运算
[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

#单括号里面是一个命令组,括号中的命令将会新开一个shell顺序执行,所以这个里面相当于一个封闭的空间,里面的变量什么的不能被剩余代码使用
[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 ]]...		#[[]]双中括号中只能使用&&,不能使用-a
if [ -n "$ret" -a $ret -gt 123 ]...			#[]单中括号中只能使用-a,不能使用&&
if(($ret)) && (($ret >123 ))...				#(())双小括号使用&&	</li></code>

shell中函数的定义

<code>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    </code>

函数的整体形式如上面的例子,这里面注意两点:

  • 首先就是返回值,通过return的返回值只能是整数,并且在调用完成之后使用echo $?可以查看返回值。
  • 要使用赋值的形式需要有echo,就像ret=$(getId "http://www.baidu.com.1234.html"),只有echo的值会传递给ret变量。

杂项知识点

字符串转数组
<code>[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</code>
常用判断标志
<code>[ -z STRING ]  “STRING” 的长度为零则为真。  
[ -n STRING ] or [ STRING ]  “STRING” 的长度为非零 non-zero则为真。
[ -d FILE ]  如果 FILE 存在且是一个目录则为真。
[ -a FILE ]  如果 FILE 存在则为真。</code>

参考文献

  • Bash Shell 里的各种括号
  • shell中各种括号的作用()、(())、[]、[[]]、{}
  • linux shell 数组建立及使用技巧
  • shell脚本----if(数字条件,字符串条件,字符串为空)
  • Shell for&while 循环详细总结

微信号: love_skills

越努力,越幸运!越幸运,越努力!

做上CEO不是梦

赢取白富美不是梦

屌丝逆袭不是梦

就是现在!!加油
Summary of shell knowledge points

以上就介绍了shell知识点小结,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn