shell脚本学习

shell脚本学习

2023年8月2日发(作者:)

shell脚本学习Shell脚本⼀、shell脚本的创建和执⾏1、编写脚本# cd /usr/local/sbin/# vi //内容如下所⽰#! /bin/bash

## This is my first shell script. //说明和⽇期都是注释的内容## Writen by tcq 2021-04-19. //最好写上,⽅便记忆

dateecho "Hello world"2、执⾏脚本[root@shell sbin]# sh //执⾏结果如下:2021年 04⽉ 19⽇ 星期⼀ 21:26:38 CSTHello world还有⼀种执⾏⽅式,⽰例如下:[root@shell sbin]# chmod +x //新建的脚本需要 增加执⾏权限[root@shell sbin]# ./2021年 04⽉ 19⽇ 星期⼀ 21:28:16 CSTHello world加上-x可以查看脚本的执⾏过程,⽰例如下:[root@shell sbin]# sh -x + date2021年 04⽉ 19⽇ 星期⼀ 21:30:41 CST+ echo 'Hello world'Hello world3、命令datedate的⼏个基本⽤法如下所⽰:l date +%Y: 表⽰以四位数字格式打印年份。l date +%y: 表⽰以两位数字格式打印年份。l date +%m: 表⽰⽉份。l date +%d: 表⽰⽇期l date +%H: 表⽰⼩时。l date +%M: 表⽰分钟。l date +%S: 表⽰秒。l date +%w:表⽰星期,结果显⽰0则表⽰周⽇。⽰例:# date +"%Y-%m-%d %H:%M:%S"2021-04-19 21:40:16# date -d "-1 day" +%d //显⽰前⼀天的⽇期18# date -d "-1 hour" +%H //显⽰前⼀⼩时20# date -d "-1 min" +%M //显⽰前⼀分钟43⼆、shell脚本中的变量1、编写⼀个与变量相关的脚本# [root@shell sbin]# vi #! /bin/bash

## In this script we will write many variable## Writen by tcq 2021-04-19

d=`date +%H:%M:%S`echo "The script begin at $d."echo "Now we'll sleep 2 seconds."sleep 2d1=`date +%H:%M:%S`echo "The script end at $d1."执⾏脚本:# ./ script begin at 22:00: we'll sleep 2 script end at 22:00:28.2、数学运算⽰例如下:[root@shell sbin]# vi #! /bin/bash

## This is about math of shell.## Writen by tcq 2021-04-19

a=1b=2sum=$[$a+$b] //数学运算要⽤[ ]括起来,前⾯要加$echo "$a+$b=$sum"

# sh 1+2=33、和⽤户交互[root@shell sbin]# vi #! /bin/bash

## Using 'read' in shell script.## tcq 2021-04-19

read -p "Please input a number:" xread -p "Please input another number:" y

sum=$[$x+$y]echo "The sum of the two numbers is: $sum"执⾏脚本:[root@shell sbin]# sh se input a number:100Please input another number:2The sum of the two numbers is: 1024、shell脚本预设变量[root@shell sbin]# vi #! /bin/bash

sum=$[$1+$2]echo "sum=$sum"

[root@shell sbin]# sh 4 5sum=9上⾯这4和5就是脚本的预设变量,预设变量时没有限制的,$0代表脚本本⾝,⽰例如下:[root@shell sbin]# vi //修改脚本#! /bin/bash

echo "$1 $2 $0"

[root@shell sbin]# sh 1 21 2 //这⾥$0=三、shell脚本中的逻辑判断1、不带else具体格式如下:if 判断语句; then commandfi⽰例脚本如下:[root@shell sbin]# vi //修改脚本#! /bin/bash

echo "$1 $2 $0"

[root@shell sbin]# sh 1 21 2 //这⾥$0=2、带有else具体格式如下:if 判断语句; then commandelse commandfi⽰例脚本如下:[root@shell sbin]# vi #! /bin/bash

read -p "Please input your score:" aif ((a<60)); then echo "You didn't pass the exam."else echo "Good! You pass the exam."fi脚本执⾏结果如下:[root@shell sbin]# sh se input your score:0You didn't pass the exam.

[root@shell sbin]# sh se input your score:90Good! You pass the exam. 3、带有elif具体格式如下:If 判断语句1; then commandelif 判断语句2; then commandelse commandfi⽰例脚本如下:[root@shell sbin]# vi #! /bin/bash

read -p "Please input your score:" aif ((a<60)); then echo "You didn't pass the exam."elif ((a>=60)) && ((a<85)); then echo "Good! You pass the exam."else

echo "Very good! Your score is very high!"fi执⾏结果如下:

判断数值除了⽤(())的形式外,还可以使⽤[],但是不能使⽤<,>,=这样的符号,要使⽤-lt(⼩于)、-gt(⼤于)、-le(⼩于或等于)、-ge(⼤于或等于)、-eq(等于)、-ne(不等于)。⽰例如下:# a=10; if [ $a -lt 5 ]; then echo ok; fi# a=10; if [ $a -gt 5 ]; then echo ok; fiok&&和||的使⽤,使⽤&&时,只有两边都符号才会输出ok,⽽使⽤||时,只要有⼀边符合,就会输出ok,⽰例如下:# a=10; if [ $a -gt 5 ] && [ $a -lt 14 ]; then echo ok; fiok# a=10; if [ $a -gt 5 ] || [ $a -lt 5 ]; then echo ok; fiok4、和⽂档相关的判断(1)If常⽤的选项有以下⼏个:² -e:判断⽂件或⽬录是否存在。² -d:判断是不是⽬录以及是否存在。² -f:判断是不是普通⽂件以及是否存在。² -r:判断是否有读权限。² -w:判断是否有写权限。² -x:判断是否有执⾏权限。² -z:字符串空为真。² -s:⽂件⼤⼩不为0为真。(2)使⽤if判断时的具体格式如下:if [ -e filename ]; then commandfi⽰例代码如下:# if [ -d /home/ ]; then echo ok; fiok

5、case判断具体格式如下:case 变量 invalue1) command ;;value2) command ;;*) command ;;esac编写⼀个判断是奇数还是偶数的脚本:[root@shell sbin]# vi #! /bin/bash

read -p "Input a number:" na=$[$n%2]case $a in 1) echo "The number is odd." ;; 0) echo "The number is even." ;; *) echo "It's not a number!" ;;esac执⾏结果如下:

四、Shell脚本中的循环1、for循环写⼀个简单的for循环脚本,实例如下:[root@shell sbin]# vi #! /bin/bash

for i in `seq 1 5`; do //seq 1 5 表⽰1到5的⼀个序列 echo $iDone执⾏脚本:[root@shell sbin]# sh 12345这个脚本就是for循环的基本结构,具体格式如下所⽰:for 变量名 in 循环的条件; do commanddone循环的条件可以是⼀组字符串或者数字(⽤⼀个或者多个空格隔开),也可以是⼀条命令的执⾏结果,⽰例如下:# for i in 1 2 3 a b; do echo $i; done123ab也可以引⽤系统命令的执⾏结果(如seq 1 5),但是必须⽤反引号括起来,⽰例如下:# for file in `ls`; do echo $file; 2、while循环基本格式如下:while 条件; do commanddone⽰例脚本如下:[root@shell sbin]# vi #! /bin/bash

a=5while [ $a -ge 1 ]; do echo $a a=$[$a-1]done执⾏结果如下:[root@shell sbin]# sh 54321如果⽤冒号代替循环条件,就可以做到死循环,⽰例如下:[root@shell sbin]# cat #! /bin/bash while :; do echo "hahahahahahahaha"done执⾏结果如下:

五、shell脚本中的函数1、写⼀个带有函数功能的脚本[root@shell sbin]# vi #! /bin/bash

function sum(){ sum=$[$1+$2] echo $sum}

sum $1 $2执⾏结果如下:[root@shell sbin]# sh 1 中的sum()为⾃定义的函数,函数⼀定要写在最前⾯,不能出现在中间或者最后,因为函数是被调⽤的,所有得先出现,才能被调⽤。六、shell脚本中的中段和继续1、break在使⽤break时都是打破循环的意思,break只打破它所在的那⼀层循环,它的上层循环不受影响,⽰例如下:[root@shell sbin]# vi #! /bin/bash

for i in `seq 1 5`do echo $i if [ $i == 3 ] then break fi echo $idoneecho aaaaa执⾏结果如下:

2、continuecontinue和break不同的是当shell中遇到continue时,结束的不是整个循环,⽽是本次循环,⽰例如下:[root@shell sbin]# vi #! /bin/bashfor i in `seq 1 5`do echo $i if [ $i == 3 ] then continue fi echo $idoneecho $i如上脚本所⽰,当i=3时,contunue出现,结束本次循环,continue后⾯的语句不再执⾏,继续下⼀次循环,执⾏结果如下:[root@shell sbin]# sh 11223

4

4553、exit和break、continue相⽐,exit的作⽤范围更⼤,直接退出整个脚本,⽰例如下:[root@shell sbin]# vi #! /bin/bashfor i in `seq 1 5`do echo $i if [ $i == 3 ] then exit fi echo $idoneecho aaaaaa执⾏结果如下:[root@shell sbin]# sh 11223由上可以看出,当i=3时,直接退出脚本,后⾯没有执⾏。七、shell中的数组1、定义数组[root@shell ~]# a=(1 2 3 4 5) //定义数组[root@shell ~]# echo ${a[@]}1 2 3 4 5[root@shell ~]# echo ${#a[@]} //获取数组的元素个数5[root@shell ~]# echo ${a[2]} //读取第三个元素,数组从0开始3[root@shell ~]# echo ${a[*]} //等同于 ${a[@]} 显⽰整个数组1 2 3 4 5

2、数组赋值[root@shell ~]# a[1]=100[root@shell ~]# echo ${a[@]} //@和*作⽤相同,都是打印所有元素1 100 3 4 5[root@shell ~]# a[5]=2 //如果下标不存在则会⾃动添加⼀个元素[root@shell ~]# echo ${a[@]}1 100 3 4 5 2[root@shell ~]# unset a[5] //删除数组[root@shell ~]# echo ${a[@]}1 100 3 4 5 //a[5]已经被删除[root@shell ~]# unset a[root@shell ~]# echo ${a[@]}        //不显⽰结果,数组已经被删除3、数组分⽚[root@shell ~]# a=(`seq 1 5`)[root@shell ~]# echo ${a[@]:0:3} //从第⼀个元素开始,截取3个1 2 3[root@shell ~]# echo ${a[@]:2:3} //从第三个元素开始,截取3个3 4 5[root@shell ~]# echo ${a[@]:0-3:2} //从倒数第3个元素开始,截取2个3 44、数组替换[root@shell ~]# echo ${a[@]/3/100}1 2 100 4 5a=(${a[@]/3/100})

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1690919891a463655.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信