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): y
OK, continue...
debian@debian-pc:~/scripts$ ./sh06.sh
Please input (Y/N): gg
I don't know what your choice is !?
3.shell script的默认变量(
0,
1…)
需求:
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 cc
The script name is ==> ./sh07.sh
Total parameter number is ==> 3
Your whole parameter is ==> aa bb cc
Test 'aa bb cc'is ==>aa bb cc
The 1st parameter ==> aa
The 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 six
Total parameter number is ==> 6
Your whole parameter is ==> 'one two three four five six'
Total parameter number is ==> 5
Your whole parameter is ==> 'two three four five six'
Total parameter number is ==>2
Your whole parameter is ==> 'five six'