加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

最全shell脚本语句语法使用(超详细)

发布时间:2020-12-16 01:42:48 所属栏目:安全 来源:网络整理
导读:博文大纲: 1、对谈式脚本——read语句 2、shell脚本中的测试的字符 3、判断脚本举例 4、条件判断——if语句 5、条件判断——case语句 6、条件判断——function函数结合case语句 7、循环判断——while、until语句 8、固定循环——for...do...done语句 9、循

博文大纲:

  • 1、对谈式脚本——read语句
  • 2、shell脚本中的测试的字符
  • 3、判断脚本举例
  • 4、条件判断——if语句
  • 5、条件判断——case语句
  • 6、条件判断——function函数结合case语句
  • 7、循环判断——while、until语句
  • 8、固定循环——for...do...done语句
  • 9、循环——cut、set结合for语句
  • 10、其他脚本类型举例
  • 11、shell脚本的追踪与debug

shell脚本的编写越规范越好,那么通常在每个shell脚本的开头几行会有如下几个方面相关的注释信息:

  • 脚本使用的shell,如/bin/bash;
  • 脚本的版本信息;
  • 脚本的作者与联络方式;
  • 脚本的history(编写时间记录);
  • 脚本内较特殊的指令,使用绝对路径的方式来下达;
  • 脚本运行是需要的环境变量预先宣告与设置。

废话不多说,直接上使用语法案例:

1、对谈式脚本——read语句

需求①:
first name 与 2. last name,最后并且在屏幕上显示:“Your full name is: ”的内容:

[[email?protected] ~]# vim 1.sh   #编辑脚本,内容如下

#!/bin/bash
echo -e "yong lai xian shi wen jian full name:n"
read -p "qing shu ru fir filename:" firname
read -p "qing shu ru sec filename:" secname
echo -e "nyour full name is ${firname}${secname}."
#其中echo后面的“n”表示换行
[[email?protected] ~]# sh 1.sh      #执行脚本
yong lai xian shi wen jian full name:

qing shu ru fir filename:lv           #手动输入文件名开头
qing shu ru sec filename:jian         #手动输入文件名结尾

your full name is lvjian.     #它将自动将开头和结尾结合起来并输出

需求②:
假设我想要创建三个空的文件(通过touch),filename最开头由当前用户输入决定,假设使用者输入 filename 好了,那今天的日期是 2019/08/25 ,我想要以前天、昨天、今天的日期来创建这些文件。

[[email?protected] ~]# vim 2.sh     #编辑脚本

#!/bin/bash
echo -e "yi ci chuang jian san ge file.n"
read -p "qing shu ru filename:" filename
filename=${filename:-file}
date1=$(date --date ‘1 days ago‘ +%Y%m%d)
date2=$(date --date ‘2 days ago‘ +%Y%m%d)
date3=$(date +%Y%m%d)
file1="${filename}${date1}"
file2="${filename}${date2}"
file3="${filename}${date3}"
touch "${file1}"
touch "${file2}"
touch "${file3}"
[[email?protected] ~]# sh 2.sh   #执行脚本
yi ci chuang jian san ge file.

qing shu ru filename:lv           #输入自定义的文件名开头

[[email?protected] ~]# find /root -name "lv*"   #查看是否创建成功
/root/lv20190825
/root/lv20190827
/root/lv20190826

需求②:
如果我们要使用者输入两个变量,然后将两个变量的内容相乘,最后输出相乘的结果。

[[email?protected] ~]# vim 3.sh      #编辑脚本

#!/bin/bash
echo -e "nzhe shi yi ge suan cheng fa de jiao ben:n"
read -p "qing shu ru yi ge shu zi:" A
read -p "qing shu ru di er ge shu zi:" B
sum=`echo "scale=4; ${A} * ${B}" | bc`
echo -e "n${A}x${B} ==> ${sum}."
[[email?protected] ~]# sh 3.sh        #执行脚本

zhe shi yi ge suan cheng fa de jiao ben:

qing shu ru yi ge shu zi:3        #输入第一个数
qing shu ru di er ge shu zi:4     #输入第二个数

3x4 ==> 12.                           #输出的结果

2、shell脚本中的测试的字符

上面所有的测试都是通过test进行的,可以使用“[ ]”来代替,将要测试的类型及指定的名字写在“[ ]” 即可,但是中括号里面两边必须有空格。(推荐使用中括号“[ ]”)

3、判断脚本举例

需求①:

  1. 这个文件是否存在,若不存在则给予一个“Filename does not exist”的讯息,并中断程序;
  2. 若这个文件存在,则判断他是个文件或目录,结果输出“Filename is regular file”或“Filename is directory”
  3. 判断一下,执行者的身份对这个文件或目录所拥有的权限,并输出权限数据!
[[email?protected] ~]# vim 4.sh     #编辑脚本

#!/bin/bash
echo "yong lai ce shi wen jian huo dirctory."
read -p "qing shu ru yi ge wen jian ming:" filename
test -z ${filename} && echo -e "nqing shu ru yi ge filename." && exit 0
test ! -e ${filename} && echo "filename does not exitst." && exit 0
test -f ${filename} && filetype="file"
test -d ${filename} && filetype="directory"
test -r ${filename} && prem="read"
test -w ${filename} && prem="${prem}+write"
test -x ${filename} && prem="${prem}+exe"
echo -e "nthis is a ${filetype},it‘s perm.. is ${prem}."
[[email?protected] ~]# sh 4.sh      #执行脚本
yong lai ce shi wen jian huo dirctory.
qing shu ru yi ge wen jian ming:/root    #输入一个目录名

this is a directory,it‘s perm.. is read+write+exe.      #脚本执行后输出的结果
[[email?protected] ~]# sh 4.sh     #再执行脚本
yong lai ce shi wen jian huo dirctory.
qing shu ru yi ge wen jian ming:/etc/passwd      #输入一个文件

this is a file,it‘s perm.. is read+write.       #脚本执行后输出的结果

需求②:

1、当执行一个程序的时候,这个程序会让用户输入Y或N。
2、如果使用者输入Y或y时,就会显示OK,continue.
3、如果使用者输入N或n时,就会显示ON,interrupt.
4、如果不是Y/y/N/n之内的字符,那么将会死循环这个脚本,直到手动退出,或输入正确的值(其实稍作改动,可以改为若默认按回车的话可以等于输入“Y”,自行研究吧)。

[[email?protected] ~]# vim 5.sh     #编辑脚本

#!/bin/bash
while [ "${yn}" != "Y" -o "${yn}" != "y" -o "${yn}" != "N" -o "${yn}" != "n" ]
do
read -p "qing shu ru ‘Y‘ or ‘N‘:" yn
[ "${yn}" == "Y" -o "${yn}" == "y" -o "${yn}" == "" ] && echo -e "nOK,continue." && exit 0
[ "${yn}" == "N" -o "${yn}" == "n" ] && echo -e "nON,interrupt." && exit 0
done
[[email?protected] ~]# sh 5.sh     #下面是多次执行脚本,测试是否达到需求
qing shu ru ‘Y‘ or ‘N‘:

OK,continue.
[[email?protected] ~]# sh 5.sh 
qing shu ru ‘Y‘ or ‘N‘:y

OK,continue.
[[email?protected] ~]# sh 5.sh
qing shu ru ‘Y‘ or ‘N‘:n

ON,interrupt.
[[email?protected] ~]# sh 5.sh
qing shu ru ‘Y‘ or ‘N‘:u
qing shu ru ‘Y‘ or ‘N‘:i
qing shu ru ‘Y‘ or ‘N‘:N

ON,interrupt.

需求③:
1、程序的文件名为何?
2、共有几个参数?
3、若参数的个数小于 2 则告知使用者参数数量太少
4、全部的参数内容为何?
5、第一个参数为何?
6、第二个参数为何

[[email?protected] ~]# vim 6.sh     #编辑脚本如下

#!/bin/bash
echo -e "ncheng xu de wen jian ming shi ${0}"
echo -e "nyi gong you $# ge can shu."
[ $# -lt 2 ] && echo "can shu tai shao le ." && exit 0
echo  "your whole parameter is ==> ‘$*‘."
echo "the 1st parameter ${1}."
echo "the 2nd parameter ${2}."
[[email?protected] ~]# sh 6.sh a b c     #执行脚本

cheng xu de wen jian ming shi 6.sh

yi gong you 3 ge can shu.
your whole parameter is ==> ‘a b c‘.
the 1st parameter a.
the 2nd parameter b.
[[email?protected] ~]# sh 6.sh a      #再次执行脚本

cheng xu de wen jian ming shi 6.sh

yi gong you 1 ge can shu.
can shu tai shao le .
#为了不为难自己,上面我用了拼音,多多体谅[ 捂脸 ]。

需求④:
查看本机都是否开启了www / ftp / mail服务,并将结果直观的显示出来

[[email?protected] ~]# vim 11.sh 

#!/bin/bash
file="/dev/shm/a.txt"
netstat -anpt  > ${file}
awk -F : ‘{print $4}‘ ${file} | awk ‘{print $1}‘ | grep "80" &> /dev/null
if [ $? -eq 0 ]
        then
        echo -e "www service is upn"
fi
awk ‘{print $4}‘ ${file} | egrep "20|21" &> /dev/null
if [ $? -eq 0 ]
        then
        echo -e "ftp service is upn"
fi
awk ‘{print $4}‘ ${file} | grep "25" &> /dev/null
if [ $? -eq 0 ]
        then
        echo -e "mail service is upn"
fi
[[email?protected] ~]# sh 11.sh     #执行脚本测试
mail service is up

[[email?protected] ~]# systemctl start httpd    #启动www服务再测试
[[email?protected] ~]# sh 11.sh 
www service is up

mail service is up

需求⑤:
都知道脚本后面的第一段是$1,第二段是$2....那么是否可以进行偏移呢,假设让原本的$2变为$1。

[[email?protected] ~]# vim 7.sh      #编辑脚本如下

#!/bin/bash
echo "total parameter number is ==> $#"
echo "your whole parameter is ==> $* "
shift
echo "total parameter number is ==> $#"
echo "your whole parameter is ==> $* "
shift 3
echo "total parameter number is ==> $#"
echo "your whole parameter is ==> $* "
#“上面默认的shift”参数是偏移1个位置,也可以指定偏移的参数,如“shift 3”则表示向后偏移三个
[[email?protected] ~]# sh 7.sh a b c    #执行脚本,并且追加三个参数
total parameter number is ==> 3
your whole parameter is ==> a b c 
total parameter number is ==> 2
your whole parameter is ==> b c 
total parameter number is ==> 2
your whole parameter is ==> b c 
#从输出结果可以发现,偏移是累加的,第一次偏移了默认1位,
#第二次偏移了3位,那么实际已经偏移了原始参数的4位(因为累加)
#但是参数只有三个,所以它会循环偏移,所以结果还是b和c。

关于上面脚本中的“$#”、“$*”的解释可以参考如下解释:

4、条件判断——if语句

需求①:

1、当执行一个程序的时候,这个程序会让用户输入Y或N。
2、如果使用者输入Y或y或者直接按下回车键时,就会显示OK,interrupt.
4、如果不是Y/y/N/n之内的字符,那么将输出“I don‘t know what your choice is”

[[email?protected] ~]# vim 11.sh     #编写脚本

#!/bin/bash
read -p "Please input (Y/N): " yn

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读