sheel脚本

sheel脚本

sheel脚本

sheel脚本的工作方式有两种:交互式和批处理

  • 交互式:用户输入一条就立即执行
  • 批处理:由用户实现编写好一个完整的sheel脚本,sheel会一次执行脚本中诸多命令

脚本中不仅会用到一般的linux命令、管道符、重定向,还需要把内部功能模块后通过逻辑语句进行处理,最终形成日常使用的脚本。

2 、编写简单的脚本

实际上,使用vim将命令写入到一个文件中,就是一个简单的脚本了。

#! /bin/bash
# this is a demo

pwd
ls

sheel 脚本的名称可以任意,但是为了区分,一般以.sh后缀,来表名这是一个脚本文件。

一般脚本的第一行脚本声明 #! 用来告诉系统使用哪种解释器来执行该脚本

第二行是对该脚本的注释信息,以便让后来使用脚本的人了解该脚本的功能或警告信息。

之后是脚本的命令

另外也可以通过脚本的路径来执行脚本。

3、脚本参数

实际上,脚本文件可以接受参数,每个参数以空格隔开

1、$0 代表当前脚本的名称

2、$N 代表N个参数,如$1为第一个参数,$2为第二个参数…

3、$*为所有得到参数

4、$? 为上一次命令执行的返回值

#! /bin/bash
# this a example scrip to demostrate the usage of arguments

echo the name of this script is $0
echo tht first of this script is $1
echo this second of this script is $2
echo there are $# arguments totally,the $*

[root@iZwz91wmx1ifron6bg7zccZ jiaobenDemo]# bash example.sh 
the name of this script is example.sh
tht first of this script is
this second of this script is
there are 0 arguments totally,the

4 、脚本参数的判断

系统可以对用户输出的参数进行判断和测试,按照测试对象划分,测试语句可以分为四种:

4.1 文件测试语句

用来判断文件是否存在或权限是否满足等情况的运算符,具体用法如下

运算符作用
-d测试文件是否目录
-e测试文件是否存在
-f判断是否为一般文件
-r测试当前用户是否有权限读取
-w测试当前用户是否有权限写入
-x测试当前用户是否有权限执行

测试 /etc/fstab 是否为一个目录类型的文件,然后通过sheel解释器内设的$? 来显示上一条命令的执行结果。如果返回值为0,则存在,非0值,则不存在。

[root@iZwz91wmx1ifron6bg7zccZ jiaobenDemo]# [ -d /etc/fstab ]
[root@iZwz91wmx1ifron6bg7zccZ jiaobenDemo]# echo $?
1

[ 与 ] 本身也是命令,所以要空格隔开

4.2 逻辑测试语句

逻辑语句对于测试结果进行逻辑分析,可根据测试结果可以实现不同的效果。

4.2.1 并 &&

逻辑并标识两者都为真则为真,所以第一条为假则为假,第一条为真时要判断第二句,也就是说, && 前面的语句执行成功才会执行后面的命令。

[root@iZwz91wmx1ifron6bg7zccZ jiaobenDemo]# [ -d /root/jiaobenDemo ] && echo "this file is a directory"
this file is a directory

4.22 或 ||

逻辑或表示有一个为真则为真,所以第一条为真则真,第一条为假时要判断第二句,也就是说,||前面的语句执行为假才会执行后面的命令。

[root@iZwz91wmx1ifron6bg7zccZ jiaobenDemo]# [ -d /root/jiaobenDemo/example.sh ] || echo "this file is not a directory"
this file is not a directory

4.2.3 非 !

它表示把判断结果取相反值。

[root@alliyun jiaobenDemo]# [ ! $USER=root ] && echo "this is not admininstrator"
[root@alliyun jiaobenDemo]# 

4.3 整数值比较语句

由于大于号小于号与定向符号冲突了,所以需要使用整数比较运算符:

运算符含义
-eq(equal)等于
-ne(not equal)不等于
-gt(greater then)大于
-lt(less than)小于
-ge(greater than or equal)大于等于
-le(less than or equal)小于等于
[root@alliyun jiaobenDemo]#  [ 1 -lt 2 ] && echo "right"
right
[root@alliyun jiaobenDemo]# [ 1 -lt 2 ] && [ 1 -eq 1 ]
[root@alliyun jiaobenDemo]# echo $?
0

4.4 字符串比较语句

字符串的比较语句用于判断测试字符串是否为空值,或者两个字符串是否相同,它经常用来判断某个变量是否被定义(即内容为空值).

运算符含义
=字符串内容是否相同
!=是否不同
-z判断字符串是否为空,空为真
[root@alliyun jiaobenDemo]# [ ! -z $SHELL ] && echo "this variable exist"
this variable exist

5 脚本的流程控制

尽量上述的测试语句可以完成基本的流程控制,但这并不够灵活,在生产环境中我们需要通过 if、for 、while 、case 四种流程控制语句编写更灵活复杂的脚本。这四个语句逻辑上和其他语言的用法并没有区别,只是语法的区别。

if

测试是否能与主机通信

#!/bin/bash

# determine whether host is online

ping -c 3 $1 &> /dev/null
if [ $? -eq 0 ]
then 
echo "host $1 is online"
else 
echo "host $1 is offline"
fi


[root@alliyun jiaobenDemo]# bash chrhost.sh 
host  is offline
[root@alliyun jiaobenDemo]# bash chrhost.sh 127.0.0.1
host 127.0.0.1 is online

多分支例子,输入分数后输出评价。

#!/bin/bash

# input an interger score,output an evalution

read -p "enter your score(0-100):" GRADE
if [ $GRADE -gt 100 ] || [ $GRADE -lt 0 ]
then echo "score is invalid"
elif [ $GRADE -ge 85 ] && [ $GRADE -le 100 ]
then echo "excellent"
elif [ $GRADE -ge 60 ] && [ $GRADE -lt 85 ]
then echo "pass"
else
echo "fail"
fi

[root@alliyun jiaobenDemo]# bash chrscore.sh 100
enter your score(0-100):100
excellent
[root@alliyun jiaobenDemo]# bash chrscore.sh
enter your score(0-100):120
score is invalid
[root@alliyun jiaobenDemo]# bash chrscore.sh
enter your score(0-100):-1
score is invalid
[root@alliyun jiaobenDemo]# bash chrscore.sh
enter your score(0-100):40
fail

for

从列表文件中读取多个用户名,然后逐一创建用户账户并设置密码
从列表文件中读取多个用户名,然后逐一创建用户账户并设置密码

[root@alliyun jiaobenDemo]# vim users.txt 
andy
barry
jack
simmen
tracy
[root@alliyun jiaobenDemo]# vim createacount.sh 
#!/bin/bash
# read name then create an acount

read -p "enter the users password:" PASSWD
for UNAME in `cat users.txt`
do 
  id $UNAME &> /dev/null
  if [ $? -eq 0 ]
  then 
  echo "already exists"
  else 
  useradd $UNAME &> /dev/null
  echo $PASSWD | passwd --stdin $UNAME &> /dev/null
  if [ $? -eq 0 ]
  then 
	echo "$UNAME,create success"
  else
	echo "$UNAME,create failure"
  fi
fi
done

# 运行脚本
[root@alliyun jiaobenDemo]# bash createacount.sh 
enter the users password:101650
andy,create success
barry,create success
jack,create success
simmen,create success
tracy,create success

while

编写一个猜数字大小的脚本

[root@alliyun jiaobenDemo]# vim  guess.sh
#! /bin/bash
# guess which the integer is
INTEGER=$(expr $RANDOM % 1000)
TIMES=0
echo "there is a integer between 0 and 999,guess who it is"
echo $INTEGER
while true
do 
read -p "please guess:" INT
let TIMES++
if [ $INT -eq  $INTEGER ]
then
echo "congratulations,your answar is right,the true integer is $INTEGER"
echo "you guessed $TIMES times"
exit 0
elif [ $INT -gt $INTEGER ]
then 
echo "too big"
else
echo "too small"
fi
done

[root@alliyun jiaobenDemo]# bash guess.sh 
there is a integer between 0 and 999,guess who it is
619
please guess:500
too small
please guess:600
too small
please guess:700
too big
please guess:800
too big
please guess:120
too small
please guess:80
too small
please guess:66
too small
please guess:619
congratulations,your answar is right,the true integer is 619
you guessed 9 times
[root@alliyun jiaobenDemo]# 

case

#! /bin/bash

read -p "please anter a string:" KEY
case "$KEY" in
[a-z] | [A-Z])
echo "what your input is alphabeta"
;;
[0-9])
echo "what you input is number"
;;
*)
echo "what you input is space or other control string"
esac

[root@alliyun jiaobenDemo]# bash checkkey.sh 
please anter a string:1
what you input is number
[root@alliyun jiaobenDemo]# bash checkkey.sh 
please anter a string:t
what your input is alphabeta
[root@alliyun jiaobenDemo]# bash checkkey.sh 
please anter a string:
what you input is space or other control string

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值