0

I'm trying to make a Linux shell script with find one or more director(y)ies which are not writeable by a specific user (envadmin).

I think I have to combine these two parts:

# Part 1 - this not complete right because I need if not true that the dir is not writeable and the owner is envadmin:
find -type d -maxdepth 1 ! -writable -user envadmin # I know, this is not working by '! - writable'

# Part 2 - at the if I should need part 1 I think:
for directory in *
do 
    if [ $directory -user envadmin ] ; then
        if [ ! -w $directory ] ; then
            echo "De directory: $directory is from envadmin but not writeable"
        fi
    fi
done

I have tried more but I hope that I have explained it right what I want and I hope that someone can put me in the right direction.

jww
  • 97,681
  • 90
  • 411
  • 885
user2363969
  • 105
  • 1
  • 10

2 Answers2

1

You actually don't need any complex scripting for that:

find -maxdepth 1 -type d -user eventadmin -and -not -writable

This should give you the desired output.

tink
  • 14,342
  • 4
  • 46
  • 50
-1

Yesterday evening I have found the solution which is working for me I think:

for directory in  `cd /tmp;find . type -d -maxdepth 1 -user envadmin`; do 
    if [ ! -w $directory ] ; then
        echo "De directory: $directory is from envadmin but not writeable"
        exit 1;
    fi
done
user2363969
  • 105
  • 1
  • 10
  • Puffff, good script, but: 1. Backticks \` are [discouraged](https://mywiki.wooledge.org/BashFAQ/082) and [this](https://wiki-dev.bash-hackers.org/scripting/obsolete). Please use `$(...)` instead. 2. `cd /tmp; find` makes no sense here, just `find /tmp`. 3. `for i in $(find)` is a [common](https://stackoverflow.com/questions/9612090/how-to-loop-through-file-names-returned-by-find) antipattern and will not work with entires with spaces, newlines, tabs etc. Use a [while read](http://mywiki.wooledge.org/BashFAQ/001) at least. 4. The `$directory` inside `[` is not quoted, again it will fail for... – KamilCuk Dec 10 '19 at 11:12
  • directories containing spaces, tabs and whitespaces (which is I guess here not possible, because they get splitted at `for director in ...`). – KamilCuk Dec 10 '19 at 11:15