连载shell(二):test判断,判断符号[ ],shell script的默认变量($0, $1...),shift参数变量偏移
来源:互联网 发布:三只松鼠 淘宝 编辑:程序博客网 时间:2024/05/20 07:36
1.利用test命令的测试功能:
需求:
让用户输入一个文件名,我们判断:
1)这个文件是否存在,若不存在则给予一个“Filename does not exist”的信息,并中断程序;
2)若这个文件存在,则判断它是个文件或目录,结果输出“Filename is regular file”或“Filename is directory”;
3)判断一下,执行者的身份对这个文件或目录所拥有的权限,并输出权限数据。
debian@debian-pc:~/scripts$ vim sh05.sh 1 #!/bin/bash 8 #1.让用户输入文件名,并且判断用户真的有输入字符串 9 echo -e "Please input a filename, I will check the filename's type and permissions. \n\n" 10 read -p "Input a filename : " filename 11 test -z $filename && echo "You MUST input a filename." && exit 0 12 13 #2.判读文件是否存在,若不存在则显示信息并结束脚本 14 test ! -e $filename && echo "The filename '$filename' DO NOT exist" && exit 0 15 16 #3.开始判断文件类型及属性 17 test -f $filename && filetype = "regulare file" 18 test -d $filename && filetype = "directory" 19 test -r $filename && perm = "readable" 20 test -w $filename && perm = "perm writable" 21 test -x $filename && perm = "perm executable" 22 23 #4.开始输出信息 24 echo "The filename: $filename is a $filetype" 25 echo "And the permissions are : $perm"
*************************************************************#不输入任何字符,结果如下:debian@debian-pc:~/scripts$ ./sh05.sh Please input a filename, I will check the filename's type and permissions. Input a filename : You MUST input a filename.*************************************************************#输入字符,结果如下:debian@debian-pc:~/scripts$ ./sh05.sh Please input a filename, I will check the filename's type and permissions. Input a filename : sc The filename 'sc' DO NOT exist
2.利用判断符号[]
需求:
1)当执行一个程序的时候,这个程序会让用户选择Y或N;
2)如果用户输入Y或y时,就显示“OK,continue”;
3)如果用户输入N或n时,就显示“Oh,interrupt!”;
4)如果不是Y/y/N/n之内的其他字符,就显示“I don`t know what your choice is”。
1 #!/bin/bash 7 read -p "Please input (Y/N): " yn 8 [ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK, continue..." && exit 0 9 [ "$yn" == "N" -o "$yn" == "n" ] && echo "Oh, interrupt!!" && exit 0 10 echo -e "I don't know what your choice is !?" && exit 0
debian@debian-pc:~/scripts$ ./sh06.sh Please input (Y/N): yOK, continue...debian@debian-pc:~/scripts$ ./sh06.sh Please input (Y/N): ggI don't know what your choice is !?
3.shell script的默认变量(
需求:
1)程序的文件名;
2)共有几个参数;
3)若参数的个数小于2则告知用户参数数量太少;
4)全部的参数内容;
5)第一个参数;
6)第二个参数。
1 #!/bin/bash 7 echo "The script name is ==> $0" 8 echo "Total parameter number is ==> $#" 9 [ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop here." && exit 0 10 echo "Your whole parameter is ==> $@" 11 echo "Test '$*'is ==>$*" 12 echo "The 1st parameter ==> $1" 13 echo "The 2nd parameter ==> $2"
debian@debian-pc:~/scripts$ ./sh07.sh aa bb ccThe script name is ==> ./sh07.shTotal parameter number is ==> 3Your whole parameter is ==> aa bb ccTest 'aa bb cc'is ==>aa bb ccThe 1st parameter ==> aaThe 2nd parameter ==> bb
4.shift:造成参数变量号码偏移
1 #!/bin/bash 7 echo "Total parameter number is ==> $#" 8 echo "Your whole parameter is ==> '$@'" 9 shift #进行第一次“一个变量的shift” 10 echo "Total parameter number is ==> $#" 11 echo "Your whole parameter is ==> '$@'" 12 shift 3 #进行第二次“三个变量的shift” 13 echo "Total parameter number is ==>$#" 14 echo "Your whole parameter is ==> '$@'"
debian@debian-pc:~/scripts$ ./sh08.sh one two three four five sixTotal parameter number is ==> 6Your whole parameter is ==> 'one two three four five six'Total parameter number is ==> 5Your whole parameter is ==> 'two three four five six'Total parameter number is ==>2Your whole parameter is ==> 'five six'
0 0
- 连载shell(二):test判断,判断符号[ ],shell script的默认变量($0, $1...),shift参数变量偏移
- Shell script 的默认变量($0, $1...)
- Shell script 传参数处理(默认变量)
- shell script 的默认参数($0,$1...)
- Linux学习-shell script 的默认变量
- shell 变量 判断 循环
- Shell script的语法二:判断命令
- Shell 的变量判断与比较
- shell 判断变量为空
- SHELL 变量处理判断式
- shell-变量、判断、重复动作
- shell中test命令及判断符号[ ]的用法统计
- 【Linux Shell】判断输入变量或者参数是否为空
- shell中的条件判断、参数以及变量替换
- shell中的条件判断、参数以及变量替换
- shell中的条件判断、参数以及变量替换
- 【Linux Shell】判断输入变量或者参数是否为空
- shell中的条件判断、参数以及变量替换
- 创建k8s集群(仅限于理论,无实践)
- AngulaJs中的简单数据绑定
- sublime3 Scala编译环境搭建
- 再论Epool
- JSON简介
- 连载shell(二):test判断,判断符号[ ],shell script的默认变量($0, $1...),shift参数变量偏移
- JSON格式规定
- C++作业7
- debian7 +tomcat部署war遇到的问题
- androlyze(或是其他Python程序)出现No module named traitlets.config异常的解决方法
- MFC:窗口指针、窗口句柄和窗口ID的相互转换
- 闭包
- c++作业-7
- C++第七次实验