Test multiple file conditions in one swoop (BASH)?

前端 未结 6 1496
滥情空心
滥情空心 2020-12-13 00:22

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

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-13 00:41

    You can use logical operators to multiple conditions, e.g. -a for AND:

    MYFILE=/tmp/data.bin
    if [ -f "$MYFILE"  -a  -r "$MYFILE"  -a  -w "$MYFILE" ]; then
        #do stuff
    fi
    unset MYFILE
    

提交回复
热议问题