Help- writing to dir with no write permissions (script)

hello,

I have a question regarding the below script.. I just simply trying to write to a file in directory that does not have write permissions but this is failing .. it still writes to it regardless ..

Many thanks in advance..

#!/bin/bash
#check if dir exists, if so write to it 



echo " please enter the dir path"
read dir

if [ "$dir" = "" ]
        then
                echo " Please enter a dir name"

elif [ -d $dir -a -w $dir ]

        then
                echo " File hello.out is created"
                echo "wow!!!" > $dir/hello.out
        else
                echo " incorrect"
                exit
fi

Pls post

  • long directory listing of $dir's parent dir (e.g. ls -l .. )
  • your user name, optionally anonymized (e.g. id )
  • xtrace execution log of your script (run with set -vx )

to enable us to help you.

hi RudiC..

please see below .. this the dir that i tried to write too " no_write_perm"

total 40
8 drwxr-xr-x 29 root root 4096 Feb  3 00:55 ..
8 -rw-r--r--  1 root root    7 Feb  3 01:02 hello.out
8 d---------  2 root root 4096 Feb  3 01:03 test
8 drwxrwxrwt  4 root root 4096 Feb  3 06:09 .
8 d---------  2 root root 4096 Feb  3 06:09 no_write_perm

This is only 1/3 of what I asked you to post. What user are you? Should you be root, no wonder you can write to that dir. Should you not be rot, we need the xtrace log.

i am user root yes.. would that be the reason? sorry-- noob on unix. but why then we have to chmod every script to run it?

also .. the dir has no write permissions ..does root user overwrite this?

i have ran the trace.. shows the following.

[root@localhost no_write_perm]# sh directorycheck.sh 
sh directorycheck.sh 
+ sh directorycheck.sh
 please enter the dir path
/var/tmp/no_write_perm
 File hello.out is created
echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"
++ echo -ne '\033]0;root@localhost:/var/tmp/no_write_perm'
[root@localhost no_write_perm]# 

Being root you can read and write anything. Why you need execute permission on a file, I guess is an additional system security measure. As I'm not sure, let's wait for additional comments...