Câu trả lời:
Bạn không cần kiểm tra nếu nó tồn tại, kiểm tra quyền đọc và ghi là đủ:
Từ help test
, một lựa chọn các bài kiểm tra có liên quan:
-a FILE True if file exists.
-e FILE True if file exists.
-f FILE True if file exists and is a regular file.
-r FILE True if file is readable by you.
-s FILE True if file exists and is not empty.
-w FILE True if the file is writable by you.
Vì vậy, bạn có thể thử:
FILE="/path/to/some/file"
if [[ -r $FILE && -w $FILE ]]
then
# do stuff
else
# file is either not readable or writable or both
fi
if [[ -r $FILE ]] && [[ -w $FILE ]]
thay thế if [[ -r $FILE && -w $FILE ]]
?
&&
v.v. được phép vào[[
-r
và-w
toán tử