Shell的基本语句与使用

1、if条件语句
单分支结构语法:
if [条件];then
指令
fi

read -p"plase a num:" m n

if [ $m -le $n ];then
    echo "$m <= $n"
fi      
[root@master scripts]# sh if_01.sh 
plase a num:3 4 
3 <= 4
[root@master scripts]# sh if_01.sh 
plase a num:2 3 
2 <= 3
[root@master scripts]# sh if_01.sh 
plase a num:3 2 
[root@master scripts]#     

双分支结构语法:
if [条件];then
指令
else
指令
fi

#!/bin/bash
## this is httpd scrpits ##
## data  2019-01-27 ## 
curl 192.168.126.128 >&/dev/null
if [ $? -eq 0 ];then
   echo "httpd is running."
else
   systemctl start httpd
fi

[root@master scripts]# bash check_02web.sh            
[root@master scripts]# bash check_02web.sh     //第一次web程序并未启动
httpd is running.                           //再次运行脚本web程序已然处于运行状态
[root@master scripts]# 

多分支结构语句:
if [条件];then
指令
elif [条件];then
指令
else
指令
fi

#!/bin/bash
read -p"请输入两个数字:" m n
if [ $m -gt $n ];then
     echo "$m >= $n"
   elif [ $m -eq $n ];then
     echo "$m = $n"
   else
     echo "$m <= $n"
fi

[root@master scripts]# bash panduan02.sh
请输入两个数字:3 4
3 <= 4
[root@master scripts]# bash panduan02.sh
请输入两个数字:3 2 
3 >= 2
[root@master scripts]# bash panduan02.sh
请输入两个数字:3 3 
3 = 3
[root@master scripts]# 

2、case结构条件句语法:
case “字符串变量” in
值1)指令…
;;
值2)指令…
;;
*)指令…
;;
esac

#!/bin/bash
read -p"请输入一个数字:" ans
case "$ans" in
   1) echo "the num you input is 1"
;;

   2) echo "the num you input is 2"
;;

   [3-9]) echo "the num you input is $ans"
;;
   *) echo "the num you input must be less 9."
      exit;
;;
esac

 [root@master scripts]# bash case-1.sh
请输入一个数字:2
the num you input is 2
[root@master scripts]# bash case-1.sh
请输入一个数字:7
the num you input is 7
[root@master scripts]# bash case-1.sh
请输入一个数字:10
the num you input must be less 9.
[root@master scripts]#      

3、while循环条件句语法:
while 条件
do
指令…
done

##条件为真时,则执行循环。
脚本1:

#!/bin/bash
i="$1"
while [ $i -gt 0 ]
do
    echo "$i"
    ((i--))
done

[root@master scripts]# bash while_04.sh 10
10
9
8
7
6
5
4
3
2
1
[root@master scripts]# 

脚本2:

#!/bin/bash
i=1
sum=0
while ((i<=100))
do
    ((sum=sum+i))
    ((i++))
done
    echo "sum=$sum"

[root@master scripts]# bash while_02.sh
sum=5050
[root@master scripts]# 

4、until条件句语法:
until 条件
do
指令…
done

##条件为假时,则执行循环

#!/bin/bash
i="$1"
until [ $i -gt 50 ]
do
      echo "$i"
      ((i++))
done


[root@master scripts]# bash until_01.sh 40
40
41
42
43
44
45
46
47
48
49
50
[root@master scripts]# 

5、for循环结构语法:
语法1:
for 变量 in 变量取值列表
do
指令…
done

语法2:
for ((exp1;exp2;exp3))
do
指令…
done

脚本1:

#!/bin/bash
for i in {5..1}
do
    echo "$i"
done

[root@master scripts]# bash for_01.sh
5
4
3
2
1
[root@master scripts]# 

脚本2:

#!/bin/bash
username=nihao0
for i in {1..10}
do
    user=$username$i
    passwd=`echo $(date +%t%N)$RANDOM |md5sum |cut -c 2-9 `
    useradd  -r $user
    echo $passwd |passwd --stdin $user   >&/dev/null
    echo -e "user: $user   passwd:$passwd"  >>/tmp/passwd.txt
    echo "$user is ok"

done


[root@master scripts]# bash user.sh 
nihao01 is ok
nihao02 is ok
nihao03 is ok
nihao04 is ok
nihao05 is ok
nihao06 is ok
nihao07 is ok
nihao08 is ok
nihao09 is ok
nihao010 is ok
[root@master scripts]# id nihao01
uid=992(nihao01) gid=980(nihao01) groups=980(nihao01)
[root@master scripts]# 
[root@master scripts]# tail -10 /tmp/passwd.txt 
user: nihao01   passwd:32e4bdf3
user: nihao02   passwd:a37d9f8f
user: nihao03   passwd:b9028091
user: nihao04   passwd:c6f4609c
user: nihao05   passwd:0f37221b
user: nihao06   passwd:5d3c979f
user: nihao07   passwd:679d0eee
user: nihao08   passwd:eb40513b
user: nihao09   passwd:ddb175c5
user: nihao010   passwd:1d3b2ca2
[root@master scripts]# 

数据库查看主从状态简易脚本:

#!/bin/bash
dbstatus1=`mysql -uroot -e"show slave status\G;" | grep -i Running | head -1 |cut -d : -f 2`
dbstatus2=`mysql -uroot -e"show slave status\G;" | grep -i Running | tail -1 |cut -d : -f 2`
if [ "$dbstatus1" =  " Yes" ] && [ "$dbstatus2" = " Yes" ];then
   echo "db master_server is running"
   else
   echo "db master_server is stop" 
fi


[root@slave scripts]# bash db_master_slave.sh 
db master_server is stop         //master还并没有启动数据库服务
[root@slave scripts]# 

[root@master scripts]# systemctl start mariadb       //master启动数据库服务
[root@master scripts]#

[root@slave scripts]# bash db_master_slave.sh 
db master_server is running     //主从已然完成
[root@slave scripts]# 

[root@master scripts]# systemctl stop mariadb   //再关闭数据库服务试试
[root@master scripts]# 

[root@slave scripts]# bash db_master_slave.sh 
db master_server is stop            //探测到master停止运行了
[root@slave scripts]# 

输入水果名称显示颜色脚本:

#!/bin/bash
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
VIOLET_COLOR='\E[1;35m'
RES='\E[0m'

read -p"Please input the fruit name you like:" ans
case "$ans" in
apple|APPLE)
    echo -e "the fruit name you like is ${RED_COLOR}"$ans.${RES}
;;
banana|BANANA)
    echo -e "the fruit name you like is ${YELLOW_COLOR}"$ans.${RES}
;;
pear|PEAR)
    echo -e "the fruit name you like is ${YELLOW_COLOR}"$ans.${RES}
;;
grape|GRAPE)
    echo -e "the fruit name you like is ${VIOLET_COLOR}"$ans.${RES}
;;
watermelon|WATERMELON)
    echo -e "the fruit name you like is ${GREEN_COLOR}"$ans.${RES}
;;
strawberry|STRAWBERRY)
    echo -e "the fruit name you like is ${RED_COLOR}"$ans.${RES}
;;
blueberry|BLUEBERRY)
    echo -e "the fruit name you like is ${BLUE_COLOR}"$ans.${RES}
    exit
;;
esac

[root@master scripts]# bash shuiguo.sh 
Please input the fruit name you like:apple
the fruit name you like is apple.
[root@master scripts]# bash shuiguo.sh 
Please input the fruit name you like:banana
the fruit name you like is banana.
[root@master scripts]# bash shuiguo.sh 
Please input the fruit name you like:watermelon
the fruit name you like is watermelon.
[root@master scripts]# bash shuiguo.sh 
Please input the fruit name you like:blueberry
the fruit name you like is blueberry.
[root@master scripts]# 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值