shell条件测试

一、文件测试表达式

题目:让用户输入一个文件名,并做如下判断

(1)如果用户输入的文件为空时显示:you must input a filename,并中断程序;

(2)如果用户输入的文件不存在时,显示the file do not exist,并中断程序;

(3)如果文件存在,判断该文件的文件类型和执行者对该文件所拥有的的权限。

①编辑.sh文件时自动生成关于脚本文件说明的注释

[root@RHCE11 ~]# vim /root/.bashc
autocmd BufNewFile *.py,*.cc,*.sh,*.java exec ":call SetTitle()"
func SetTitle()
  if expand("%:e") == 'sh'
     call setline(1,"#!/bin/bash")
     call setline(2,"#########################")
     call setline(3,"#File name:".expand("%"))
     call setline(4,"#Version:v1.0")
     call setline(5,"#Email:admin@test.com")
     call setline(6,"#Created time:".strftime("%F %T"))
     call setline(7,"#Description:")
     call setline(8,"#########################")
     call setline(9,"")
  endif
endfunc

②创建文件并编辑(使用test和[<测试表达式>]的条件测试语法进行编辑文件,注意:test命令和<测试表达式>之间至少有一个空格 ;[]的边界和内容之间至少有一个空格

[root@RHCE11 ~]# mkdir /shell/
[root@RHCE11 ~]# mkdir /shell/chap01/
[root@RHCE11 ~]# cd /shell/chap01/
[root@RHCE11 chap01]# vim ./test.sh
#!/bin/bash
#########################
#File name:./test.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-12-09 12:44:15
#Description:
#########################
read -p "please input a filename:" filename
test -z $filename && echo "you must input a filename" && exit 1
! [ -a $filename ] && echo "the file $filename does not exist" && exit 2
[ -L $filename ] && echo "filetype: link file" || ([ -d $filename ] && echo "filetype: directory file")
test -f $filename && echo "filetype:regulare file"
test -r $filename && perm="readable"
test -w $filename && perm="$perm writable"
test -x $filename && perm="$perm executable"
echo "perm is $perm"

③修改权限

[root@RHCE11 chap01]# chmod a+rx ./test.sh

④测试

[root@RHCE11 chap01]# ./test.sh;echo $?
please input a filename:
you must input a filename
1
[root@RHCE11 chap01]# ./test.sh;echo $?
please input a filename:1
the file 1 does not exist
2
[root@RHCE11 chap01]# ./test.sh;echo $?
please input a filename:test.sh
filetype:regulare file
perm is readable writable executable
0
[root@RHCE11 chap01]# ./test.sh;echo $?
please input a filename:/sbin
filetype: link file
perm is readable writable executable
0

二、逻辑操作符

实验1:通过read传入一个数字,如果传入的数字等于1,就打印1;如果等于2,就打印2,如果不等于1也不等于2,就提示输入不对,然后退出程序。

①编辑脚本文件

[root@RHCE11 chap01]# vim + ./test1.sh
#!/bin/bash
#########################
#File name:./test1.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-12-09 14:27:41
#Description:
#########################
read -p "please input a number:" num
expr "$num" + 1 &> /dev/null        #将标准输出以及标准错误输出重定向到黑洞
[ $? -ne 0 ] &&echo 'please input a "number"!!!' && read -p "please input a number:" num
[ $num -eq 1 ] && echo 1 && exit 0
[ $num -eq 2 ] && echo 2 && exit 0
echo "error"

②修改权限

[root@RHCE11 chap01]# chmod a+rx ./test1.sh

③测试

[root@RHCE11 chap01]# ./test1.sh
please input a number:1
1
[root@RHCE11 chap01]# ./test1.sh
please input a number:2
2
[root@RHCE11 chap01]# ./test1.sh
please input a number:3
error
[root@RHCE11 chap01]# ./test1.sh
please input a number:q
please input a "number"!!!
please input a number:3
error

实验2:通过read读入两个整数,并比较他们的大小。

①编辑脚本文件

[root@RHCE11 chap01]# vim + ./test2.sh
#!/bin/bash
#########################
#File name:./test2.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-12-09 15:09:32
#Description:
#########################
read -p "please input your two 'number':" a b        #给用户强调某部分内容时可以用''将内容引起以引起用户的注意
expr "$a" + "$b" + 1 &> /dev/null
[ $? -ne 0 ] && {
                  echo "please input two 'int'"
                  exit 1
}
[ $a -gt $b ] && echo "$a > $b" && exit 0
[ $a -eq $b ] && echo "$a = $b" && exit 0
echo "$a < $b"

②修改权限

[root@RHCE11 chap01]# chmod a+rx ./test2.sh

③测试

[root@RHCE11 chap01]# ./test2.sh
please input your two 'number':1 2
1 < 2
[root@RHCE11 chap01]# ./test2.sh
please input your two 'number':4 2
4 > 2
[root@RHCE11 chap01]# ./test2.sh
please input your two 'number':3 3
3 = 3
[root@RHCE11 chap01]# ./test2.sh
please input your two 'number':1 q
please input two 'int'
[root@RHCE11 chap01]# ./test2.sh
please input your two 'number':q w
please input two 'int'

实验3:假设执行一个可以携带参数的script,执行该脚本后屏幕会显示如下的数据

(1)程序的文件名;

(2)共有几个参数;

(3)若参数的个数小于2个则告知用户参数数量太少;

(4)全部的参数内容;第一个参数;第二个参数。

①编辑脚本文件

[root@RHCE11 chap01]# vim + ./test3.sh
#!/bin/bash
#########################
#File name:./test3.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-12-09 15:19:08
#Description:
#########################
echo "the script name is $0"
echo "the parameter number is $#"
[ "$#" -lt 2 ] && echo "the number of parameter is less than 2." && exit 0
echo "your whole parameter is '$@'"
echo "the 1st parameter is $1"
echo "the 2nd parameter is $2"

②修改权限

[root@RHCE11 chap01]# vim + ./test3.sh

③测试

[root@RHCE11 chap01]# ./test3.sh
the script name is ./test3.sh
the parameter number is 0
the number of parameter is less than 2.
[root@RHCE11 chap01]# ./test3.sh 1 2
the script name is ./test3.sh
the parameter number is 2
your whole parameter is '1 2'
the 1st parameter is 1
the 2nd parameter is 2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值