0

My goal is to check if the the execution bit is not set for a directory.

I changed the permission of /tmp so that the execution bit is off.

root$: chmod 666 /tmp

root$: ls -l /
    ....
    .....    
    drw-rw-rw-  12 root root  4096 Feb 29 15:17 tmp

In my bash script, I have tried the following without success:

if [ ! -x /tmp ]; then 
......

I have experimented with all the suggestions at the following link, but the only different syntax approach does not work for me either:

if [[ ! -x /tmp] ; then

check if a file is executable

These work as expected for regular files, but not for any directory, but I don't know why. Any ideas?

Update #2

I wrote a mini bash script with only the code suggested in a comment below.

Results:

[root@mc/]# cat tst.sh
#!/bin/bash
if [ ! -x /tmp ]; then echo 'not executable!'; fi
exit

[root@mc/]# ./tst.sh
[root@mc/]#

2 Answers 2

1

All of the code that you have provided in your question is correct (I just finished testing it myself). It stands to reason, therefore, that something else in the script that is failing. If you could, try running this simplified snippet: if [ ! -x /tmp ]; then echo 'not executable!'; fi

As a quick side note, the "executable" flag for directories in Unix systems does not actually mean "executable". It is actually the way that the directory is marked as searchable. While I'm not sure if this will help with the problem you are working on, it is an interesting usage of existing fields.

Sign up to request clarification or add additional context in comments.

8 Comments

check out update #2 in my post. I wrote a mini bash script with only your simplified snippet and then ran it. Nothing was output.
Hmm... can you echo the permissions on /tmp again? ls -lAh /tmp
drw-rw-rw- 12 root root 4096 Mar 1 13:15 tmp
What operating system and shell are you using?
Red Hat Enterprise Linux Server release 5.11 (Tikanga). I am using bash.
|
0

You can perhaps use the find command to single out any directory without the executable bit

notex=$(find . -type d -maxdepth 1 -perm 666)

I think that may help..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.