官术网_书友最值得收藏!

Using file attributes with conditional logic

Earlier in this book, we touched upon various tests for strings, numbers, and variables. Using a similar concept built into Bash, we can also use various attributes to test against files and directories. This extends upon the introduction conditional logic to perform tests on files. Does an example exist? Is it a directory? and so on.

For a moment, though, couldn't we just use the results from executing and checking the return code? Absolutely! This is another method you can use, especially if you are using a version of Bash that supports all of Bashes features. It is just another way to "skin the rabbit".

Let's start off first with some of the common flags, which return true if:

  • -e: The file exists
  • -f: This is a regular file and not a directory or device file
  • -s: The file is not empty or zero in size
  • -d: This is a directory
  • -r: This has read permissions
  • -w: This has write permissions
  • -x:This has execute permissions
  • -O: This is the owner of the file the current user
  • -G: This executes the user if they have the same group as yours
  • f1 (- nt, -ot, -eff2: Refers to if f1 is newer than f2, older than f2, or are hard-linked to the same file
There is more information available on file test operations in the GNU Bash manual:  https://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html.
主站蜘蛛池模板: 淮北市| 军事| 张北县| 富锦市| 德安县| 年辖:市辖区| 大名县| 保亭| 松潘县| 抚松县| 永顺县| 清原| 满洲里市| 温州市| 元朗区| 延津县| 大埔区| 三原县| 公主岭市| 内丘县| 嘉定区| 桓仁| 正定县| 保德县| 高邑县| 九龙城区| 阳新县| 西青区| 常宁市| 阿尔山市| 盐城市| 青铜峡市| 长宁县| 普宁市| 额济纳旗| 东丽区| 乌拉特中旗| 沅江市| 长海县| 斗六市| 铜山县|