一、变量
一、变量
shell基本语法,shell语法
一、变量
1.变量的命名规则:以字母或下划线初始,后边跟数字,字母或下划线,最棒不用随便命名,要成功看见变量名能猜出其含义
2.变量赋值: x=100
echo $x
删除变量:unset x
3.定义变量名的境界用大括号
[[email protected]
~]# egon_salary=20000
[[email protected]
~]# echo ${egon_salary}yuan
20000yuan
4.bash中不要注解数据类型,默许都是字符型
二、运算符
1.算术运算符:+ – * / %
[[email protected]
~]# echo $[5%2]
1
2.赋值运算符:=,+=,-=,*=,/=,%=
[[email protected]
~]# x=10
[[email protected]
~]# ((x+=1))
[[email protected]
~]# echo $x
11
3.关系运算符:<,>,!=,==,>=,<=,||,&&
涉及运算符常与(( ))连用,[]能够高达同等的结果,但((
))无法判断一个文件的类型,判断文件类型必须求用到[],[]又和test命令效果同样
shell基本语法,shell中test命令用法详解。用$?查看命令执行结果,结果为0代表真,非0代表假
[[email protected]
~]# x=10
[[email protected]
~]# ((x>=8))
[[email protected]
~]# echo $?
0
4.shell里的计算器
事先说过用$[]能够开始展览部分差不离的演算,但是借使波及到小数的运算,就需求用到shell里面包车型客车总括器了
率先要设置软件,yum install -y bc
[[email protected]
~]# res=$(echo ‘scale=2;1/3′ |bc -l |cut -d’.’ -f2)
[[email protected]
~]# echo ${res}%
33%
5.test 发令测试
test
-n str 字符串长度不为零
-z str 字符串长度为零
-b 文件存在且为块文件
-d 文件存在且为目录文件
-e 文件存在
-f 文件存在且为普通文书
-h 文件存在且为链接文件(同 -L)
-s 文件存在且高于零字节
文件之间的可比
file1 -nt file2 file1 的创设时间比file2晚
file1 -ot file2 file1 的创导时间比file2早
整数之间的相比
int1 -ne int2 int1和int2不相等
int1 -eq int2 int1和int2 相等
int1 -lt int2 int1小于int2
int1 -le int2 int1低于等于int2
澳门金沙国际, int1 -gt int2 int1大于int2
int1 -ge int2 int1抢先等于int2
字符串之间比较
str1 = str2 str1和str2相等
str1 !=str2 str1和str2不相等
表明式之间的可比
expression1 -a expression2 表达式1与发挥式2都为真
expression1 -o expression2 表明式1或发布式2为真
6.测试举例
数字相比测试:
[[email protected] ~]# [[ 2 > 1 ]]
[[email protected] ~]# echo $?
0
[[email protected] ~]# ((20>10))
[[email protected] ~]# echo $?
0
[[email protected] ~]# ((20<10))
[[email protected] ~]# echo $?
1
字符串测试
[[email protected] ~]# [ "abc" = "abc" ]
[[email protected] ~]# echo $?
0
[[email protected] ~]# [[ "abc" = "abc" ]]
[[email protected] ~]# echo $?
0
[[email protected] ~]# (("abc" = "abc"))
[[email protected] ~]# echo $?
1
[[email protected] ~]# [[ a = a && 1 < 2 ]]
[[email protected] ~]# echo $?
0
[[email protected] ~]# [[ a = a && 1 < 2 ]]
[[email protected] ~]# echo $?
0
[[email protected] ~]# (( a = a || 1 > 2 ))
[[email protected] ~]# echo $?
1
[[email protected] ~]# [[ a = a || 1 > 2 ]]
[[email protected] ~]# echo $?
0
只有比较数字,用(( ))
除去单纯数字之外的比较,用[[ ]]
叁 、流程序控制制
1.if 分支机构
1)验证用户账号密码:
input your name : zhangcan
input password : 123
login successful
[[email protected] ~]# ./usertest.sh
input your name : hha
input password : hag
user or password error
#! /bin/bash
user='zhangcan'
password='123'
read -p 'input your name : ' name
read -p 'input password : ' code
if [ $name = $user -a $code = $password ];then
echo 'login successful'
else
echo 'user or password error'
fi
~
2)判断成绩档次
#!/bin/bash
#根据用户输入的成绩,判断所属档次,并输出给用户
read -p 'input your score : ' score
if [ $score -ge 90 ];then
echo '优秀'
elif [ $score -ge 70 -a $score -lt 90 ];then
echo '良好'
elif [ $score -ge 60 -a $score -lt 70 ];then
echo '及格'
elif [ $score -lt 60 ];then
echo '较差'
fi
2.while循环
while(条件)
do
命令
done
演示:判断用户输入的公文是何种类型
#!/bin/bash
while :
do
read -p 'input your file : ' file
if [ -z $file ];then
continue
else
break
fi
done
if [ -f $file ];then
echo "$file is regular file"
elif [ -b $file ];then
echo "$file is block file"
elif [ -d $file ];then
echo "$file is directory file"
else
echo "$file type unkonw"
fi
3.for循环
for i in {1..10}
#in前边不肯定是数字,只假设有重返结果的命令都得以
do
echo $i
done
示范1:写八个剧本,测试子网内能够选用的IP
#!/bin/bash
for i in {1..50}
do
ping -c1 192.168.16.$i &> /dev/null # -c1表示ping一次
if [ $? -ne 0 ];then
echo "192.168.16.$i successful"
echo "192.168.16.$i" >> ~/ipavailable.txt
fi
done
~
示范2:总结/dev下各类文件类型的数目
#!/bin/bash
dir='/dev'
for i in $(ls $dir)
do
if [ -h $dir/$i ];then
((link+=1))
elif [ -f $dir/$i ];then
(( rfile+=1))
elif [ -d $dir/$i ];then
((directory+=1))
elif [ -b $dir/$i ];then
(( block+=1 ))
else
(( typeunknow+=1))
fi
done
echo 'block' $block
echo 'regular file' $rfile
echo 'directory' $directory
echo 'link' $link
echo 'unknow' $typeunknow
4.嵌套巡回
以身作则1:输出四个九九乘法表
#!/bin/bash
for ((i=1;i<=9;i++))
do
for ((j=1;j<=i;j++))
do
echo -n "$i*$j=$[$i*$j]"
done
echo
done
示范2:验证用户登陆账号密码,登陆成功后得以执行命令,当输入quit时退出
#!/bin/bash
user='zhangcan'
password='123'
tag=true
while $tag
do
read -p 'input your name : ' name
read -p 'input your password : ' code
if [[ $name = $user ]] && [[ $code = $password ]];then
echo 'login successful'
while $tag
do
read -p '>>: ' cmd
if [[ $cmd = 'quit' ]];then
tag=false
else
$cmd
fi
done
fi
done
一 、变量
1.变量的命名规则:以字母或下划线开始,前边跟数字,字母或下划线,最棒不要随便命名,要达成看见…
test命令用法。作用:检查文件和比较值
1.变量的命名规则:以字母或下划线初叶,前面跟数字,字母或下划线,最棒不用随便命名,要做到看见变量名能猜出其意义
1.变量的命名规则:以字母或下划线伊始,前面跟数字,字母或下划线,最棒不用随便命名,要实现看见变量名能猜出其意思
1)判断表达式
2.变量赋值: x=100
2.变量赋值: x=100
if test (表明式为真)
echo $x
echo $x
if test !表明式为假
删除变量:unset x
删除变量:unset x
test 表达式1 –a 表达式2 四个表明式都为真
3.定义变量名的境界用大括号
3.定义变量名的边际用大括号
test 表达式1 –o 表明式2 五个表明式有2个为真
[root@bogon ~]# egon_salary=20000
[root@bogon ~]# echo ${egon_salary}yuan
20000yuan
[root@bogon ~]# egon_salary=20000
[root@bogon ~]# echo ${egon_salary}yuan
20000yuan
2)判断字符串
4.bash中不用注解数据类型,默许都以字符型
4.bash中不用注脚数据类型,暗许都是字符型
test –n 字符串 字符串的长度非零
二、运算符
二、运算符
test –z 字符串 字符串的尺寸为零
1.算术运算符:+ – * / %
1.算术运算符:+ – * / %
test 字符串1=字符串2 字符串相等
[root@bogon ~]# echo $[5%2]
1
[root@bogon ~]# echo $[5%2]
1
test 字符串1!=字符串2 字符串不等
2.赋值运算符:=,+=,-=,*=,/=,%=
2.赋值运算符:=,+=,-=,*=,/=,%=
3)判断整数
[root@bogon ~]# x=10
[root@bogon ~]# ((x+=1))
[root@bogon ~]# echo $x
11
[root@bogon ~]# x=10
[root@bogon ~]# ((x+=1))
[root@bogon ~]# echo $x
11
test 整数1 –eq 整数2 整数相等
3.事关运算符:<,>,!=,==,>=,<=,||,&&
3.关系运算符:<,>,!=,==,>=,<=,||,&&
test 整数1 –ge 整数2 整数1大于等于整数2
论及运算符常与(( ))连用,[]能够实现平等的结果,但((
))不可能判断三个文件的类型,判断文件类型必供给用到[],[]又和test命令效果同样
关联运算符常与(( ))连用,[]能够达到平等的结果,但((
))不能够断定1个文件的门类,判断文件类型必需求用到[],[]又和test命令效果同样
test 整数1 –gt 整数2 整数1超乎整数2
用$?查看命令执行结果,结果为0代表真,非0代表假
用$?查看命令执行结果,结果为0代表真,非0代表假
test 整数1 –le 整数2 整数1稍低于等于整数2
[root@bogon ~]# x=10
[root@bogon ~]# ((x>=8))
[root@bogon ~]# echo $?
0
[root@bogon ~]# x=10
[root@bogon ~]# ((x>=8))
[root@bogon ~]# echo $?
0
test 整数1 –lt 整数2 整数1低于整数2
4.shell里的总计器
4.shell里的计算器
test 整数1 –ne 整数2 整数1不对等整数2
事先说过用$[]能够开始展览部分大致的演算,可是一旦涉嫌到小数的运算,就供给用到shell里面包车型客车计算器了
从前说过用$[]能够举香港行政局地简便的演算,不过假如波及到小数的运算,就要求用到shell里面包车型地铁总结器了
4)判断文件
先是要设置软件,yum install -y bc
率先要安装软件,yum install -y bc
test File1 –ef File2
四个公文具有相同的装备号和i结点号
[root@bogon ~]# res=$(echo ‘scale=2;1/3′ |bc -l |cut -d’.’ -f2)
[root@bogon ~]# echo ${res}%
33%
[root@bogon ~]# res=$(echo ‘scale=2;1/3′ |bc -l |cut -d’.’ -f2)
[root@bogon ~]# echo ${res}%
33%
test File1 –nt File2 文件1比文件2 新
5.test 下令测试
5.test 指令测试
test File1 –ot File2 文件1比文件2 旧
test
test
test –b File
文件存在并且是块设备文件
-n str 字符串长度不为零
-n str 字符串长度不为零
test –c File
文件存在并且是字符设备文件
-z str 字符串长度为零
-z str 字符串长度为零
test –d File
文件存在并且是目录
-b 文件存在且为块文件
-b 文件存在且为块文件
test –e File 文件存在
-d 文件存在且为目录文件
-d 文件存在且为目录文件
test –f File
文件存在并且是正统文件
-e 文件存在
-e 文件存在
test –g File
文件存在并且是设置了组ID
-f 文件存在且为普通文书
-f 文件存在且为普通文书
test –G File
文件存在并且属于有效组ID
-h 文件存在且为链接文件(同 -L)
-h 文件存在且为链接文件(同 -L)
test –h File
文件存在并且是多个标记链接(同-L)
-s 文件存在且高于零字节
-s 文件存在且高于零字节
test –k File
文件存在并且安装了sticky位
文件之间的可比
文件之间的可比
test –b File
文件存在并且是块设备文件
file1 -nt file2 file1 的创设时间比file2晚
file1 -nt file2 file1 的创导时间比file2晚
test –L File
文件存在并且是3个标记链接(同-h)
file1 -ot file2 file1 的始建时间比file2早
file1 -ot file2 file1 的开创时间比file2早
test –o File
文件存在并且属于有功效户ID
整数之间的相比
整数之间的可比
test –p File
文件存在并且是一个命名管道
int1 -ne int2 int1和int2不相等
int1 -ne int2 int1和int2不相等
test –r File
文件存在并且可读
int1 -eq int2 int1和int2 相等
int1 -eq int2 int1和int2 相等
test –s File
文件存在并且是二个套接字
int1 -lt int2 int1小于int2
int1 -lt int2 int1小于int2
test –t FD
文件讲述符是在二个极端打开的
int1 -le int2 int1紧跟于等于int2
int1 -le int2 int1稍低于等于int2
test –u File
文件存在并且安装了它的set-user-id位
int1 -gt int2 int1大于int2
int1 -gt int2 int1大于int2
test –w File
文件存在并且可写
int1 -ge int2 int1抢先等于int2
int1 -ge int2 int1大于等于int2
test –x File
文件存在并且可实施
字符串之间相比
字符串之间比较
各样标准语句的根基都以判断什么是真什么是假。是还是不是领会其行事规律将决定你编写的是品质一般的台本还是你将引以为荣的脚本。
str1 = str2 str1和str2相等
str1 = str2 str1和str2相等
Shell
脚本的力量时常被低估,但事实上其力量的发挥受制于脚本撰写者的能力。您精晓得越多,您就越能像变戏法似地撰写1个文书来使职责自动化和简化您的管理工科作。
str1 !=str2 str1和str2不相等
str1 !=str2 str1和str2不相等
在 shell
脚本中开始展览的每一个操作(除最简便的一声令下编组之外)都急需检讨标准。全体的
shell 脚本“逻辑” — 广义意义下的“逻辑” — 平时都足以分成以下三大类:
表明式之间的比较
表明式之间的相比较
if {condition exists} then …
expression1 -a expression2 表明式1与表明式2都为真
expression1 -a expression2 表明式1与发挥式2都为真
while {condition exists} do …
expression1 -o expression2 表达式1或发布式2为真
expression1 -o expression2 表明式1或发布式2为真
until {condition exists} do …
6.测试举例
6.测试举例
无论随后的操作是怎么样,这么些依照逻辑的下令都依赖判断一种口径是不是真正存在来支配继续的操作。test
命令是驱动在种种情景下都可以规定要判断的标准化是还是不是留存的实用工具。由此,彻底理解那个命令对于作品成功的
shell 脚本至关心重视要。
数字相比测试:
数字比较测试:
工作原理
[root@bogon ~]# [[ 2 > 1 ]]
[root@bogon ~]# echo $?
0
[root@bogon ~]# ((20>10))
[root@bogon ~]# echo $?
0
[root@bogon ~]# ((20<10))
[root@bogon ~]# echo $?
1
[root@bogon ~]# [[ 2 > 1 ]]
[root@bogon ~]# echo $?
0
[root@bogon ~]# ((20>10))
[root@bogon ~]# echo $?
0
[root@bogon ~]# ((20<10))
[root@bogon ~]# echo $?
1
test 命令最短的概念可能是评估八个表达式;假诺基准为真,则赶回叁个 0
值。倘若表明式不为真,则赶回一个超乎 0 的值 —
也能够将其名为假值。检查最终所执行命令的动静的最便捷方法是运用 $?
值。出于演示的目标,本文中的例子全体行使了这些参数。
字符串测试
字符串测试
test 命令期望在命令行中找到三个参数,当 shell
没有为变量赋值时,则将该变量视为空。这意味着在拍卖脚本时,一旦脚本寻找的参数不设有,则
test 将报告该错误。
[root@bogon ~]# [ "abc" = "abc" ]
[root@bogon ~]# echo $?
0
[root@bogon ~]# [[ "abc" = "abc" ]]
[root@bogon ~]# echo $?
0
[root@bogon ~]# (("abc" = "abc"))
[root@bogon ~]# echo $?
1
[root@bogon ~]# [[ a = a && 1 < 2 ]]
[root@bogon ~]# echo $?
0
[root@bogon ~]# [[ a = a && 1 < 2 ]]
[root@bogon ~]# echo $?
0
[root@bogon ~]# (( a = a || 1 > 2 ))
[root@bogon ~]# echo $?
1
[root@bogon ~]# [[ a = a || 1 > 2 ]]
[root@bogon ~]# echo $?
0
[root@bogon ~]# [ "abc" = "abc" ]
[root@bogon ~]# echo $?
0
[root@bogon ~]# [[ "abc" = "abc" ]]
[root@bogon ~]# echo $?
0
[root@bogon ~]# (("abc" = "abc"))
[root@bogon ~]# echo $?
1
[root@bogon ~]# [[ a = a && 1 < 2 ]]
[root@bogon ~]# echo $?
0
[root@bogon ~]# [[ a = a && 1 < 2 ]]
[root@bogon ~]# echo $?
0
[root@bogon ~]# (( a = a || 1 > 2 ))
[root@bogon ~]# echo $?
1
[root@bogon ~]# [[ a = a || 1 > 2 ]]
[root@bogon ~]# echo $?
0
当试图珍重脚本时,您能够透过将具有参数包涵在双引号中来缓解这些难点。然后
shell 将变量展开,要是变量没有值,那么将传递3个空值给
test。另一种办法是在脚本内扩展三个附加检查进度来判定是还是不是设置了命令行参数。借使没有安装命令行参数,那么脚本会告诉用户缺乏参数,然后退出。大家会经过有些事例来更实际地印证全部那几个剧情。
仅仅比较数字,用(( ))
偏偏相比数字,用(( ))
test 和 [ 命令
除此而外单纯数字之外的比较,用[[ ]]
除了这一个之外单纯数字之外的可比,用[[ ]]
虽然 Linux 和 UNIX
的种种版本中都含有 test 命令,但该命令有多个更常用的外号 —
左方括号:[。test 及其别称常常都可以在 /usr/bin 或 /bin
(取决于操作系统本子和供应商)中找到。
三 、流程序控制制
三 、流程序控制制
当你使用左方括号而非 test
时,其后必须始终跟着1个空格、要评估的原则、叁个空格和右边括号。右方括号不是任刘帅西的外号,而是表示所需评估参数的截至。条件两边的空格是必不可少的,那象征要调用
test,以分别于同一常常选拔方括号的字符/情势匹配操作。
1.if 分支机构
1.if 分支机构
test 和 [ 的语法如下:
1)验证用户账号密码:
1)验证用户账号密码:
test expression
input your name : zhangcan
input password : 123
login successful
[root@bogon ~]# ./usertest.sh
input your name : hha
input password : hag
user or password error
#! /bin/bash
user='zhangcan'
password='123'
read -p 'input your name : ' name
read -p 'input password : ' code
if [ $name = $user -a $code = $password ];then
echo 'login successful'
else
echo 'user or password error'
fi
~
input your name : zhangcan
input password : 123
login successful
[root@bogon ~]# ./usertest.sh
input your name : hha
input password : hag
user or password error
#! /bin/bash
user='zhangcan'
password='123'
read -p 'input your name : ' name
read -p 'input password : ' code
if [ $name = $user -a $code = $password ];then
echo 'login successful'
else
echo 'user or password error'
fi
~
[ expression ]
2)判断成绩档次
2)判断成绩档次
在那三种景况下,test 都评估一个表明式,然后再次回到真或假。假设它和
if、while 或 until
命令结合使用,则您能够对程序流举行广泛的支配。但是,您无需将 test
命令与其余别的协会一起利用;您能够从命令行间接运营它来检查大约任何事物的动静。
#!/bin/bash
#根据用户输入的成绩,判断所属档次,并输出给用户
read -p 'input your score : ' score
if [ $score -ge 90 ];then
echo '优秀'
elif [ $score -ge 70 -a $score -lt 90 ];then
echo '良好'
elif [ $score -ge 60 -a $score -lt 70 ];then
echo '及格'
elif [ $score -lt 60 ];then
echo '较差'
fi
#!/bin/bash
#根据用户输入的成绩,判断所属档次,并输出给用户
read -p 'input your score : ' score
if [ $score -ge 90 ];then
echo '优秀'
elif [ $score -ge 70 -a $score -lt 90 ];then
echo '良好'
elif [ $score -ge 60 -a $score -lt 70 ];then
echo '及格'
elif [ $score -lt 60 ];then
echo '较差'
fi
因为它们相互互为小名,所以使用 test 或 [
均要求3个表明式。表达式一般是文本、数字或文件和目录属性的比较,并且能够分包变量、常量和平运动算符。运算符能够是字符串运算符、整数运算符、文件运算符或布尔运算符
— 我们将在偏下各部分各种介绍每一个运算符。
2.while循环
2.while循环
test 文件运算符
while(条件)
while(条件)
利用那个运算符,您能够在程序中依照对文件类型的评估结果进行不一的操作:
do
do
-b file 若是文件为3个块杰出文件,则为真
命令
命令
-c file 要是文件为多少个字符特殊文件,则为真
done
done
-d file 假使文件为二个索引,则为真
示范:判断用户输入的文本是何连串型
以身作则:判断用户输入的文书是何体系型
-e file 要是文件存在,则为真
#!/bin/bash
while :
do
read -p 'input your file : ' file
if [ -z $file ];then
continue
else
break
fi
done
if [ -f $file ];then
echo "$file is regular file"
elif [ -b $file ];then
echo "$file is block file"
elif [ -d $file ];then
echo "$file is directory file"
else
echo "$file type unkonw"
fi
#!/bin/bash
while :
do
read -p 'input your file : ' file
if [ -z $file ];then
continue
else
break
fi
done
if [ -f $file ];then
echo "$file is regular file"
elif [ -b $file ];then
echo "$file is block file"
elif [ -d $file ];then
echo "$file is directory file"
else
echo "$file type unkonw"
fi
-f file 假若文件为一个一般文书,则为真
3.for循环
3.for循环
-g file 若是设置了文本的 SGID 位,则为真
for i in {1..10}
#in前边不自然是数字,只假设有再次回到结果的吩咐都能够
for i in {1..10}
#in前边不必然是数字,只要是有重回结果的指令都得以
-G file 假设文件存在且归该组全数,则为真
do
do
-k file 假若设置了文本的粘着位,则为真
echo $i
echo $i
-O file 借使文件存在并且归该用户全数,则为真
done
done
-p file 若是文件为贰个命名管道,则为真
演示1:写1个本子,测试子网内能够使用的IP
演示1:写一个本子,测试子网内能够运用的IP
-r file 如果文件可读,则为真
#!/bin/bash
for i in {1..50}
do
ping -c1 192.168.16.$i &> /dev/null # -c1表示ping一次
if [ $? -ne 0 ];then
echo "192.168.16.$i successful"
echo "192.168.16.$i" >> ~/ipavailable.txt
fi
done
~
#!/bin/bash
for i in {1..50}
do
ping -c1 192.168.16.$i &> /dev/null # -c1表示ping一次
if [ $? -ne 0 ];then
echo "192.168.16.$i successful"
echo "192.168.16.$i" >> ~/ipavailable.txt
fi
done
~
-s file 若是文件的长度不为零,则为真
示范2:计算/dev下每个文件类型的多少
演示2:总括/dev下每一种文件类型的数据
-S file 假诺文件为四个套接字特殊文件,则为真
#!/bin/bash
dir='/dev'
for i in $(ls $dir)
do
if [ -h $dir/$i ];then
((link+=1))
elif [ -f $dir/$i ];then
(( rfile+=1))
elif [ -d $dir/$i ];then
((directory+=1))
elif [ -b $dir/$i ];then
(( block+=1 ))
else
(( typeunknow+=1))
fi
done
echo 'block' $block
echo 'regular file' $rfile
echo 'directory' $directory
echo 'link' $link
echo 'unknow' $typeunknow
#!/bin/bash
dir='/dev'
for i in $(ls $dir)
do
if [ -h $dir/$i ];then
((link+=1))
elif [ -f $dir/$i ];then
(( rfile+=1))
elif [ -d $dir/$i ];then
((directory+=1))
elif [ -b $dir/$i ];then
(( block+=1 ))
else
(( typeunknow+=1))
fi
done
echo 'block' $block
echo 'regular file' $rfile
echo 'directory' $directory
echo 'link' $link
echo 'unknow' $typeunknow
-t fd 就算 fd 是2个与终点相连的开拓的文本讲述符(fd 暗许为
1),则为真
4.嵌套循环往复
4.嵌套循环
-u file 即便设置了文本的 SUID 位,则为真
以身作则1:输出1个九九乘法表
以身作则1:输出一个九九乘法表
-w file 假使文件可写,则为真
#!/bin/bash
for ((i=1;i<=9;i++))
do
for ((j=1;j<=i;j++))
do
echo -n "$i*$j=$[$i*$j]"
done
echo
done
#!/bin/bash
for ((i=1;i<=9;i++))
do
for ((j=1;j<=i;j++))
do
echo -n "$i*$j=$[$i*$j]"
done
echo
done
-x file 假设文件可实施,则为真
演示2:验证用户登陆账号密码,登陆成功后能够执行命令,当输入quit时退出
以身作则2:验证用户登陆账号密码,登陆成功后方可执行命令,当输入quit时退出
#!/bin/bash
user='zhangcan'
password='123'
tag=true
while $tag
do
read -p 'input your name : ' name
read -p 'input your password : ' code
if [[ $name = $user ]] && [[ $code = $password ]];then
echo 'login successful'
while $tag
do
read -p '>>: ' cmd
if [[ $cmd = 'quit' ]];then
tag=false
else
$cmd
fi
done
fi
done
#!/bin/bash
user='zhangcan'
password='123'
tag=true
while $tag
do
read -p 'input your name : ' name
read -p 'input your password : ' code
if [[ $name = $user ]] && [[ $code = $password ]];then
echo 'login successful'
while $tag
do
read -p '>>: ' cmd
if [[ $cmd = 'quit' ]];then
tag=false
else
$cmd
fi
done
fi
done
以下示例突显了此简单操作的周转情状:
$ ls -l
total 33
drwxr-xr-w 2 root root 1024 Dec 5 05:05 LST
-rw-rw-rw- 1 emmett users 27360 Feb 6 07:30 evan
-rwsrwsrwx 1 root root 152 Feb 6 07:32 hannah
drwxr-xr-x 2 emmett users 1024 Feb 6 07:31 karen
-rw——- 1 emmett users 152 Feb 6 07:29 kristin
-rw-r–r– 1 emmett users 152 Feb 6 07:29 spencer
$
$ test -r evan
$ echo $?
0
$ test -r walter
$ echo $?
1
$
由于第1次评估为真 — 文件存在且可读 — 再次来到值为真,或
0。由于第3次评估的文本不存在,该值为假,重临值不为零。将值钦定为零或非零很首要,因为在波折时不会始终再次来到1(即便那是平日重临的值),大概回到八个非零值。
正如开首所涉嫌的,除了运用 test 外,您还足以用方括号 [ ]
将下令括住来向 shell 发出同样的命令 — 如下所示:
$ [ -w evan ]
$ echo $?
0
$ [ -x evan ]
$ echo $?
1
$
同样,第三个表明式为真,第③个表达式为假 —
正如重返值所提示的那么。您还是可以使用以下命令将多少个文件相互举行相比:
file1 -ef
file2 测试以咬定多个文本是或不是与同3个装置源源,是还是不是拥有一致的
inode 编号
file1 -nt
file2 测试以判断第一个文件是不是比第③个文本更新(由修改日期决定)
file1 -ot file2
测试以判断第②个公文是不是比第2个公文更旧
以下示例彰显了动用这几个运算符比较文件的结果:
$ [ evan -nt spencer ]
$ echo $?
0
$ [ karen -ot spencer ]
$ echo $?
1
$
名为 evan 的公文比名为 spencer
的公文更新,由此评估为真。类似地,名为 karen 的文书比名为 spencer
的文本更新,由此该评估为假。
字符串比较运算符
如标题所示,那组函数相比较字符串的值。您能够检查它们是或不是留存、是不是一律或然是还是不是不一样。
String 测试以咬定字符串是不是不为空
-n string 测试以判断字符串是还是不是不为空;字符串必须为 test 所识别
-z string 测试以判断字符串是还是不是为空;字符串必须为 test 所识别
string1 = string2 测试以咬定 string1 是还是不是与 string2 相同
string1 != string2 测试以判断 string1 是或不是与 string2 差异
对任何变量实行的最实用的测试之一是判定它的值是还是不是不为空,能够省略地将其坐落
test 命令行中推行这种测试,如下例所示:
$ test “$variable”
强烈提议进行此种测试时用双引号将变量括住,以让 shell
识别变量(即便变量为空)。暗许意况下进行的基本字符串评估和 -n
测试从效能上讲是同样的,如以下示例所示:
#example1
if test -n “$1”
then
echo “$1”
fi
执行以上例子中的代码将基于 $1 是还是不是存在给出以下结果:
$ example1 friday
friday
$
$ example1
$
如果将代码更改为以下方式,则结果将一律:
#example2
if test “$1”
then
echo “$1”
fi
如下所示:
$ example2 friday
friday
$
$ example2
$
全数那么些表明,经常不需求 -n,它意味着暗许操作。
要从四个不一样的角度来查看各样只怕,您能够用另三个摘取来替换
-n,并检查该值是还是不是为空(相对于非空)。那能够用 -z 选项来兑现,代码为:
#example3
if test -z “$1”
then
echo “no values were specified”
fi
运营如下:
$ example3
no values were specified
$ example3 friday
$
借使在从来不命令行参数的事态下运维该程序,而表明式评估为真,那么将举办顺序块中的文本。假设在命令行中有值,则脚本退出,不履行别的操作。将评估操作放在脚本的早先卓殊有用,那足以在大概产生错误的尤其处理之前先行检查变量值。
别的的字符串运算符对七个变量/字符串之间的精确匹配或内部的出入(您也足以称呼等价性和“不等价性”)举办业评比估。第三个例证对男才女貌举办测试:
$ env
LOGNAME=emmett
PAGER=less
SHELL=/bin/bash
TERM=linux
$
$ [ “$LOGNAME” = “emmett” ]
$ echo $?
0
$
$ [ “$LOGNAME” = “kristin” ]
$ echo $?
1
$
也许,该评估能够以脚本的款型用于决定是不是运行脚本:
#example4
if [ “$LOGNAME” = “emmett” ]
then
echo “processing beginning”
else
echo “incorrect user”
fi
那种格局能够用来探寻任意的值(如终端体系或 shell
类型),在同意脚本运营之前那么些值必须同盟。请小心,= 或 !=
运算符的先期级高于其它大部分可内定选项,且需要必须伴有表明式。因而,除了相比字符串的取舍之外,=
或 !=
都不可能和自小编批评某种东西(如可读文件、可执行文件或目录)的存在性的抉择一起使用。
整数相比较运算符
正如字符串相比运算符验证字符串相等或分歧一样,整数比较运算符对数字执行同一的效果。要是变量的值匹配则表达式测试为真,假如不包容,则为假。整数相比较运算符不处理字符串(正如字符串运算符不处理数字同样):
int1 -eq int2 如果 int1 等于 int2,则为真
int1 -ge int2 假若 int1 大于或等于 int2,则为真
int1 -gt int2 如果 int1 大于 int2,则为真
int1 -le int2 若是 int1 小于或等于 int2,则为真
int1 -lt int2 如果 int1 小于 int2,则为真
int1 -ne int2 如果 int1 不等于 int2,则为真
以下示例展现了一个代码段,个中在命令行中给出的值必须等于 7:
#example5
if [ $1 -eq 7 ]
then
echo “You’ve entered the magic number.”
else
echo “You’ve entered the wrong number.”
fi
运行中:
$ example5 6
You’ve entered the wrong number.
$
$ example5 7
You’ve entered the magic number.
$
和字符串一样,相比较的值可以是在脚本外为变量赋的值,而不必总是在命令行中提供。以下示例演示了贯彻那或多或少的一种方法:
#example6
if [ $1 -gt $number ]
then
echo “Sorry, but $1 is too high.”
else
echo “$1 will work.”
fi
$ set number=7
$ export number
$ example6 8
Sorry, but 8 is too high.
$ example6 7
7 will work.
$
整数相比运算符最棒的用处之一是评估钦赐的一声令下行变量的数据,并认清它是或不是符合所要求的专业。例如,假诺有个别特定的命令只可以在有五个或更少变量的情形下运维,
#example7 – display variables, up to three
if [ “$#” -gt 3 ]
then
echo “You have given too many variables.”
exit $#
fi
只要内定多少个或更少的变量,该示例脚本将符合规律运营(并再次来到值
0)。假如钦命了四个以上的变量,则将彰显错误新闻,且例程将脱离 —
同时重临与命令行中给定的变量数相等的淡出代码。
对那么些历程进展修改能够用来在允许运行报表此前判断当天是还是不是是本月的末段几天:
#example8 – to see if it is near the end of the month#
set `date` # use backward quotes
if [ “$3” -ge 21 ]
then
echo “It is close enough to the end of the month to proceed”
else
echo “This report cannot be run until after the 21st of the month”
exit $3
fi
在这几个事例中,设置了八个变量(通过空格相互分开):
$1 = Fri
$2 = Feb
$3 = 6
$4 = 08:56:30
$5 = EST
$6 = 2004
那个值能够在本子中应用,如同它们是在命令行中输入的等同。请小心,退出命令再度归来3个值
— 在那种情景下,再次回到的值是从 $3
的值中赢得的日子。这一技巧在故障诊断时会非常有效 —
借使您觉得脚本应该运转而尚未运转,那么请查看 $? 的值。
一体系似的想法只怕是写作叁个只在每种月的第伍个星期五运转的脚本。第几个星期天一定在该月的
15 日到 21 日里边。使用 cron,您能够调用脚本在 15 日到 2十十四日以内天天的一个钦点时间运作,然后选用脚本的第贰行检查
$1(在安装日期未来)的值是还是不是为 Thu。要是为
Thu,那么执行剩下的剧本,假若不是,则脱离。
而另二个想方设法大概是,只允许脚本在超过 6:00 p.m.
(18:00),全数用户都回家未来运营。只要撰写剧本,使其在值小于 18
时退出,并由此运用以下命令来收获时间(将其设为 $1)
set `date +%H`
布尔运算符
布尔运算符在大概每一种语言中的工作措施都一样 — 包蕴 shell 脚本。在
nutshell
中,它们检查七个规范为真或为假,也许针对假的规范而不是真的条件选取操作。与
test 搭配使用的演算符有
! expr 即使表达式评估为假,则为真
expr1 -a expr2 万一 expr1 和 expr2 评估为真,则为真
expr1 -o expr2 假诺 expr1 或 expr2 评估为真,则为真
可以用 != 运算符代替 =
进行字符串评估。那是最简易的布尔运算符之一,对 test 的例行结果取非。
其他多少个运算符中的首先个是 -a(即
AND)运算符。要使测试最后为真,七个表明式都无法不评估为真。假若其它3个评估为假,则全部育项目测验试将评估为假。例如,
$ env
HOME=/
LOGNAME=emmett
MAIL=/usr/mail/emmett
PATH=:/bin:/usr/bin:/usr/lbin
TERM=linux
TZ=EST5:0EDT
$
$ [ “$LOGNAME” = “emmett” -a “$TERM” =
“linux” ]
$ echo $?
0
$
$ [ “LOGNAME” = “karen” -a “$TERM” = “linux” ]
$ echo $?
1
$
在首先个评估中,多少个原则都测试为真(在二个 linux 终端上登录的是
emmett),由此全部评估为真。在其次个评估中,终端检查无误但用户不得法,因而总体评估为假。
简单的讲,AND
运算符可以确定保证代码只在八个规格都满意时才实施。相反,只要任何二个表明式测试为真,O酷威(-o)
运算符即为真。大家来修改先前的事例,并将其放置贰个本子中来证实那一点:
#example9
if [ “$LOGNAME” = “emmett” -o “$TERM” = “linux” ]
then
echo “Ready to begin.”
else
echo “Incorrect user and terminal.”
fi
$ env
HOME=/
LOGNAME=emmett
MAIL=/usr/mail/emmett
PATH=:/bin:/usr/bin:/usr/lbin
TERM=linux
TZ=EST5:0EDT
$ example9
Ready to begin.
$
$ LOGNAME=karen
$ example9
Ready to begin.
$
在本子第①遍运转时,评估判断用户是还是不是等于 emmett。借使发现用户等于
emmett,则脚本转至 echo 语句,并跳过任何的检查。它从不检查终端是不是等于
linux,因为它只须要找到一条为真正语句就足以使整个运算为真。在剧本第贰回运转时,它判断用户不是
emmett,因而它将检查并发现终点确实是
linux。由于1个规则为真,脚本现在转至 echo
命令。为了引出第3条音讯,八个规范都必须为假。
在此前明显时间是否为月末的例子中,能够实施类似的自我批评来幸免用户准备在星期六运营脚本:
#example10 – Do not let the script run over the weekend#
set `date` # use backward quotes
if [ “$1” = “Sat” -o “$1” = “Sun” ]
then
echo “This report cannot be run over the weekend.”
fi
一些卓有作用的演示
示例
1:在剧本文件中冒出的“逻辑”的最简便的格局(如本文全体示例中所示)是“if
…
then”语句。先前的一个代码段检查是或不是存在必然数量的变量,然后将这个变量回显。若是大家对此稍微做一些修改,比如大家想回显变量,并且每一遍回显均减去最右边的变量,以显示一个倒的三角。
即便那听起来很简短,但实在并非如此;那是您在实践大规模拍卖时想完结的措施:处理第陆个变量、转移、处理下二个变量……
出于演示的指标,能够按以下办法撰写脚本中的首要行:
#example11 – display declining variables, up to three
if [ “$#” -gt 3 ] # see if more than three variables are given
then
echo “You have given more than three variables.”
exit
fi
echo $*
if test -n “$2”
then
shift
echo $*
fi
if test -n “$2”
then
shift
echo $*
fi
它将按以下方法实行:
$ example11 one
one
$
$ example11 one two
one two
two
$
$ example11 one two three
one two three
two three
three
$
$ example11 one two three four
You have given more than three variables.
$
鉴于检查的目的将数据限制为八个变量的原因是减少在例子中要检查的行数。一切都依照地开始展览,尽管它令人疑忌地混乱;用户因采纳了跨越程序依设计所能处理的变量数而赢得警示,且脚本退出。借使变量数为
3 或更少,则运算的中坚部分起先实践。
回显变量,执行测试以查看另七个变量是还是不是存在。借使另二个变量存在,则实施二次转移,回显该变量,执行另一测试,等等。总共使用了
16 个有效行,而先后仅能处理不当先八个变量 —
非凡混乱。假使消除变量数的限量,程序能够处理任意数量的变量。经过一些修改,脚本被浓缩(美化)了,并能处理任意数量的变量:
#example12 – display declining variables, any number
while [ “$#” -gt 0 ]
do
echo $*
shift
done
$ example12 1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
2 3 4 5 6 7 8 9 0
3 4 5 6 7 8 9 0
4 5 6 7 8 9 0
5 6 7 8 9 0
6 7 8 9 0
7 8 9 0
8 9 0
9 0
0
以往收缩到唯有 多少个有效行,且消除了第三个脚本八个变量的范围,并在运维时要更敏捷。
示例
2:无论几时当在脚本内实施与处理有关的操作时,下2个操作将平昔检查上一操作的景色,以确认它已成功做到。您能够透过检查
$? 的景况并证实它约等于 0
来促成这一指标。例如,假若3个数量目录是或不是能访问十分重要,
#example13
TEMP=LST
cd $TEMP
if [ $?-ne 0 ]
then
echo “Data directory could not be found.”
Exit
fi
处理错误
资源
下载针对 Linux
的 Oracle 数据库
10g
Oracle 数据库 10g 第 1 版
(10.1.0.2) 今后可用来 Linux x86 和 Linux Itanium 平台;请在此从 OTN
上免费下载。
访问 Linux 技术为主
收藏本页,以获得有关 Linux
系统一管理理员最好应用的貌似技术音信,以及关于
Oracle-on-Linux
产品群的有血有肉技术音信。
相关文章
Linux 相关技能小说的存档
test
命令常常现身的失实事实上唯有三种档次。第3种是未利用正确的评估项目,例如将字符串变量与整型变量实行比较只怕将带填充的字符串与不带填充的字符串进行相比。仔细评估您使用的变量将使你最终找到错误的起点,并让您能够化解这么些标题。
第二种错误类型包涵将方括号误认为别称之外的有个别东西。方括号与其内容之间必须有三个空格;不然,它们将无法表达在那之中的对象。例如,
$ [ “$LOGNAME” -gt 9]
test:] missing
$
请留意,错误音讯提示 test 存在难点,即便接纳了别称
]。那一个难点很不难发觉,因为错误新闻确切地将这么些题材显得出来,然后你能够扩展要求的空格。
结论
要在 shell
脚本中营造逻辑,您必须添加条件语句。每一条那种话语的主导都以对规范的评估,以咬定它是还是不是留存
— 通过选拔 test 命令完成评估。掌握它和它的别称(左方括号
([)的劳作规律将使你能够撰写能够成功部分复杂操作的 shell 脚本
转载自: