在 Shell Script 检查档案及目录是否存跟 Perl 很相似, 都是透过 -e 及 -d 在 if 里面判断, 写法如下:
检查目录是否存在
|
1 2 3 4 5 6 7 8 9 |
#!/bin/sh if [ -d "/path/to/dir" ]; then # 目录 /path/to/dir 存在 echo "Directory /path/to/dir exists." else # 目录 /path/to/dir 不存在 echo "Directory /path/to/dir does not exists." fi |
检查档案是否存在
|
1 2 3 4 5 6 7 8 9 |
#!/bin/sh if [ -f "/path/to/dir/filename" ]; then # 档案 /path/to/dir/filename 存在 echo "File /path/to/dir/filename exists." else # 档案 /path/to/dir/filename 不存在 echo "File /path/to/dir/filename does not exists." fi |