I am running scriptA.sh as root. Inside the script I need to echo data into FileA. FileA is owned by user1. FileA is located under /usr/local and has the following permissions:

-rw-r----- 1 user1 group 

FileA permissions cannot be modified. Right now, when I run scriptA I get permission denied on FileA. Is there anyway to get around this inside the script?

Psuedo code from scriptA that writes to FileA:

function update_FileA()
      if [ condition ] then
         echo "XYZ data" >> FileA
         echo "ABC data" >> FileA

Update: Tried a few things. I tried to vi FileA as user1 and as root and I was able to successfully edit. Its only when trying to edit the file through the script write access is denied.

  • echo foo | sudo -u user1 tee FileA, perhaps. How exactly are you executing the script and how exactly are you echoing data to the file? – muru Jun 21 '18 at 5:18
  • Root has permission to read all files, can you show the contents of scriptA.sh – Arushix Jun 21 '18 at 5:22
  • @muru, tried ur suggestion, still seeing permission denied. I am logged in as root and then script is executed from the terminal-> ./scriptA.sh – sotn Jun 21 '18 at 5:35
  • 3
    Then it's an selinux issue. – roaima Jun 21 '18 at 8:10
  • 2
    Or perhaps the file has the "immutable" attribute set? (check with the lsattr command) Or maybe the system has detected some filesystem corruption and switched to read-only mode? – telcoM Jun 21 '18 at 14:47

protected by Community Nov 14 '18 at 11:05

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Browse other questions tagged or ask your own question.