Skip to content

Instantly share code, notes, and snippets.

@fromradio
Last active January 7, 2016 05:35
Show Gist options
  • Save fromradio/178077b1696928a9b070 to your computer and use it in GitHub Desktop.
Save fromradio/178077b1696928a9b070 to your computer and use it in GitHub Desktop.
Bash学习

Bash中的,,-,+和冒号结合可以得到不同的效果

  • var=${str:="abc"}的意思是当str为空或者不存在的时候将等号后面的值赋给str并且返回str的值
  • var=${str:-"abc"}的意思是当str为空的时候将等号后面的值返回,否则返回str的值
  • var=${str:-"abc"}的意思是当str不为空的时候将后面的值返回,否则返回空

Test命令

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment