Reputation: 19
myscript.sh is
#/!bin/sh
mkdir -p $1
cp -p a.txt ./$1
cp b.txt /usr
If I invoke it with sudo ./myscript.sh, the directory $1 is owned by root, so the user can't modify a.txt (which is a problem). I could change the script to
#/!bin/sh
mkdir -p $1
cp -p a.txt ./$1
sudo cp b.txt /usr
and invoke with just ./myscript.sh but I get the impression this is bad practice. How to proceed in the general case, where I don't know the user, so chown doesn't help?
Upvotes: 2
Views: 1321
Reputation: 252
You should add this line
chmod ug+rw a.txt
With this one, the user will have read/write permissions on 'a.txt'.
Upvotes: 1
Reputation: 21517
SUDO_USER
environment variable is set by sudo
to the name of the user who invoked sudo
. You can use it for chown
.
As of bad practices, you don't check anything and your $1
argument substitution is broken for filenames with spaces. If that much doesn't matter, should you care for the rest?
Upvotes: 1