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
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...