1833792 Members
2530 Online
110063 Solutions
New Discussion

umask in scripts.

 
SOLVED
Go to solution
brian_31
Super Advisor

umask in scripts.

Hi:

How do you handle umask in scripts? Any tips welcome.

Thanks
Brian.
6 REPLIES 6
Sridhar Bhaskarla
Honored Contributor

Re: umask in scripts.

Brian,

Can you be more specific?.

I don't pay much attention to umask as the script will run in a new shell. If I need the umask from the script to the current shell, then I will dot (.) in the script.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Michael Tully
Honored Contributor

Re: umask in scripts.

umask can be handled just like any other environment variable.

e.g.

umask 022

If I wanted to change the default mask of root for example I will change the umask in the script. If I don't I leave it alone and it will pick up the default of the user running it.
Anyone for a Mutiny ?
John Poff
Honored Contributor

Re: umask in scripts.

Hi,

Do you mean, how do you set it in a script? Just do a 'umask 027' and that should set it for the duration of your script.

JP

James R. Ferguson
Acclaimed Contributor
Solution

Re: umask in scripts.

Hi Brian:

As required, set it the way you want in your script:

# umask 027

For directories, you could do something like:

# mkdir -m 750 /tmp/mydir

Note in the above that a 'mkdir' yields the same permissions with either form when 'umask' as set as shown.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: umask in scripts.

I handle it something like this:

SAVEMASK=$(umask 007)
echo "This is a test" > myfile
# restore to original value for following commands
umask ${SAVEMASK} > /dev/null
If it ain't broke, I can fix that.

Re: umask in scripts.

If a script must create files with specific permissions, do not rely on the inherited umask value being acceptable. In particular, take care with scripts that you intend the root user to run. I've seen root umasks of 0, leaving files and directories wide open.