第十五7.26(shell脚本)

1.编写一个简单的脚本,并运行

[root@mj ~]# vim helloworld.sh
#! /bin/bash
echo "hello world"
​
[root@mj ~]# source helloworld.sh 
hello world
[root@mj ~]# chmod +x helloworld.sh 
[root@mj ~]# ./helloworld.sh 
hello world

2.nginx一键安装

[root@shell ~]# vim nginx.sh 
#!/bin/bash
yum -y install gcc gcc-c++ make pcre-devel openssl-devel wget
cd /usr/local/src/
wget 'https://nginx.org/en/download.html'
tar xf nginx-1.22.1.tar.gz
cd nginx-1.22.1
./configure --prefix=/usr/local/nginx
make -j 4
make install
[root@shell ~]#chmod +x nginx.sh 
[root@shell ~]#  ./nginx.sh 

3.数字判断

-eq:equal,等于,一般用于 [ $? -eq 0 ],也就是判断上条命令返回值等于 0,直接数字 -eq 数字也可以equals

-ne:not equal,不等于,一般用于 [ $? -ne 0 ],判断上条命令返回值不等于0

-gt:greater than,大于

-ge:greater or equal,大于或等于

-lt:less than,小于

-le:less or equal,小于或等于

1.判断数字是否相等
[root@shell ~]# vim if.sh
#!/bin/bash
echo ":1"
read a
echo ":2"
read  b
​
if [ $a -eq  $b ]; then
           echo "两个数字相等"
else
           echo "两个数字不相等"
fi
[root@shell ~]# bash if.sh 
:1
2
:2
2
两个数字相等
[root@shell ~]# bash if.sh 
:1
1
:2
2
两个数字不相等

2.创建网络是否畅通
[root@shell ~]# vim ping.sh
#!/bin/bash
read -p "输入网址:" weburl
ping -c 3 $weburl &> /dev/null
​
if  [ $? -eq 0 ]; then 
          echo "网络通畅" 
else
          echo "网络卡死"
​
fi
[root@shell ~]# bash ping.sh 
输入网址:www.baidu.com
网络通畅
​

4.字符串格式

[ 字符串1 == 字符串2 ] 字符串内容相同

[ 字符串1 != 字符串2 ] 字符串内容不同

[ -z字符串 ] 字符串内容为空

[ -n字符串 ] 字符串内容不为空

[root@shell ~]# aaa="abc"
[root@shell ~]# echo $aaa
abc
[root@shell ~]# test $aaa == "abc"
[root@shell ~]# echo $?
0
[root@shell ~]# test $aaa == "aaaa"
[root@shell ~]# echo $?
1
[root@shell ~]# unset aaa
[root@shell ~]# echo $aaa
空值
[root@shell ~]# test -z $aaa
[root@shell ~]# echo $?
0
[root@shell ~]# echo $aaa
空值
[root@shell ~]# [ -z aaa ]
[root@shell ~]# echo $?
1

1.字符串判断登录账户
[root@shell ~]# vim login001.sh
read -p "请输入账号" usr
if [ $usr == "admin" ]; then
   echo "欢迎登录: $user"
   else
   echo "账号或者密码错误"
fi  
[root@shell ~]# bash login001.sh 
请输入账号admin
欢迎登录:
[root@shell ~]# bash login001.sh 
请输入账号as
账号或者密码错误

2.创建rpm检查nginx是否安装
[root@shell ~]# vim nginxrpm.sh
#!/bin/bash
rpm -qa|grep nginx
#echo $?
​
yum -y install epel.relese
​
if [ $? -eq 1 ]; then
    yum -y install nginx
else
    yum -y remove nginx 
    yum -y install nginx
fi

5.文件、目录、权限的判断

1.或运算判断
[root@shell ~]# vim test002.sh
#!/bin/bash
read -p "请输入名称" name
​
if [ $name == "1" ]||[ $name == "2" ];then
   echo "友好"
else
   echo "不友好"
fi
[root@shell ~]# bash test002.sh 
请输入名称1
友好
[root@shell ~]# bash test002.sh 
请输入名称2
友好
[root@shell ~]# bash test002.sh 
请输入名称3
不友好
​

2.与运算判断
 [root@localhost test]# vim yu.sh
 #!/bin/bash
 read -p "请输⼊⼀个数值:" age
 if [ $age -gt 30 ]&&[ $age -lt 80 ];then # 这两个条件都得满足,大于30且小于80,可使用[ $age -gt 30 -a $age -lt 80 ]
  echo "age>30 and age<80"
  echo "working"
 else 
  echo "in else"
 fi 
 [root@localhost test]# sh ./yu.sh
 请输⼊⼀个数值:60
 age>30 and age<80
 working
 [root@localhost test]# sh ./yu.sh
 请输⼊⼀个数值:90
 in else
 

3.混合判断
[root@shell ~]# vim eliftest.sh 
#!/bin/bash
echo "1新增文件 2删除文件 3修改文件 4查找文件"
read -p "请输入序号选择功能" m
if [ $m == 1 ]; then
    touch aaa.txt
elif [ $m == 2 ]; then
    rm -rf aaa.txt
else
   echo "其他功能正在开发"
fi
[root@shell ~]# bash eliftest.sh 
1新增文件 2删除文件 3修改文件 4查找文件
请输入序号选择功能1
[root@shell ~]# ls
aaa.txt  

6.多重判断语法elif

if 多分支语句结构

if 条件1; then

命令,条件1成⽴执⾏

elif 条件2;then

命令,条件1不成⽴,条件2成⽴执⾏

elif 条件3;then

命令,条件1不成⽴,条件2不成⽴,条件3成⽴执⾏

else

命令 ,以上条件都不成⽴执⾏

fi

 [root@localhost test]# vim fs.sh
 #!/bin/bash
 #分数等级评定
 read -p "请输⼊您的分数(0-100):" fs
 if [ $fs -ge 0 -a $fs -lt 60 ];then
  echo "$fs分,不及格!"
 elif [ $fs -ge 60 -a $fs -lt 70 ];then
  echo "$fs分,及格!"
 elif [ $fs -ge 70 -a $fs -lt 85 ];then
  echo "$fs分,良好!"
 elif [ $fs -ge 85 -a $fs -le 100 ];then
  echo "$fs分,优秀!"
 else 
  echo "您输⼊的分数有误!"
 fi 
 [root@localhost test]# sh ./fs.sh
 请输⼊您的分数(0-100):20
 20分,不及格!
 [root@localhost test]# sh ./fs.sh
 请输⼊您的分数(0-100):85
 85分,优秀!
 [root@localhost test]# sh ./fs.sh
 请输⼊您的分数(0-100):70
 70分,良好!
 [root@localhost test]# sh ./fs.sh
 请输⼊您的分数(0-100):123
 您输⼊的分数有误!
​

7.多重判断的case语句

case $变量名称 in 模式1) 命令序列1 ;; 模式2) 命令序列2 ;; *) 默认命令序列 esac

1.提示用户输入一个字符,判断该字符是字母、数字或者其他字符的脚本
 [root@localhost test]# vim hitkey.sh
 #!/bin/bash
 #击键类型识别
 read -p "请输⼊⼀个字符,并按Enter键确认:" key
 case $key in
  [a-z]|[A-Z]) # a到z或A到Z,当变量输入为字母则执行下面的echo命令
  echo "您输⼊的是⼀个 字⺟"
  ;; 
  [0-9])  # 0到9,当变量输入为数字则执行下面的echo的命令
  echo "您输⼊的是⼀个 数字"
  ;;
  *) # 若变量输入为空格等其他符号字符,则执行下面的echo命令
  echo "您输⼊的是 空格、功能键或其他控制字符"
  ;;
 esac
 [root@localhost test]# sh ./hitkey.sh
 请输⼊⼀个字符,并按Enter键确认:5
 您输⼊的是⼀个 数字
 [root@localhost test]# sh ./hitkey.sh
 请输⼊⼀个字符,并按Enter键确认:b
 您输⼊的是⼀个 字⺟
 [root@localhost test]# sh ./hitkey.sh
 请输⼊⼀个字符,并按Enter键确认:P
 您输⼊的是⼀个 字⺟
 [root@localhost test]# sh ./hitkey.sh
 请输⼊⼀个字符,并按Enter键确认:!
 您输⼊的是 空格、功能键或其他控制字符
​

2.输入分数变量,然后判定等级脚本
 [root@localhost test]# vim fscase.sh
 #!/bin/bash
 #使⽤case语句编写分数等级评定脚本
 read -p "请输⼊您的分数(0-100):" fs
 case $fs in
  [0-9]|[0-5][0-9]) # 0到9或59以内的两位数
  echo "$fs分,不及格!"
  ;;
  6[0-9]) # 6开头的两位数,若$fs输入为0,则判定为60,即执行下面的echo命令
  echo "$fs分,及格!"
  ;;
  7[0-9]|8[0-5]) # 以7开头的两位数或以8开头的两位数
  echo "$fs分,良好!"
  ;;
  8[6-9]|9[0-9]|100) # 以8开头的两位数,第二位最少为6,也就是最小是86 | 以9开头的两位数 | 100
  echo "$fs分,优秀!"
  ;;
  *) # 输入不在上述规则内的其他字符,则echo如下命令
  echo "您输⼊的分数有误!"
 esac
 
 [root@localhost test]# sh ./fscase.sh
 请输⼊您的分数(0-100):5
 5分,不及格!
 [root@localhost test]# sh ./fscase.sh
 请输⼊您的分数(0-100):58
 58分,不及格!
 [root@localhost test]# sh ./fscase.sh
 请输⼊您的分数(0-100):69
 69分,及格!
 [root@localhost test]# sh ./fscase.sh
 请输⼊您的分数(0-100):70
 70分,良好!
 [root@localhost test]# sh ./fscase.sh
 请输⼊您的分数(0-100):89
 89分,优秀!
 [root@localhost test]# sh ./fscase.sh
 请输⼊您的分数(0-100):100
 100分,优秀!
 [root@localhost test]# sh ./fscase.sh
 请输⼊您的分数(0-100):110
 您输⼊的分数有误!
​

8.循环语法

1.for循环

for 变量名 in 取值列表(范围) do 命令序列 done

2.分隔值循环
[root@shell ~]# vim citygood.sh
​
!/bin/bash
​
for city in 青岛 庆阳
do
   echo "$city是个好地方"
done
[root@shell ~]# bash citygood.sh 
青岛是个好地方
庆阳是个好地方

1.判断包是否已安装
 [root@localhost test]# vim 2.sh
 #!/bin/bash
 for softpack in wget gcc pcre pcre-devel zlib zlib-devel
 do
  soft_result=$(rpm -qa $softpack)
  if [ -z "$soft_result" ];then
  yum install -y $softpack
  else
  echo "$softpack is installed"
  fi
 done
 [root@localhost test]# bash 2.sh
 wget is installed
 gcc is installed
 pcre is installed
 pcre-devel is installed
 zlib is installed
 zlib-devel is installed
​

2.在命令结果中循环
[root@shell ~]# vim shower.sh
#!/bin/bash
for u in $(awk -F ':' "{print $1}"  /etc/passwd)
do
 echo $u
done
[root@shell ~]# bash shower.sh 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
​
[root@shell ~]# awk -F ":" '{print $1}' /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none

3.检测某个网段的存活主机
 [root@localhost test]# vim ping.sh
 #!/bin/bash
 for IP in $(echo 192.168.33.{100..120}) # 192.168.33网段的100到120的主机,在此循环
 do
  ping -c 2 -i 0.1 $IP &> /dev/null
  if [ $? -eq 0 ];then
  echo "Host $IP is up."
  fi
 done
 :wq
 [root@localhost test]# bash ping.sh
 Host 192.168.100.100 is up.
 Host 192.168.100.101 is up.
​

9.while循环

while 条件测试操作 do 命令序列 done

1.添加批量用户

创建时交互输入用户前缀、创建用户个数、初始密码、过期时间(可选设置),用户首次登陆强制要求修改密码

 [root@localhost test]# vim useradd.sh # 批量创建⽤户脚本
 #!/bin/bash
 read -p "请输⼊创建⽤户的名称前缀:" QZ
 read -p "请输⼊创建⽤户的个数:" NUM
 read -p "请输⼊⽤户的初始密码:" PS
 i=1
 while [ $i -le $NUM ]
 do
  useradd $QZ$i
  echo "$PS" | passwd --stdin $QZ$i &> /dev/null
  chage -d 0 $QZ$i
  let i++
 done
 :wq
 [root@localhost test]# bash useradd.sh
 请输⼊创建⽤户的名称前缀:admin
 请输⼊创建⽤户的个数:5
 请输⼊⽤户的初始密码:123456
 [root@localhost test]# tail /etc/passwd
 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
 tcpdump:x:72:72::/:/sbin/nologin
 yunjisuan:x:1000:1000:yunjisuan:/home/yunjisuan:/bin/bash
 apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
 nginx:x:975:974:Nginx web server:/var/lib/nginx:/sbin/nologin
 admin1:x:1001:1001::/home/admin1:/bin/bash
 admin2:x:1002:1002::/home/admin2:/bin/bash
 admin3:x:1003:1003::/home/admin3:/bin/bash
 admin4:x:1004:1004::/home/admin4:/bin/bash
 admin5:x:1005:1005::/home/admin5:/bin/bash
​

2. 批量删除用户
 批量删除用户
 [root@localhost test]# vim userdel.sh # 批量删除⽤户脚本
 #!/bin/bash
 read -p "请输⼊要删除⽤户的前缀:" QZ
 read -p "请输⼊要删除⽤户的个数:" NUM
 i=1
 while [ $i -le $NUM ]
 do
  userdel -r $QZ$i
  let i++
 done 
 
 [root@localhost test]# bash userdel.sh
 请输⼊要删除⽤户的前缀:admin
 请输⼊要删除⽤户的个数:5
​

3.循环的 break 和 continue
 [root@localhost test]# vim test.sh
 #!/bin/bash
 for line in 北京 上海 ⼴州 深圳
 do 
  echo $line
  if [ "$line" == "上海" ];then # 循环到上海⽴即退出
  break
  fi 
 done
 :wq
 [root@localhost test]# bash test.sh
 北京
 上海
 [root@localhost test]# vim test.sh
 #!/bin/bash
 for line in 北京 上海 ⼴州 深圳
 do
 if [ "$line" == "上海" ];then
  continue
 fi
  echo $line
 done
 :wq
 [root@localhost test]# bash test.sh
 北京
 ⼴州
 深圳
​

4.九九乘法表
 [root@localhost test]# vim 99.sh
 #!/bin/bash
 #九九乘法表
 for i in {1..9};do
  for j in {1..9};do
  echo -n "$j*$i=$(($i*$j)) "
  if [ $j == $i ];then
  echo -e '\n'
  break
  fi
  done
 done
 :wq
 [root@localhost test]# bash 99.sh
 1*1=1
 1*2=2 2*2=4
 1*3=3 2*3=6 3*3=9
 1*4=4 2*4=8 3*4=12 4*4=16
 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
​
[root@localhost test]# vim 99-2.sh
 #!/bin/bash
 #九九乘法表
 i=1
 while [ $i -le 9 ];do
  j=1
  while [ $j -le 9 ];do
  echo -n "$j*$i=$(($i*$j)) "
  if [ $j -eq $i ];then
  echo -e '\n'
  break
  fi
  let j++
  done
  let i++
 done
​
 [root@localhost test]# bash 99-2.sh
 1*1=1
 1*2=2 2*2=4
 1*3=3 2*3=6 3*3=9
 1*4=4 2*4=8 3*4=12 4*4=16
 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
​

10.sed流式编辑器

sed 选项 “(定位符)指令” 文件名

(定位符)指令---想对文件的哪一行进行操作

-e指定要执行的命令 (操作) ,只有一个编辑命令 (操作) 时可省略
-n屏蔽默认输出 //不加选项-n默认先全文打印再执行命令打印所要求内容
-i直接修改源文件,不输出结果
-r支持扩展正则
1.行号定位
 [root@localhost day04]# sed "2p" /etc/hosts
 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
 加上-n不全文打印
 [root@localhost day04]# sed -n "2p" /etc/hosts
 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
 打印第三行
 [root@localhost day04]# sed -n "3p" /etc/passwd
 daemon:x:2:2:daemon:/sbin:/sbin/nologin
 打印一到三行
 [root@localhost day04]# sed -n "1,3p" /etc/passwd
 root:x:0:0:root:/root:/bin/bash
 bin:x:1:1:bin:/bin:/sbin/nologin
 daemon:x:2:2:daemon:/sbin:/sbin/nologin
 打印第一行和第三行
 [root@lib ~]# sed -n '1p;3p' ifcfg-ens33 
 TYPE=Ethernet
 BROWSER_ONLY=no
 打印奇数行 (行数从1开始每次自加2)
 [root@localhost day04]# sed -n "1~2p" /etc/passwd
 打印偶数行 (行数从2开始每次自加2)
 [root@localhost day04]# sed -n "2~2p" /etc/passwd
 打印第二行以及后面相邻的三行 (行数,+数字)---表示行数以及后面相邻的数字行
 [root@localhost day04]# sed -n "2,+3p" /etc/passwd
 bin:x:1:1:bin:/bin:/sbin/nologin
 daemon:x:2:2:daemon:/sbin:/sbin/nologin
 adm:x:3:4:adm:/var/adm:/sbin/nologin
 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
​

2.正则定位

sed可以使用正则匹配需要数据然后编辑数据 过滤出现root开头的行 [root@localhost day04]# sed -n "/^root/p" /etc/passwd 过滤三位数 [root@localhost day04]# sed -rn "/[0-9]{3}/p" /etc/passwd

1.sed修改配置
p(print)打印(输出)
d(delete)删除(整行)
s(substitution)替换关键字(字符串匹配)
c(replace)替换行(整行)
r(read)读取指定文件(追加到行后)| 导入文件(追加到行后)
a(append)追加到指定内容到行后
i (insert)追加指定内容到行前
w(write)写入文件 | 导出文件
=打印行号
[root@lib ~]# sed -i '4d' ifcfg-ens33     //删除第四行
 [root@lib ~]# sed -i '3aBOOTPROTO="dhcp"' ifcfg-ens33     //将指定内容追加到第三行后面    
 [root@lib ~]# sed -i '/dhcp/ s/dhcp/none/g' ifcfg-ens33    //定位到dhcp的一行,并将dhcp换为none

2.sed命令引用变量

1、sed命令使用单引号的情况下,可以使用 '"$var"' 引用(单引号,然后 双引号,变量):

sed -i '2s/node_base/'"​i.xml2、sed命令中使用双引号的情况下,直接 shell command 或者 $(shell command) 引用命令执行。

sed -i "2s/node_base/​i.xml

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值