case分支 shell函数 字符串的处理

目录

 

case分支

编写nginx脚本

shell函数

什么是函数?

使用函数的好处?

服务脚本中的函数应用

如何定义一个函数

脚本中断 

编写脚本,从1-20中找到6的倍数

编写脚本,for嵌套

字符串处理

1.字符串截取

编写一个脚本,随机截取字符串

编写脚本,随机截取八位密码

2.字符串替换

3.字符串删除

编写批量修改扩展名的脚本(使用字符串的删除功能)

编写九九乘法表脚本


case分支

case分支,功能类似于if,不如if强大,但代码比if精简

case  变量  in

模式1)

      指令1;;

模式2)

      指令2;;

*)

    指令n

esac

 

root@server0 opt]# vim test01.sh
#!/bin/bash
case  $1  in
a)
        echo  "aaa";;
b)
        echo  "bbb";;
*)
        echo   "请输入a或b"
esac

 

[root@server0 opt]# vim test01.sh

#!/bin/bash       
case  $1  in
t|T)                                    #输入小写t或大写T都可以
        touch  $2";;
m|M|mm) 
        echo  "2";;
r) 
        echo   "请输入t|m/r"
esac

编写nginx脚本

  • 1.在真机上找到nginx源码包,远程复制到虚拟机server
student@room9pc01 ~]$ cd  /linux-soft/02
[student@room9pc01 02]$ ls  
ceph10.iso  exam  exam.tar.gz  grade  grade.tar.gz  lnmp_soft.tar.gz
[student@room9pc01 02]$ scp  lnmp_soft.tar.gz   root@172.25.0.11:/opt
lnmp_soft.tar.gz                                                       100%  155MB  18.0MB/s   00:08   
  • 2.在虚拟机server上解压到当前文件
[root@server0 opt]# ls
lnmp_soft.tar.gz  rh  test01.sh

[root@server0 opt]# tar -xf  lnmp_soft.tar.gz   -C  .  #解压到当前

[root@server0 opt]# ls
lnmp_soft  lnmp_soft.tar.gz  rh  test01.sh

[root@server0 opt]# ls lnmp_soft
apache-tomcat-8.0.30.tar.gz             php-fpm-5.4.16-42.el7.x86_64.rpm
apache-tomcat-9.0.6.tar.gz              php-mbstring-5.4.16-42.el7.x86_64.rpm
buffer.sh                               php_scripts
DenyHosts-2.6.tar.gz                    pssh-2.3.1-5.el7.noarch.rpm
install_lnmp.sh                         python-docutils-0.11-0.2.20130715svn7687.el7.noarch.rpm
jenkins-2.164.3-1.1.noarch.rpm          redis-3.0.6.tar.gz
libevent-devel-2.0.21-4.el7.x86_64.rpm  tomcat_session
nginx-1.10.3.tar.gz                     varnish-5.2.1.tar.gz
nginx-1.12.2.tar.gz                     vpn
nginx-1.15.8.tar.gz                     wordpress.zip
nginx.conf.bak                          www_template.zip
nginx.spec                              zabbix-3.4.4.tar.gz
php-bcmath-5.4.16-42.el7.x86_64.rpm
[root@server0 opt]# cp  lnmp_soft/nginx-1.12.2.tar.gz   .    #复制到当前

[root@server0 opt]# ls
lnmp_soft  lnmp_soft.tar.gz  nginx-1.12.2.tar.gz  rh  test01.sh
  • 3.部署安装nginx脚本
[root@server0 opt]# vim  test02.sh
#!/bin/bash
yum  -y  install   gcc  openssl-devel   pcre-devel   #安装源码编译包以及依赖包们
tar  -xf nginx-1.12.2.tar.gz             #解压
cd  nginx-1.12.2                         #切换到nginx的默认路径
./configure                              #源码编译安装
make
make install
[root@server0 opt]# bash test02.sh

[root@server0 opt]# cd /usr/local/nginx/sbin/    #切换到nginx程序所在目录

[root@server0 sbin]# ls                          #查看,nginx为可执行文件
nginx
[root@server0 sbin]# ./nginx                     #执行nginx程序
[root@server0 sbin]# systemctl stop firewalld    #关闭防火墙
     #此时在真机上打开浏览器访问172.25.0.11可以看到nginx的欢迎界面

natestat  

netstat命令可以查看系统中启动的端口信息,该命令常用选项如下:

-n        以数字格式显示端口号

-t          显示TCP连接的端口

-u         显示UDP连接的端口

-l          显示服务正在监听的端口信息,如httpd启动后,会一直监听80端口

-p         显示监听端口的服务名称是什么(也就是程序名称)

-ntulp   查看该服务所有信息

[root@server0 sbin]# netstat   -ntulp  |  grep nginx     #查看nginx的服务信息
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      550/nginx: master p 
       
  • 4.写nginx状态的脚本
[root@server0 sbin]# vim test03.sh
#!/bin/bash
case $1  in
kai)
        netstat  -ntulp  | grep  -q  nginx
        if [  $?  -eq  0 ];then
                exit
        fi
        /usr/local/nginx/sbin/nginx;;              #开启nginx服务
guan)
        netstat  -ntulp  | grep  -q  nginx
        if  [ $?  -ne   0  ];then
                 exit 
        fi
        /usr/local/nginx/sbin/nginx  -s  stop;;    #关闭nginx服务
res)
        /usr/local/nginx/sbin/nginx  -s  stop      #重起nginx服务(先关闭再开启)
        /usr/local/nginx/sbin/nginx;;              #case分支在命令输完再写双分号
cha)
        netstat  -ntulp  | grep  -q  nginx        #grep  -q  等同于/dev/null,把输出信息扔到黑洞
        [ $? -eq 0  ]&& echo -e  "\033[32m服务已开启\033[0m" || echo -e "\033[31m服务未开启\033[0m";;          #当服务开启时候显示绿色,未开启显示红色
*)  
        echo "请输入kai|guan|res|cha"
esac
[root@server0 sbin]# bash test03.sh  guan         #关闭nginx服务

[root@server0 sbin]# bash test03.sh  kai          #开启nginx服务

[root@server0 sbin]# bash test03.sh  res          #重起nginx服务

[root@server0 sbin]# bash test03.sh  cha          #查看当前nginx服务状态
服务未开启

[root@server0 sbin]# echo  -e  "\033[31mABCD\033[0m"      #\033[   要改颜色  \033[0m改为默认     #31m是红色
ABCD
[root@server0 sbin]# echo  -e  "\033[32maaaa\033[0m"        #32m为绿色
aaaa
[root@server0 sbin]# echo  -e  "\033[33maaaa\033[0m"        #33m为黄色
aaaa
[root@server0 sbin]# echo  -e  "\033[34mbbb\033[0m"          #34m为蓝色
bbb
[root@server0 sbin]# echo  -e  "\033[35mffff\033[0m"             #35m为紫色
ffff


31-35改前景色
41-45改背景色
91-95改为高亮



shell函数

什么是函数?

可以将公共的语句块定义成一个名称,来达到精简脚本的目的

使用函数的好处?

  • 使脚本代码更简洁,增强易读性
  • 提高shell脚本的执行效率

服务脚本中的函数应用

  • 适用于比较复杂的启动/终止控制操作
  • 方便在需要时多次调用

如何定义一个函数

function   函数名{

       命令序列

        ....

}

或者

函数名(){

    命令序列

    .....

  • 调用已定义的函数

格式: 函数名

先定义了才能调用,就好比脚本的"内部命令"

  • 函数传值

格式: 函数名  值1  值2....

传递的值作为函数的"位置参数"

[root@server0 opt]#  vim  test04.sh#!/bin/bash
cecho() {                              #在函数中定义变量,改变输出参数的颜色
echo -e  "\033[$1m$2\033[0m"
}

cecho  32  qqqq                       
cecho  33  zzzz
cecho  34  ssss
cecho  91  aaaa
[root@server0 sbin]# bash test04.sh
qqqq                                    #我是绿色的
zzzz                                    #我是黄色
ssss                                    #我是蓝色
aaaa                                    #我是高亮红


脚本中断 

类型含义
break跳出当前所在的循环体,执行循环体后的语句块
continue跳过循环体内余下的语句,重新判断条件以决定是否需要执行下一次循环
exit退出脚本,默认返回值为0

编写脚本,可以帮助用户计算整数的求和

[root@server0 opt]# vim test05.sh
#!/bin/bash
x=0
while :
do
read -p  "请输入一个数字求和,0是结束" n
[ $n  -eq 0  ]&& break         #还要继续循环之后的任务,所以要用break
let x+=n
done
echo $x

编写脚本,从1-20中找到6的倍数

[root@server0 opt]# vim test06.sh
#!/bin/bash
for i in {1..20}
do
        x=$[i%6]
        [ $x  -ne 0  ] && continue        #当$x不等于0,则终止当前循环,继续下一次循环
        echo $i
done
[root@server0 opt]# bash test06.sh
6
12
18

编写脚本,for嵌套

[root@server0 opt]# vim test07.sh
#!/bin/bash
for i in {1..3}
do
        for i in {1..3}
        do
        echo -n  "X"          #echo  -n  不换行输出
        done
        echo
done
[root@server0 opt]# bash test07.sh
XXX
XXX
XXX


字符串处理

1.字符串截取

  • 使用${}表达式,格式:  ${变量名:起始位置:长度}     (起始位置的编号从0开始,可省略)

编写一个脚本,随机截取字符串

[root@server0 opt]# vim test08.sh
#!/bin/bash
x=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
n=$[RANDOM%62]
echo ${x:n:1}
[root@server0 opt]# bash test08.sh
7
[root@server0 opt]# bash test08.sh
y
[root@server0 opt]# bash test08.sh
J
[root@server0 opt]# bash test08.sh
r
[root@server0 opt]# bash test08.sh
r

编写脚本,随机截取八位密码

[root@server0 opt]# vim test08.sh
#!/bin/bash
x=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
for i in {1..8}
do
n=$[RANDOM%62]
pass=${x:n:1}
pass1=$pass1$pass
done
echo $pass1
pass1=                                 #设置pass1的值为空,每次运行后自动还原pass1的值
[root@server0 opt]# bash test08.sh
BTfQh0E5
[root@server0 opt]# bash test08.sh
wjpBZPQp
[root@server0 opt]# . test08.sh
wQw6XEBR
[root@server0 opt]# . test08.sh
b8ag5frW

2.字符串替换

  • 使用${}表达式,格式:  替换第1个匹配结果  ${变量名/old/new}
  •                                    替换全部匹配结果  ${变量名//old/new}
  •                                   在一定程度上可以删除某个结果${变量名/old/}

3.字符串删除

  • 使用${}表达式,格式:  ${变量名#  }  从左往右删除
[root@server0 opt]# a=`head -1  /etc/passwd`
[root@server0 opt]# echo $a
root:x:0:0:root:/root:/bin/bash
[root@server0 opt]# echo ${a#root}                 #删除到第一个root
:x:0:0:root:/root:/bin/bash
[root@server0 opt]# echo ${a##*root}               #删除到最后一个root
:/bin/bash
[root@server0 opt]# echo ${a##*/}                  #删除到最后一个/
bash
  • 使用${}表达式,格式: ${变量名%    } 从右往左删除
[root@server0 ~]#  a=`head -1  /etc/passwd`
[root@server0 ~]# echo $a
root:x:0:0:root:/root:/bin/bash         
[root@server0 ~]# echo ${a%bash}                  #删除到第一个bash
root:x:0:0:root:/root:/bin/
[root@server0 ~]# echo ${a%/*}                    #删除到第一个/bash
root:x:0:0:root:/root:/bin
[root@server0 ~]# echo ${a%%/*}                   #删除到最后一个/
root:x:0:0:root:
[root@server0 ~]# echo ${a%%:*}                   #删除到最后一个:
root

 按条件掐头去尾

  • 字符串掐头:  从左向右,最短匹配删除    格式:   ${变量名#*关键词}  
  •                      从左向右,最长匹配删除    格式:   ${变量名##*关键词}
  • 字符串去尾:  从右向左,最短匹配删除    格式:  ${变量名%关键词*}
  •                      从右向左,最长匹配删除    格式:  ${变量名%%关键词*}

编写批量修改扩展名的脚本(使用字符串的删除功能)

[root@server0 opt]# touch abc{1..10}.txt     #批量创建文件
[root@server0 opt]# ls
abc10.txt  abc3.txt  abc6.txt  abc9.txt          nginx-1.12.2         test01.sh  test06.sh  test09.sh
abc1.txt   abc4.txt  abc7.txt  lnmp_soft         nginx-1.12.2.tar.gz  test02.sh  test07.sh
abc2.txt   abc5.txt  abc8.txt  lnmp_soft.tar.gz  rh                   test05.sh  test08.sh
[root@server0 opt]# vim  test09.sh
#!/bin/bash
for i in  `ls *.$1`
do
n=${i%.*}                                    #n是去除了扩展名的文件名
mv  $i  $n.$2                               #将原文件修改为文件名.doc
done
[root@server0 opt]# bash test09.sh    txt   doc
[root@server0 opt]# ls 
abc10.doc  abc3.doc  abc6.doc  abc9.doc          nginx-1.12.2         test01.sh  test06.sh  test09.sh
abc1.doc   abc4.doc  abc7.doc  lnmp_soft         nginx-1.12.2.tar.gz  test02.sh  test07.sh
abc2.doc   abc5.doc  abc8.doc  lnmp_soft.tar.gz  rh                   test05.sh  test08.sh


编写九九乘法表脚本

[root@server0 opt]# vim test10.sh
#!/bin/bash
for i in {1..9}
do
        for j in `seq $i`
        do
        echo -n "${i}x$j=$[i*j] "
        done
        echo
done
[root@server0 opt]# bash test10.sh
1x1=1 
2x1=2 2x2=4 
3x1=3 3x2=6  3x3=9 
4x1=4 4x2=8  4x3=12 4x4=16 
5x1=5 5x2=10 5x3=15 5x4=20 5x5=25 
6x1=6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36 
7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49 
8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64 
9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值