Linux下的shell的应用示例

1.用户建立脚本

1.执行users_create.sh userfile passfile,建立userlist列表中的用户,设定userlist列表中的密码为passlist列表中的密码,当脚本后面跟的文件个数不足两个时报错,当文件行署不一致时报错,当文件不存在时报错,当用户存在时报错。

[root@westos mnt]# vim users_create.sh 

【12】【13】

[root@westos mnt]# vim userfile 
[root@westos mnt]# vim passfile 
[root@westos mnt]# cat userfile passfile
[root@westos mnt]# sh users_create.sh userfile passfile 
[root@westos mnt]# userdel user1
[root@westos mnt]# sh users_create.sh userfile passfile 

【14】

[root@westos mnt]# vim passfile 
[root@westos mnt]# cat userfile passfile 
[root@westos mnt]# sh users_create.sh userfile passfile

【15】

2.数据库备份

执行mysql_dump.sh westos(数据库密码),脚本执行后会备份数据库中的所有库到/mnt/mysqldump目录中,备份文件名称为"库名称.sql",当此文件存在时报错并询问动作,输入"S“跳过备份,输入”B“时备份”库名称.sql“文件为”库名称_backup.sql",当输入"O"时,覆盖原文件。

[root@westos mnt]# vim mysql_dump.sh

【16】【17】

[root@westos mnt]# sh mysql_dump.sh 
[root@westos mnt]# sh mysql_dump.sh westos
[root@westos mnt]# ls
[root@westos mnt]# cd mysqldump/
[root@westos mysqldump]# ls
[root@westos mysqldump]# cd /mnt
[root@westos mnt]# sh mysql_dump.sh 
[root@westos mnt]# sh mysql_dump.sh westos
Please input action: B
[root@westos mnt]# ls
[root@westos mnt]# cd mysqldump/
[root@westos mysqldump]# ls

【18】

3.脚本中的语句控制器

exit n		/脚本退出,退出值为n
break		/退出当前循环
continue 	/提前结束循环内部的命令,但不终止循环

4.运算方式及运算符号

+,-			   		 /加法、减法
*,/,%				 /乘法、除法、取余
**			    	 /幂运算
++,--				 /自增加、自减少
<,<=,>,>=			 /比较符号
=,+=,-=,*=,/=,%=	 /赋值运算

5.shell中常用的运算命令

运算操作于运算命令				含义
      (())               用于整数运算
      let		    用于整数运算,与(())类似
      expr		    用于整数运算,功能相对较多
       bc       linux下的计算器,适合整数及小数运算
      $[]			    用户整数运算

6.编写脚本

请用运算和已经学过的shell语句写一个10秒的倒计时的脚本

[root@westos mnt]# vim cunt_down.sh 
[root@westos mnt]# sh cunt_down.sh 

【19】

请用运算和已经学过的shell语句写一个1分10秒倒计时的脚本

[root@westos mnt]# vim cunts_down.sh 
[root@westos mnt]# sh cunts_down.sh 

【20】

7.编写脚本

执行Calculator.sh后显示:
请输入您要操作的数字:
请输入要操作的运算:
请输入要操作的第二个数字:
执行后显示操作后的数值

[root@westos mnt]# vim Calculator.sh 
[root@westos mnt]# sh Calculator.sh 
Please input first number: 5
Please input operation: +
Please input second number: 7

【21】

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值