sheel脚本

本文详细介绍了Shell脚本的基础概念,包括交互式和批处理模式,如何编写简单脚本,参数的使用与判断,以及流程控制如if、for、while、case的实战应用。通过实例演示了如何创建用户账户、猜数字游戏和字符串检查等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

1. Linux 脚本编写基础 1.1 语法基本介绍 1.1.1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh   符号#!用来告诉系统它后面的参数是用来执行该文件的程序。在这个例子中我们使用/bin/sh来执行程序。   当编辑好脚本时,如果要执行该脚本,还必须使其可执行。   要使脚本可执行: 编译 chmod +x filename 这样才能用./filename 来运行 1.1.2 注释   在进行shell编程时,以#开头的句子表示注释,直到这一行的结束。我们真诚地建议您在程序中使用注释。   如果您使用了注释,那么即使相当长的时间内没有使用该脚本,您也能在很短的时间内明白该脚本的作用及工作原理。 1.1.3 变量   在其他编程语言中您必须使用变量。在shell编程中,所有的变量都由字符串组成,并且您不需要对变量进行声明。要赋值给一个变量,您可以这样写: #!/bin/sh #对变量赋值: a="hello world" # 现在打印变量a的内容: echo "A is:" echo $a 有时候变量名很容易与其他文字混淆,比如: num=2 echo "this is the $numnd" 这并不会打印出"this is the 2nd",而仅仅打印"this is the ",因为shell会去搜索变量numnd的值,但是这个变量时没有值的。可以使用花括号来告诉shell我们要打印的是num变量: 网管u家u.bitsCN.com num=2 echo "this is the ${num}nd"   这将打印: this is the 2nd 1.1.4 环境变量 由export关键字处理过的变量叫做环境变量。我们不对环境变量进行讨论,因为通常情况下仅仅在登录脚本中使用环境变量。 1.1.5 Shell命令和流程控制 在shell脚本中可以使用三类命令:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

one 大白(●—●)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值