views:

408

answers:

2

I need a script that takes a single command line argument that is a directory path. The script should check the argument to determine if it is in fact a directory. If it is a directory, then the script should change the protection mode of any subdirectories located in it to 600. If the argument is not a directory, then an appropriate message should be printed out.

I have

if [ -d $1 ] ; then


else

echo "This is not a directory"

fi

Basically I don't know what to put on the blank line. I was fooling around with chmod but my line seemed to want to change the inputted path and not just the subdirectories.

+8  A: 
ephemient
though personally, I'd skip printing an error message. The goal of the user is "after this command runs, all subdirectories should have a permission of 600". If you give it a file, that condition is met since a file has zero subdirectories.
Bryan Oakley
If I were designing it, I'd agree with you: the script would just be the `find` command and nothing else. But I'm following OP's structure, mostly, here.
ephemient
roger34 was not clear if `$1` should also be changed to 600. If not the command should start `find "$1" -mindepth 1 -type d ...`
Alexander Pogrebnyak
A: 
([ -d "$1" ] && find $1 -type d -mindepth 1 | xargs chmod 600 | true) || echo 'Not a directory'
nicerobot
3 problems with this solution: first, it fails if any of the paths involved have whitespace (solution: use `find "$1" ... -print0 | xargs -0 ...`, or find's `-exec` option); second, it prints "Not a directory" if $1 is a directory, but some error occurs with chmod (e.g. you don't have permissions to chmod some subdirectory) (solution: use `if ... then ... else ... fi` instead of ` finally, it exits with status 0 (success) even if there was a problem.
Gordon Davisson
Given that ephemient had already provided those alternatives, i was simply providing the one-line solution rather than duplicating solutions already provided. And it's easy enough to solve the chmod failure by piping the find to true, e.g. "... 600 | true)". Editing...
nicerobot

related questions