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/]#
Community
  • 1
  • 1
kernel_klink
  • 1
  • 1
  • 2

2 Answers2

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.

Toberumono
  • 31
  • 3
  • 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. – kernel_klink Mar 01 '16 at 18:36
  • Hmm... can you echo the permissions on /tmp again? `ls -lAh /tmp` – Toberumono Mar 01 '16 at 18:54
  • drw-rw-rw- 12 root root 4096 Mar 1 13:15 tmp – kernel_klink Mar 01 '16 at 18:55
  • What operating system and shell are you using? – Toberumono Mar 01 '16 at 19:00
  • Red Hat Enterprise Linux Server release 5.11 (Tikanga). I am using bash. – kernel_klink Mar 02 '16 at 16:02
  • Sorry about the delay. I cannot acquire a copy of RHEL5 myself. Could you please run `bash --version` and let me know what version it returns? It is entirely possible that the version of bash does not support some part of the syntax. – Toberumono Mar 03 '16 at 21:16
  • GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu). – kernel_klink Mar 07 '16 at 15:11
  • Unfortunately, I'm not going to be able to test your script on even a remotely similar system - I cannot force Fedora to install a version of bash below 4.3. I'd recommend updating Bash to version 4.3.x as it contains numerous security and bugfixes and then testing the script again. – Toberumono Mar 08 '16 at 02:19
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..

AguThadeus
  • 310
  • 2
  • 10