(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:[email protected]")
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:[email protected]
#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
①编辑脚本文件
[root@RHCE11 chap01]# vim + ./test1.sh
#!/bin/bash
#########################
#File name:./test1.sh
#Version:v1.0
#Email:[email protected]
#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
①编辑脚本文件
[root@RHCE11 chap01]# vim + ./test2.sh
#!/bin/bash
#########################
#File name:./test2.sh
#Version:v1.0
#Email:[email protected]
#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'
(1)程序的文件名;
(2)共有几个参数;
(3)若参数的个数小于2个则告知用户参数数量太少;
(4)全部的参数内容;第一个参数;第二个参数。
①编辑脚本文件
[root@RHCE11 chap01]# vim + ./test3.sh
#!/bin/bash
#########################
#File name:./test3.sh
#Version:v1.0
#Email:[email protected]
#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