- Bash Cookbook
- Ron Brash Ganesh Naik
- 238字
- 2021-07-23 19:17:39
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, -ef) f2: 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.
推薦閱讀
- JavaScript百煉成仙
- Kali Linux Web Penetration Testing Cookbook
- Blockly創意趣味編程
- Learning Salesforce Einstein
- iOS開發實戰:從入門到上架App Store(第2版) (移動開發叢書)
- Scratch趣味編程:陪孩子像搭積木一樣學編程
- Mastering AWS Security
- 寫給程序員的Python教程
- 3ds Max印象 電視欄目包裝動畫與特效制作
- IoT Projects with Bluetooth Low Energy
- 游戲設計的底層邏輯
- Hands-On ROS for Robotics Programming
- SOA Patterns with BizTalk Server 2013 and Microsoft Azure(Second Edition)
- 計算機輔助設計與繪圖技術(AutoCAD 2014教程)(第三版)
- 熱處理常見缺陷分析與解決方案