LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   BASH:find out if volume "foo"/folder "goo" can be written to (http://www.linuxquestions.org/questions/programming-9/bash-find-out-if-volume-foo-folder-goo-can-be-written-to-829006/)

SilversleevesX 08-28-2010 09:17 AM

BASH:find out if volume "foo"/folder "goo" can be written to
 
My habit is to write BASH scripts that export output to ASCII text files, typically in the same directory as the files they're evaluating. It occurred to me recently that, with read-only volumes, or in directories where my permissions may be "less than 776," that could throw an unfriendly error or two in my direction. To avoid it/work around it, I'd like to know the best way(s) of determining the write-ability of a folder before I run a script on it OR have the script 'ping' for write status/privileges in the directory it's in and dump its output there or elsewhere, as appropriate.

Regarding volumes:
What command would one use, and how would one parse the output?
I have a suspicion that stat might be useful, and I know from perusing the man page that there are %-letter tags that will "pull out" attribute data for a file or folder. Just now I did a 'stat' command, and I noticed in two fields there were zeros. Is this a practical "rule of thumb" when determining whether or not a volume is write-able?

Regarding the permissions of folders:
I'm familiar with the different options to 'ls', but here again I think 'stat' may be the better approach, owing to the attribute "print out" options I mentioned above. Here I suppose it's a question of "which one gives up the goods with less hassle," or "which one can be counted on to 'tell the truth' about folder WXYZ right off the bat." stat I can parse; I have zero practise at pulling ls's teeth, so to speak.

Any advice on this matter would be much appreciated.

BZT

David the H. 08-28-2010 10:01 AM

You can use -w in the test command.
Code:

if [ -w dirname ]; then echo "writable"; fi
http://www.tldp.org/LDP/abs/html/fto.html

grail 08-28-2010 10:03 AM

Quote:

Regarding volumes:
What command would one use, and how would one parse the output?
Sorry but to do what?

Quote:

I'd like to know the best way(s) of determining the write-ability of a folder
Whenever I need this information the first place to go is man test. There you will see an answer is -w:
Code:

[[ -w dir ]] && echo yes it is
Remembering that linux see both files and directories as files.


All times are GMT -5. The time now is 01:25 PM.