Bash
中的,
,-
,+
和冒号结合可以得到不同的效果
var=${str:="abc"}
的意思是当str
为空或者不存在的时候将等号后面的值赋给str
并且返回str
的值var=${str:-"abc"}
的意思是当str
为空的时候将等号后面的值返回,否则返回str的值var=${str:-"abc"}
的意思是当str
不为空的时候将后面的值返回,否则返回空
Bash
中的,
,-
,+
和冒号结合可以得到不同的效果
var=${str:="abc"}
的意思是当str
为空或者不存在的时候将等号后面的值赋给str
并且返回str
的值var=${str:-"abc"}
的意思是当str
为空的时候将等号后面的值返回,否则返回str的值var=${str:-"abc"}
的意思是当str
不为空的时候将后面的值返回,否则返回空test
命令可以判断一个文件或文件夹的存在与否,具体参数如下
-e |
该档案名是否存在 |
---|---|
-f |
该档案名是否存在且为文件 |
-d |
该档案名是否存在且为目录 |
-b |
该档案名是否存在且为block device |
-S |
该档案名是否存在且为socket文件 |
-p |
该档案名是否存在且为FIFO档案 |
-L |
该档案名是否存在且为链接文件 |
一个文件夹如果为如下
-rw-r--r-- 1 ruimin staff 0 1 7 13:21 file
drwxr-xr-x 2 ruimin staff 68 1 7 13:21 files
其中file为一个空文件同时files问一个目录,这个时候我们可以对其做一个简单的测试
#!/bin/bash
# Func: test command 'test'
function call_test() {
test $* && echo "exists" || echo "not exists"
}
call_test -e file
call_test -e files
call_test -f file
call_test -f files
call_test -d file
call_test -d files
得到的结果当然是
exists
exists
exists
not exists
not exists
exists