2

I am writing a script that downloads packages to a specific folder.

However, I want to make it possible for all users to download packages to that folder and use any packages installed there. How do I do that?

I want to check/change the permission for the /usr/local/src folder. I don't know how I've to use the if/else statement properly. In text it'll look like (I guess): if stat/permission of src folder isn't 777 then chmod to 777

13
  • 1
    Welcome to U&L, Hudhud! Why exactly do you need everything in each src folder to have readable, writable, and executable permissions for every user? Also feel free the peruse Advanced Bash Scripting, Bash Hackers, and Google for information on how to write code in Bash. Commented Oct 11, 2016 at 20:39
  • 1
    Thank you very much :) I missed something, I want the folder /usr/local/src to have the permission 777 :)
    – Hudhud
    Commented Oct 11, 2016 at 20:47
  • So you want like a script that will periodically kick-off and check the permissions of /usr/local/src and make sure that everybody can read, write, and execute inside of that directory? Commented Oct 11, 2016 at 20:55
  • yes, exactly :)
    – Hudhud
    Commented Oct 11, 2016 at 20:56
  • What you'll want to look into is writing a cronjob that will run the script in question. How often/periodically do you need/want that script to check on /usr/local/src's permissions? Commented Oct 11, 2016 at 20:57

1 Answer 1

2

You can use stat -c "%a" /usr/local/src to get the full permissions. But you should consider 1777 instead of 777.

So something like

if [ "$(stat -c '%a' /usr/local/src)" == "777" ]
then
  # something
else
  # something else
fi

In answer to your other question, if the permissions are already 777 then there will be no effect.

EDIT: corrected typos. @Alexej Magura why would I use double brackets? As far as I'm aware that would turn it into an arithmetic expression ..

8
  • Why would you recommend that? and is there a way to say like: if the permissions aren't 777 (or 1777) then set to 777 (1777)?
    – Hudhud
    Commented Oct 11, 2016 at 21:06
  • 1
    Didn't read your edited version, but why do you recommend 1777?
    – Hudhud
    Commented Oct 11, 2016 at 21:07
  • 1
    1777 means that other users a cannot delete user b's files and vice versa, where as 777 is more open
    – Unbeliever
    Commented Oct 11, 2016 at 21:08
  • Please use double brackets, single brackets are only really necessary when you're running dash or /bin/sh and they support less advanced syntax. Commented Oct 11, 2016 at 21:11
  • Dear, when I do it this way it keeps thinking that the permissions aren't 777 even though they're. I've tried to check manually with stat it says 0777
    – Hudhud
    Commented Oct 12, 2016 at 16:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .