0

0

shell知识点总结

php中文网

php中文网

发布时间:2016-06-13 12:23:33

|

1247人浏览过

|

来源于php中文网

原创

shell知识点小结

目录
  • 引言
  • shell中的数组

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

    • 字符串转数组
    • 常用判断标志
    • linux后台运行相关
  • 参考文献

引言

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

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


shell中的数组

数组的定义
arr=(1 2 3 4 5)arr=(燕睿涛 yrt lulu yanruitao)arr=('^[0-9]+$' '^yrt\.(\d+)\.log$')arr=(	"燕睿涛" \    "yanruitao" \    "today is a good day!")

数组的使用
len=${#arr[@]}	#返回的是数组元素的个数echo ${arr[0]}	#数组中的第一个元素,这个和其他语言的数组类似,下表从0开始echo ${arr[2]}	#数组中的第3个元素

实际的例子
[[email protected]_runtime sh]$ arr=(> "燕睿涛"> "http:\/\/www\.baidu\.com\/(\d+)\.html"> "yanruitao"> "lulu"> "yrt"> )[[email protected]_runtime sh]$ echo ${#arr[@]}5[[email protected]_runtime sh]$ echo ${arr[1]}http:\/\/www\.baidu\.com\/(\d+)\.html[[email protected]_runtime sh]$ echo ${arr[0]}燕睿涛[[email protected]_runtime sh]$ echo ${arr[5]}[[email protected]_runtime sh]$

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 燕睿涛

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

Deep Search
Deep Search

智能文献、网页检索与分析工具。AI赋能,洞悉万象,让知识检索与总结触手可及

下载
  • 双小括号[(())]里面是可以直接使用大于小于号进行比较(>、=),而且不需要“坑爹”的空格,用于数学计算
  • 单中括号([])里面比较必须使用-gt、-lt、-ne、-eq这些运算符,而且必须要有严格的空格要求
  • 双中括号([[]])里面比较可以使用>、、

shell中的括号

#看看小括号的用法,首先是在for循环里面,相当于还是数学计算[[email protected]_runtime ad]$ for((a=0;a<10;a++))> do> echo $a> done0123456789#对变量进行++,还是相当于数序运算[[email protected]_runtime ad]$ i=1[[email protected]_runtime ad]$ echo $i1[[email protected]_runtime ad]$ let i++[[email protected]_runtime ad]$ echo $i2[[email protected]_runtime ad]$ ((i++))[[email protected]_runtime ad]$ echo $i3#数学运算[[email protected]_runtime ad]$ echo 1+21+2[[email protected]_runtime ad]$ echo $((1+2))3#单括号里面是一个命令组,括号中的命令将会新开一个shell顺序执行,所以这个里面相当于一个封闭的空间,里面的变量什么的不能被剩余代码使用[[email protected]_runtime ad]$ a=1[[email protected]_runtime ad]$ (a=3;echo $a)3[[email protected]_runtime ad]$ echo $a1#括号中and的使用if [[ -n "$ret" && $ret -gt 123 ]]...		#[[]]双中括号中只能使用&&,不能使用-aif [ -n "$ret" -a $ret -gt 123 ]...			#[]单中括号中只能使用-a,不能使用&&if(($ret)) && (($ret >123 ))...				#(())双小括号使用&&	

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}[[email protected]_runtime sh]$ echo $?0[[email protected]_runtime sh]$ getId "http://www.baidu.com/123.htl"[[email protected]_runtime sh]$ echo $?1[[email protected]_runtime sh]$ getId "http://www.baidu.com/123.html"123[[email protected]_runtime sh]$ echo $?0    

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

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

杂项知识点

字符串转数组
[[email protected]_runtime sh]$ str="燕睿涛 lulu yrt yanruitao"[[email protected]_runtime sh]$ arr=($str)			#这一步将字符串转化为了数组[[email protected]_runtime sh]$ echo ${arr[*]}燕睿涛 lulu yrt yanruitao[[email protected]_runtime sh]$ echo ${#arr[@]}4

常用判断标志
[ -z STRING ]  “STRING” 的长度为零则为真。  [ -n STRING ] or [ STRING ]  “STRING” 的长度为非零 non-zero则为真。[ -d FILE ]  如果 FILE 存在且是一个目录则为真。[ -a FILE ]  如果 FILE 存在则为真。

linux后台运行相关
& 	#在一个命令的最后加上这个命令,可以将该命令放到后台执行./update.sh 100 500 &ctrl + z		#讲一个正在前台执行的命令放到后台,并且处于暂停状态jobs		#查看当前后台运行的命令jobs -l		#可以显示所有后台任务的PID[[email protected]_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 		#把后台中的命令调至前台继续运行,如果后台有多个命令可以使用`fg %jobnumber`将选中命令调出[[email protected]_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 &[[email protected]_runtime sh]$ fg %2./t.sh 100 300    bg 		#讲一个在后台暂停的命令变成在后台继续执行。同样,如果有多个命令,可以使用bg %jobnumber[[email protected]_runtime sh]$ jobs -l[1]- 11655 Running                 ./t.sh 100 300 &[2]+ 11662 Running                 ./t.sh 100 300 &[[email protected]_runtime sh]$ fg %1./t.sh 100 300^Z[1]+  Stopped                 ./t.sh 100 300[[email protected]_runtime sh]$ jobs -l[1]+ 11655 Stopped                 ./t.sh 100 300[2]- 11662 Running                 ./t.sh 100 300 &[[email protected]_runtime sh]$ bg %1[1]+ ./t.sh 100 300 &[[email protected]_runtime sh]$ jobs -l[1]- 11655 Running                 ./t.sh 100 300 &[2]+ 11662 Running                 ./t.sh 100 300 &kill	#终止进程kill %num	#通过jobs查看的job号,进行杀死kill pid 	#通过进程号杀掉进程ctrl + C 	#终止当前前台的进程

参考文献

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

微信号: love_skills

越努力,越幸运!越幸运,越努力!做上CEO不是梦赢取白富美不是梦屌丝逆袭不是梦就是现在!!加油

相关专题

更多
虚拟号码教程汇总
虚拟号码教程汇总

本专题整合了虚拟号码接收验证码相关教程,阅读下面的文章了解更多详细操作。

29

2025.12.25

错误代码dns_probe_possible
错误代码dns_probe_possible

本专题整合了电脑无法打开网页显示错误代码dns_probe_possible解决方法,阅读专题下面的文章了解更多处理方案。

20

2025.12.25

网页undefined啥意思
网页undefined啥意思

本专题整合了undefined相关内容,阅读下面的文章了解更多详细内容。后续继续更新。

37

2025.12.25

word转换成ppt教程大全
word转换成ppt教程大全

本专题整合了word转换成ppt教程,阅读专题下面的文章了解更多详细操作。

6

2025.12.25

msvcp140.dll丢失相关教程
msvcp140.dll丢失相关教程

本专题整合了msvcp140.dll丢失相关解决方法,阅读专题下面的文章了解更多详细操作。

2

2025.12.25

笔记本电脑卡反应很慢处理方法汇总
笔记本电脑卡反应很慢处理方法汇总

本专题整合了笔记本电脑卡反应慢解决方法,阅读专题下面的文章了解更多详细内容。

6

2025.12.25

微信调黑色模式教程
微信调黑色模式教程

本专题整合了微信调黑色模式教程,阅读下面的文章了解更多详细内容。

5

2025.12.25

ps入门教程
ps入门教程

本专题整合了ps相关教程,阅读下面的文章了解更多详细内容。

4

2025.12.25

苹果官网入口直接访问
苹果官网入口直接访问

苹果官网直接访问入口是https://www.apple.com/cn/,该页面具备0.8秒首屏渲染、HTTP/3与Brotli加速、WebP+AVIF双格式图片、免登录浏览全参数等特性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

218

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Excel 教程
Excel 教程

共162课时 | 9.7万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号