The basic format of the If judgment is as follows:
If condition then #then可省略 processing end
1. Determine if the file exists
#!/usr/bin/env rubyif file.exist? ("/etc/hosts") print "OK" end
Plus Else
#!/usr/bin/env rubyif file.exist? ("/etc/hosts") print "OK" else print "error,file not exist" end
If the program is running in the background, you will need to change the print to write log
#!/usr/bin/env rubyrequire ' logger ' logger = logger.new ('/tmp/test.log ', ' daily ') Logger.sev_threshold = logger::D Ebugif file.exist? ("/etc/hosts") logger.debug "OK" logger.closeelse logger.debug "error,file not Exist" Logger.closeend
2, judge whether the document can be written
If file.writable? ("/etc/hosts") {print "OK"}
3, judge whether the document is readable
If file.readable? ("/etc/hosts")
4. Determine if the file is executable
If file.executable? ("/etc/hosts")
5. Determine file size
If File.size? ("/etc/hosts") #文件大小非零为true
If File.zero? ("/etc/hosts") #文件大小为零位true
This article from the "Focus on Linux operation and Maintenance" blog, reproduced please contact the author!
If Judge of Ruby