Linux学习之Shell Scripts(二)

一、test判断式

(1)关于某个档名的文件类型判断

测试的标志代表意义
-e该档名是否存在?
-f该档名是否为存在且为档案(file)?
-d该文件名是否存在且为目录(directory)?

 

 

 

 

 

[root@localhost dalianmao]# ll
total 64
-rw-rw-r--. 1 dalianmao dalianmao   18 Nov  5 08:42 catfile
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Desktop
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Documents
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Downloads
-rw-rw-r--. 1 dalianmao dalianmao 2784 Nov  5 08:13 homefile
-rw-rw-r--. 1 dalianmao dalianmao   36 Oct 18 11:01 list_error
-rw-rw-r--. 1 dalianmao dalianmao    9 Oct 18 11:01 list_right
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Music
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Pictures
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Public
-rw-rw-r--. 1 dalianmao dalianmao  665 Oct 21 11:07 regular_express.txt
-rw-rw-r--. 1 dalianmao dalianmao  108 Nov  5 08:24 rootfile
drwxrwxr-x. 2 dalianmao dalianmao 4096 Nov  8 12:40 scripts
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Templates
-rw-rw-r--. 1 dalianmao dalianmao  125 Oct  1 06:23 test.txt
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Videos
[root@localhost dalianmao]# test -e scripts/ && echo "exist" || echo "not exist"
exist
[root@localhost dalianmao]# test -e regular_express.txt  && echo "exist" || echo "not exist"
exist
[root@localhost dalianmao]# test -f regular_express.txt  && echo "exist" || echo "not exist"
exist
[root@localhost dalianmao]# test -f dalianmao.txt && echo"exist" || echo "not exist"
not exist
[root@localhost dalianmao]# test -d scripts/ && echo "exist" || echo "not exist"
exist
[root@localhost dalianmao]# test -d wzc && echo "exist" || echo "not exist"
not exist

 (2)关于档案的权限的判断

测试的标志代表的意义
-r该档案是否有可读权限
-w该档案是否有可写权限
-x该档案是否有可执行权限
-s该档案是否存在且为非空白档案

 

 

 

 

 

 

[root@localhost dalianmao]# ll
total 64
-rw-rw-r--. 1 dalianmao dalianmao   18 Nov  5 08:42 catfile
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Desktop
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Documents
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Downloads
-rw-rw-r--. 1 dalianmao dalianmao 2784 Nov  5 08:13 homefile
-rw-rw-r--. 1 dalianmao dalianmao   36 Oct 18 11:01 list_error
-rw-rw-r--. 1 dalianmao dalianmao    9 Oct 18 11:01 list_right
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Music
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Pictures
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Public
-rw-rw-r--. 1 dalianmao dalianmao  665 Oct 21 11:07 regular_express.txt
-rw-rw-r--. 1 dalianmao dalianmao  108 Nov  5 08:24 rootfile
drwxrwxr-x. 2 dalianmao dalianmao 4096 Nov  8 12:40 scripts
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Templates
-rw-rw-r--. 1 dalianmao dalianmao  125 Oct  1 06:23 test.txt
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Videos
[root@localhost dalianmao]# test -r catfile  && echo "exist"||echo "not exist"
exist
[root@localhost dalianmao]# test -w catfile  && echo "exist"||echo "not exist"
exist
[root@localhost dalianmao]# test -x catfile  && echo "exist" || echo "not exist"
not exist
[root@localhost dalianmao]# test -s catfile  && echo "exist" || echo "not exist"
exist
[root@localhost dalianmao]# touch dalianmao.py
[root@localhost dalianmao]# ll
total 64
-rw-rw-r--. 1 dalianmao dalianmao   18 Nov  5 08:42 catfile
-rw-r--r--. 1 root      root         0 Nov  9 07:27 dalianmao.py
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Desktop
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Documents
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Downloads
-rw-rw-r--. 1 dalianmao dalianmao 2784 Nov  5 08:13 homefile
-rw-rw-r--. 1 dalianmao dalianmao   36 Oct 18 11:01 list_error
-rw-rw-r--. 1 dalianmao dalianmao    9 Oct 18 11:01 list_right
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Music
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Pictures
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Public
-rw-rw-r--. 1 dalianmao dalianmao  665 Oct 21 11:07 regular_express.txt
-rw-rw-r--. 1 dalianmao dalianmao  108 Nov  5 08:24 rootfile
drwxrwxr-x. 2 dalianmao dalianmao 4096 Nov  8 12:40 scripts
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Templates
-rw-rw-r--. 1 dalianmao dalianmao  125 Oct  1 06:23 test.txt
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Videos
[root@localhost dalianmao]# test -s dalianmao.py && echo "exist" || echo "not exist"
not exist

(3)两个整数之间的判定

测试的标志代表的意义
-eq两数值相等(equal)
-ne两数不等(not equal)
-gtnum1 大于 num2 (greater than)
-ltnum1 小于 num2(less than)
-genum1 大于等于 num2(greater than or equal)
-lenum1 小于等于num2(less than or equal)

 

 

 

 

 

 

 

 

[root@localhost dalianmao]# num1=13;num2=13;num3=15;num4=10
[root@localhost dalianmao]# test $num1 -eq $num2 && echo "true"||echo "false"
true
[root@localhost dalianmao]# test $num1 -ne $num3 && echo "true"||echo "false"
true
[root@localhost dalianmao]# test $num1 -gt $num4 && echo "true"||echo "false"
true
[root@localhost dalianmao]# test $num1 -lt $num3 && echo "true"||echo "false"
true
[root@localhost dalianmao]# test $num1 -ge $num2 && echo "true"||echo "false"
true
[root@localhost dalianmao]# test $num1 -le $num2 && echo "true"||echo "false"
true

(4)判定字符串数据

测试的标志代表的意义
test -z string判定字符串是否为0?若string为空字符串,则为true
test -n string

判定字符串是否为非0?若string为空字符串,则为false

注:-n可省略

test str1=str2判定str1 是否等于str2,若相等,则回传true
test str1 !=str2判定str1是否不等于str2,若相等,则回传false

 

 

 

 

 

 

(5)多重判定条件

测试的标志代表的意义
-a(and) 两状况同时成立!例如test -r file -a -x file,则file同时具有r与x权限时,才回传true。
-o(or)两状况任何一个成立!例如test -r file -o -x file,则file具有r或x权限,就回传true。
!反相状态,如test ! -x file,当file不具有x时,回传true

 

 

 

 

 

 

(6) shell练习题

    1、输入档案名,如果没有输入提示并退出

    2、判断档案是否存在,如果不存在,输出the file filename do not exists!,并退出程序

    3、若档案存在,则判断该档案为file还是directory,并输出the filename is regular file 或者

    the filename is directory。

    4、判断执行者对于这个文件的权限,并输出。

[dalianmao@localhost ~]$ ll
total 68
-rw-rw-r--. 1 dalianmao dalianmao   18 Nov  5 08:42 catfile
-rw-r--r--. 1 root      root         0 Nov  9 07:27 dalianmao.py
-rw-rw-r--. 1 dalianmao dalianmao  897 Nov 12 10:11 dalianmao.sh
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Desktop
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Documents
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Downloads
-rw-rw-r--. 1 dalianmao dalianmao 2784 Nov  5 08:13 homefile
-rw-rw-r--. 1 dalianmao dalianmao   36 Oct 18 11:01 list_error
-rw-rw-r--. 1 dalianmao dalianmao    9 Oct 18 11:01 list_right
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Music
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Pictures
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Public
-rw-rw-r--. 1 dalianmao dalianmao  665 Oct 21 11:07 regular_express.txt
-rw-rw-r--. 1 dalianmao dalianmao  108 Nov  5 08:24 rootfile
drwxrwxr-x. 2 dalianmao dalianmao 4096 Nov  8 12:40 scripts
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Templates
-rw-rw-r--. 1 dalianmao dalianmao  125 Oct  1 06:23 test.txt
drwxr-xr-x. 2 dalianmao dalianmao 4096 Jul 11 15:01 Videos
[dalianmao@localhost ~]$ sh dalianmao.sh 
Please input a filename.


Keyin a filename: 
You must input a filename
[dalianmao@localhost ~]$ sh dalianmao.sh
Please input a filename.


Keyin a filename:dashabi
the file dashabi do not exists!
[dalianmao@localhost ~]$ sh dalianmao.sh
Please input a filename.


Keyin a filename:scripts               
The scripts is directory !
The excutor has these permissions:readable writable excutable
[dalianmao@localhost ~]$ sh dalianmao.sh 
Please input a filename.


Keyin a filename:regular_express
the file regular_express do not exists!
[dalianmao@localhost ~]$ sh dalianmao.sh 
Please input a filename.


Keyin a filename:regular_express.txt
The regular_express.txt is regular file !
The excutor has these permissions:readable writable
[dalianmao@localhost ~]$ nl dalianmao.sh 
     1	#!/bin/bash
     2	#Program:
       
     3	#    user keyin filename,program will check flowing:
       
     4	#   (1)exist?(2)file or directory? (3)file permissions
       
     5	# History:
       
     6	# 2018/11/12 dalianmao v1.0
       
     7	PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
     8	export PATH
       
     9	#输入档名
    10	echo -e "Please input a filename.\n\n"
       
    11	read -p "Keyin a filename:" filename
       
    12	test -z $filename && echo "You must input a filename" && exit 0
       
    13	#判断档案是否存在
    14	test ! -e $filename && echo "the file $filename do not exists!" && exit 0
    15	# 判断档案为file还是directory?判断执行者对于该文件的权限
    16	test -f $filename && filetype="regular file"
    17	test -d $filename && filetype="directory"
    18	test -r $filename && perm="readable"
    19	test -w $filename && perm="$perm writable"
    20	test -x $filename && perm="$perm excutable"
    21	#打印
    22	echo "The $filename is $filetype !"
    23	echo "The excutor has these permissions:$perm"

二、[ ] 判断式

(1) 判断变量是否为空?

[dalianmao@localhost ~]$ str=dalianmao
[dalianmao@localhost ~]$ [ -z "$str" ];echo $?
1
[dalianmao@localhost ~]$ str=''
[dalianmao@localhost ~]$ [ -z "$str" ];echo $?
0

(2)判断两变量是否相等

[dalianmao@localhost ~]$ str1=dalianmao;str2=$str1;str3=haha
[dalianmao@localhost ~]$ echo $str1 $str2 $str3
dalianmao dalianmao haha
[dalianmao@localhost ~]$ [ "$str1" == "$str2" ];echo $?
0
[dalianmao@localhost ~]$ [ "$str1" == "$str3" ];echo $?
1

(3)注意

1、在中括号[ ] 中每个组件都需要用空格键来分隔;

2、在中括号中的变量,都要用双引号括起来;

3、在中括号中的常量,要用单引号或者双引号括起来;

(4)当变量str或者str1不为空时,回传true(此处要表达的是-o 或者-a要放在[ ] 内)

[dalianmao@localhost ~]$ str='';str1=dalianmao
[dalianmao@localhost ~]$ [ -z "$str" -o -z "$str1" ];echo $?
0
[dalianmao@localhost ~]$ [ -z "$str" -a -z "$str1" ];echo $?
1

三、shell script 默认变量

(1)常用默认变量

名称说明
$0脚本名称
$1-9脚本执行的参数1到参数9
$?上一句脚本的返回值
$#脚本执行时,输入参数的个数
$@输入参数的具体内容(即所有参数的一个列表
$*输入的参数的具体内容(默认空格分隔,为一个单词

 

 

 

 

 

 

 

 

样例:注意$@和$*的区别哈!

[dalianmao@localhost ~]$ vi dalianmao_test.sh
[dalianmao@localhost ~]$ sh dalianmao_test.sh w1 w2 w3 w4 w5 w6 w7 w8 w9
filename: dalianmao_test.sh
参数$@的参数值为: w1 w2 w3 w4 w5 w6 w7 w8 w9
第1个参数是: w1
第2个参数是: w2
第3个参数是: w3
第4个参数是: w4
第5个参数是: w5
第6个参数是: w6
第7个参数是: w7
第8个参数是: w8
第9个参数是: w9
参数$*的参数值为:w1 w2 w3 w4 w5 w6 w7 w8 w9
第1个参数值为:w1 w2 w3 w4 w5 w6 w7 w8 w9
参数$1-9为: w1,w2,w3,w4,w5,w6,w7,w8,w9
[dalianmao@localhost ~]$ nl dalianmao_test.sh 
     1	#!/bin/bash
       
     2	#Progam:
     3	#     This program test particular parameters!
       
     4	#History:
     5	# 2018/11/12 dalianmao v1.0
       
     6	PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~bin
     7	export PATH
       
     8	echo "filename:" $0
       
     9	echo "参数\$@的参数值为:" $@
       
    10	n=1
       
    11	for temstr in "$@"
    12	do
    13		echo "第$n个参数是:" $temstr
    14		let n+=1
    15	done
       
    16	n=1
    17	echo "参数\$*的参数值为:"$*
    18	for temstr in "$*"
    19	do
    20		echo "第$n个参数值为:"$temstr
    21		let n+=1
    22	done
       
    23	echo "参数\$1-9为:" $1,$2,$3,$4,$5,$6,$7,$8,$9
       

(2)shift:默认变量偏移

后可接数字,默认为1。一次偏移就是默认拿掉前面几个参数变量。废话不多说直接看例子。

[dalianmao@localhost ~]$ sh sh_01.sh 1 2 3 4 5 6 7 8
sh_01 before shift: 1 2 3 4 5 6 7 8
sh_01 after shift: 3 4 5 6 7 8
sh_02 : 3 4 5 6 7 8
[dalianmao@localhost ~]$ nl sh_01.sh 
     1	#!/bin/bash
       
     2	#Program:
     3	#        test shift
       
       
     4	echo "sh_01 before shift:" $@
       
     5	shift 2
     6	echo "sh_01 after shift:" $@
       
     7	sh  sh_02.sh $@
[dalianmao@localhost ~]$ nl sh_02.sh 
     1	#!/bin/bash
       
       
       
       
     2	echo "sh_02 :" $@

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值