Linux九阴真经之无影剑残卷9(Shell脚本编程进阶)

Shell脚本编程进阶

条件选择if语句
选择执行:
注意:if语句可嵌套
单分支


if 判断条件;then
条件为真的分支代码
fi
双分支
if 判断条件; then
条件为真的分支代码
else


条件为假的分支代码
fi

   多分支
if 判断条件1; then
条件为真的分支代码


elif 判断条件2; then
条件为真的分支代码


elif 判断条件3; then
条件为真的分支代码


else
以上条件都为假的分支代码
fi
逐条件进行判断,第一次遇为“真”条件时,执行其分支,而后结束整个if语句

 

例:请输入一个年龄数字,系统会提示相应的提示语

[root@laobai ~#vim bitch.sh

测试一下结果

[root@laobai ~#bash bitch.sh 
Please input your age:22
good good work
[root@laobai ~#bash bitch.sh 
Please input your age:33
good good work
[root@laobai ~#bash bitch.sh 
Please input your age:66
free your life
[root@laobai ~#bash bitch.sh 
Please input your age:666
are you people?

 

If示例


根据命令的退出状态来执行命令
if ping -c1 -W2 station1 &> /dev/null; then


echo 'Station1 is UP' elif grep "station1" ~/maintenance.txt &> /dev/null
then


echo 'Station1 is undergoing maintenance‘


else echo 'Station1 is unexpectedly DOWN!' exit 1
fi

条件判断:case语句

case 变量引用 in
PAT1)
分支1
;;
PAT2)
分支2
;;
...
*)
默认分支
;;
esac
case支持glob风格的通配符:
*: 任意长度任意字符
?: 任意单个字符
[ ]:指定范围内的任意单个字符
a|b: a或b

例:编写脚本/root/bin/yesorno.sh,提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息

[root@laobai ~#vim esac.sh 

我们来测试一下结果,成功!

[root@laobai ~#bash esac.sh 
are you agree?yes or no: 
Please input you answer
[root@laobai ~#bash esac.sh 
are you agree?yes or no: y
your answer is yes

 

循环

分三种:  for, while, until

 

for 循环

for + 变量名(前面不加$) in  + 列表;do

           循环体

         done 结束

执行机制:
依次将列表中的元素赋值给“变量名”; 每次赋值后即执行一次循环体; 直到列表中的元素耗尽,循环结束

例:创建numiber1-numiber10的文件

[root@laobai ~#for name in {1..10} ;do echo the numiber is $name ;done
the numiber is 1
the numiber is 2
the numiber is 3
the numiber is 4
the numiber is 5
the numiber is 6
the numiber is 7
the numiber is 8
the numiber is 9
the numiber is 10

或者       也可以用seq   1..3..10    在命令里则需要 加上 ` ` 

[root@laobai ~#for name in {1..10..4} ;do echo the numiber is $name ;done
the numiber is 1
the numiber is 5
the numiber is 9

也可以匹配命令

[root@laobai ~#for num in `ls /boot`;do echo the file is $num ; done 
the file is config-2.6.32-696.el6.x86_64
the file is efi
the file is grub
the file is initramfs-2.6.32-696.el6.x86_64.img
the file is lost+found
the file is symvers-2.6.32-696.el6.x86_64.gz
the file is System.map-2.6.32-696.el6.x86_64
the file is vmlinuz-2.6.32-696.el6.x86_64


例:用 for 循环 计算 1到100 之和

[root@centos7 ~]#vim sum.sh

给与执行权限,进行测试

[root@centos7 ~]#chmod +x sum.sh
[root@centos7 ~]#bash sum.sh
jia=5050

成功。

例:判断系统内是否有user21-user25的账号,如果没有则创建,密码为magedu

[root@centos7 ~]#vim us.sh

 

我们来测试一下

[root@centos7 ~]#bash us.sh
user21 add scussfor
user22 add scussfor
user23 add scussfor
user24 add scussfor
user25 add scussfor

成功!

 例:写一个脚本ping一个IP地址,ping通提示net is up  ping不通提示 net is down

[root@centos7 ~]#vim ping.sh

里面加了{},表示并行执行,能提升效率。我们测试一下

成功

例:ping10个192.168.2/24网段的IP地址,把UP和down的数量记录下来

[root@centos7 ~]#vim ping.sh

执行一下

如果循环里有变量,则会出现一下效果

 可以在 echo 前 加一个“eval” ,此命令先将$n变成10,然后再执行这个循环脚本

例:打印一个随机颜色的等腰三角形,用*号组合

 

例:打印 9x9 乘法表

(将$i 和 $j 换个位)

 

 例:在/testdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字母,如:1AbCdeFgH.html

 

 或者

 

while 循环

while循环用于不知道循环次数的时候使用,维持循环的是一个条件表达式,条件为真的时候,继续执行循环体,当条件为假时,终止循环体。

格式

while  循环控制条件;do

         循环体

 

 done

 

循环控制条件:进入循环之前,先做一次判断;每次循环之后会再次做判断,当条件为true时,继续循环,直到条件为false 时 终止循环。

 

 :利用while循环,定义变量i=1,sum的值为0 ;条件是1-100之间。当 i 的值小于等于100时都执行并循环,并让变量i每执行一次循环+1,直到100时停止循环,得出1到100的数字相加之和

执行一下,成功。

[root@centos7 ~]#bash while.sh
sum=5050

  

 

转载于:https://www.cnblogs.com/huxiaojun/p/9009379.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值