mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Updated Template (markdown)
28
Template.md
28
Template.md
@@ -1,35 +1,17 @@
|
|||||||
# To run commands as another user, use su -c or sudo.
|
# (Message goes here)
|
||||||
|
|
||||||
### Problematic code:
|
### Problematic code:
|
||||||
|
|
||||||
./configure
|
(Simple example of problematic code)
|
||||||
make
|
|
||||||
su root
|
|
||||||
make install
|
|
||||||
|
|
||||||
### Correct code:
|
### Correct code:
|
||||||
|
|
||||||
./configure
|
(Simple example of above code, only fixed)
|
||||||
make
|
|
||||||
sudo make install # or su -c 'make install' root
|
|
||||||
|
|
||||||
### Rationale:
|
### Rationale:
|
||||||
|
|
||||||
`su` doesn't actually switch user. It starts a brand new shell, running as another user. You can't put `su foo` in a script to make the following command run as user `foo`.
|
(An explanation of why the code is problematic and how the correct code is an improvement)
|
||||||
|
|
||||||
Use `sudo -u username cmd` or `su -c cmd username` instead.
|
|
||||||
|
|
||||||
### Contraindications
|
### Contraindications
|
||||||
|
|
||||||
If you want to present the user with a root shell or when you're redirecting input, you can ignore this message:
|
(Cases where the user may choose to ignore this warning, if any.)
|
||||||
|
|
||||||
until mycommand
|
|
||||||
do
|
|
||||||
echo "Failed. You will now get a root shell. Fix problem and exit to retry."
|
|
||||||
su
|
|
||||||
done
|
|
||||||
|
|
||||||
or
|
|
||||||
|
|
||||||
exec < mycommands
|
|
||||||
su foo
|
|
Reference in New Issue
Block a user