检查文件以及文件是否可读写
我正在尝试编写一个脚本来查找保存到我桌面的某个.txt文件。 我希望脚本能够检查此文件是否存在,然后检查它是否可读写。
任何提示?
您无需检查它是否存在,对读写权限的检查就足够了:
从help test
,选择相关测试:
-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.
所以你可以尝试:
FILE="/path/to/some/file" if [[ -r $FILE && -w $FILE ]] then # do stuff else # file is either not readable or writable or both fi