Often when writing for the bash shell, one needs to test if a file (or Directory) exists (or doesn't exist) and take appropriate action. Most common amongst these test are...
-e
- file exists, -f
- file is a regular file (not a directory or device file), -s
- file is not zero size, -d
- file is a directory, -r
- file has read permission, -w
- file has write, or -x
execute permission (for the user running the test)
This is easily confirmed as demonstrated on this user-writable directory....
#/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
? no -f for you ?
? YES SIR -w is fine ?
? YES SIR -d is fine ?
My question, although seemingly obvious, and unlikely to be impossible - is how to simply combine these tests, without having to perform them separately for each condition... Unfortunately...
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 `]'
? no -wd for you ?
? no -w | -d for you ?
? no [ -w [ -d .. ]] for you ?
? no -w && -d for you ?
What am I missing here?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…