1 shell 脚本文件

shell 解释器可当作人与计算机硬件的“翻译官”, 他作为用户与linux系统内部的通信媒介,除了能够支持各种变量与参数外,还提供了像判断,循环等高级编程语言有的控制流程的特性。 想要正确高效的做好系统运维工作,脚本的使用至关重要。

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

  • 交互式(interactive) : 用户输入一条就立即执行
  • 批处理(batch): 由用户事先编写好一个完整的shell脚本,shell会一次性执行脚本中诸多的命令。

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

2 编写简单的脚本

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

[root@linuxlearner Desktop]# vim demo.txt 
#!/bin/bash
# this is a demo 
pwd
ls 
[root@linuxlearner Desktop]# bash demo.txt 
/home/redhat/Desktop
demo.txt

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

一般脚本的第一行脚声明 # !用来告诉系统使用哪种shell解释器来执行该脚本
第二行是对该脚本的注释信息,以便让后来使用脚本的人了解该脚本的功能或警告信息
之后是脚本的命令

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

3 脚本参数

上述的脚本只能执行一些预先定义好的命令,让shell脚本程序更好地满足用户的实时需求,以便灵活完成工作,必须要让脚本像交互式执行命令一样,能够接收用户的参数。
实际上,脚本文件就可以接受参数,每个参数以空格隔开。

  1. $0代表当前脚本程序的名称
  2. $N 代表第N个参数, 如$1为第一个参数,$2为第二个参数。。。
  3. $* 为所有的参数
  4. $?为上一次命令执行的返回值

比如下例:

[root@linuxlearner Desktop] vim example.sh
#!/bin/bash 
# this a example scrip to demonstrate the usage of arguments 
echo the name of this script is $0 
echo the first argument of this script is $1
echo the second argument of this script is $2
echo there are $# arguments totally, they $* 
                                              
[root@linuxlearner Desktop]# bash example.sh hello world
the name of this script is example.sh
the first argument of this script is hello
the second argument of this script is world
there are 2 arguments totally, they hello world

4 脚本参数的判断

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

4.1 文件测试语句

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

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

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

[root@linuxlearner Desktop]# [ -d /etc/fstab ] 
[root@linuxlearner Desktop]# echo $?
1

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

4.2 逻辑测试语句

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

4.2.1 并 &&

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

[root@linuxlearner Desktop]# [ -d /home/redhat/Desktop ] && echo "this file is a directory" 
this file is a directory
4.2.2 或 ||

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

[root@linuxlearner Desktop]# [ -d /home/redhat/Desktop/example.sh ] || echo "this file is not a directory" 
this file is not a directory
4.2.3 非 !

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

[redhat@linuxlearner /]$ [ ! $USER = root ] && echo "this is not administrator"
this is not administrator

4.3 整数值比较语句

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

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

4.4 字符串比较语句

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

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

5 脚本的流程控制

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

if

测试是否能与主机通信

[root@linuxlearner Desktop]# vim chkhost.sh 
#!/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@linuxlearner Desktop]# bash chkhost.sh 
host  is offline
[root@linuxlearner Desktop]# bash chkhost.sh 192.168.10.10 
host 192.168.10.10 is online
[root@linuxlearner Desktop]# bash chkhost.sh 192.168.10.20
host 192.168.10.20 is offline

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

[root@linuxlearner Desktop]#vim chksceore.sh
#! /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@linuxlearner Desktop]# bash chksceore.sh 100
enter your score(0-100):100
excellent
[root@linuxlearner Desktop]# bash chksceore.sh 
enter your score(0-100):120 
score is invalid
[root@linuxlearner Desktop]# bash chksceore.sh 
enter your score(0-100): -1
score is invalid
[root@linuxlearner Desktop]# bash chksceore.sh 
enter your score(0-100):40 
fail
for

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

#新建带创建用户名单
[root@linuxlearner Desktop]# vim users.txt 
andy
barry 
jack 
simmen 
tracy 
# 新建脚本
#!/bin/bash 
# read name then cerate 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, cerate failure" 
  fi
 fi
done

# 运行脚本
[root@linuxlearner Desktop]# 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@linuxlearner Desktop]# 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@linuxlearner Desktop]# bash guess.sh 
there is a integer between 0 and 999, guess who it is
please guess:500
too big
please guess:250
too small
please guess:375
too big
please guess:320
too big
please guess:300
too big
please guess:265
congratulations, your answar is right, the true integer is 265
you guessed 6 times
case
[root@linuxlearner Desktop]# vim checkkey.sh
#!/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@linuxlearner Desktop]# bash checkkey.sh 
please anter a string:1
what you input is number
[root@linuxlearner Desktop]# bash checkkey.sh 
please anter a string:t
what your input is alpha
[root@linuxlearner Desktop]# bash checkkey.sh 
please anter a string: 
what you input is spass or other control string             
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐