【发布时间】:2011-10-18 11:54:19
【问题描述】:
在编写 bash shell 时,通常需要测试文件(或目录)是否存在(或不存在)并采取适当的措施。这些测试中最常见的是...
-e - 文件存在,-f - 文件是常规文件(不是目录或设备文件),-s - 文件不是零大小,-d - 文件是目录,-r - 文件有读权限,-w - 文件有写权限,或者-x 执行权限(针对运行测试的用户)
这很容易确认,如此用户可写目录....
#/bin/bash
if [ -f "/Library/Application Support" ]; then
echo 'YES SIR -f is fine'
else echo 'no -f for you'
fi
if [ -w "/Library/Application Support" ]; then
echo 'YES SIR -w is fine'
else echo 'no -w for you'
fi
if [ -d "/Library/Application Support" ]; then
echo 'YES SIR -d is fine'
else echo 'no -d for you'
fi
➝ 不适合你的 -f ✓
➝ YES SIR -w 很好 ✓
➝ YES SIR -d 很好 ✓
我的问题,虽然看起来很明显,而且不太可能是不可能的 - 是如何简单地组合这些测试,而不必为每个条件单独执行它们......不幸的是......
if [ -wd "/Library/Application Support" ]
▶ -wd: unary operator expected
if [ -w | -d "/Library/Application Support" ]
▶ [: missing `]'
▶ -d: command not found
if [ -w [ -d "/Library.... ]] & if [ -w && -d "/Library.... ]
▶ [: missing `]'
➝ 不适合你 ✖
➝ 不-w | -d 为你 ✖
➝ 没有 [ -w [ -d .. ]] 给你 ✖
➝ no -w && -d for you ✖
我在这里缺少什么?
【问题讨论】:
标签: bash testing scripting syntax