I have a simple script that checks if a file has write permission on it or No. If yes, then it allows the user to append some text data to end of file. If No, it kicks user back out.

I am following a youtube series for learning Shell scripting by ProgrammingKnowledge. Specifically Video 7.

Here is the script:

echo -e "Enter file name: \c"
read filename

if [ -f $filename ]

 if [ -w $filename ]
   echo "Type some text data. To quit, press CTRL + d: "
   cat >> $filename

   echo "File has no write permissions"


 echo "$filename is not found"


My script allows still allows the user to append to the file provided, even if it has no write permission. Other flags like -O or -s are working as expected. What is wrong here?

Running ls -l on available files:

-r--r--r-- 1 nobody nobody    0 Dec  6 12:25 Mane
-r-xr-xr-x 1 root   root    165 Dec  6 12:06 Ramy.txt
-r--r--r-- 1 root   root      0 Dec  6 12:24 Salah

The output of running the script with Debug turned on:

[root@Console2-4KMH2 scripts]# bash -x ./hello_FILE_TEST_OPERAT_APPEND_2_File.sh

+ echo -e 'Enter file name: \c'
Enter file name: + read filename
+ '[' -f Salah ']'
+ '[' -w Salah ']'
+ echo 'Type some text data. To quit, press CTRL + d: '
Type some text data. To quit, press CTRL + d:
+ cat

My question got resolved. Thanks everyone for all the edits on my rookie post. Stephen Harris in the comments section spotted that I am running the script as a root user, and highlighted that root will write to a file even if said file has no write permissions on it. I created a sudo user, re-ran the script and found it to behave as expected (not writing to file that have no write permissions)

  • Using hastebin.com/etupatibat.bash to attempt to replicate, I am unable to replicate attempting to write to a file without the write bit set. [[ -w file ]] correctly returns false for me on files to which I cannot write. Also, if [[ -f file ]]; then if [[ -w $file ]] is redundant; -w checks both for the file existing and being able to be written to. Please don't add details in comments; edit your question to include the additional information. – DopeGhoti Dec 6 '18 at 18:30
  • 4
    Are you running as root? That will have write access even on r only files – Stephen Harris Dec 6 '18 at 18:36
  • @DopeGhoti I tried running your code on my server and it did append to the file, even though it has no write permissions. – Romio Dec 6 '18 at 18:41
  • @StephenHarris Yes I am running as root and I think that is where the problem lies since DopeGhoti 's script also allows append of data to the file created within it – Romio Dec 6 '18 at 18:41
  • Thank you all for your comments. I will try to run it outside root and check if that fixes the issue. – Romio Dec 6 '18 at 18:53

The answer to my question came from the comments section from Stephen Harris. He spotted that I am running the script as root, which will write to any file whether there are write permissions or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.