I made some scripts containing some functions which by design needs sudo permission. I have added those path in .bashrc
for Linux
and .bash_profile
for MacOS
so that it can be called from anywhere.
But I do not want the user to type sudo
each time they want to call those script functions. Is there any way I can imply sudo
in a way that whenever these functions are called, terminal would assume its being called from root user?
I think I should just add sudo -i
at the beginning of the script or maybe each function? Or is there any other alternative way of implying sudo
? Also, would be great to know if you think it would be terrible or dangerous to imply sudo and if it is not recommended.
An example of dangerous-function
script that contains some functions which, I am trying to accomplish without specifying sudo
#!/bin/bash
start-one()
{
## do dangerous stuff with sudo
systemctl start dangerous.service
}
start-two()
{
systemctl start dangerous1.service
}
start-launchwizard()
{
systemctl start dangerous2.service
}
## Calling functions one by one...
"$@"
I dont want to call them by sudo dangerous-function start-one
I just want to call them with dangerous-function start-one
but still get the same result as the previous one.
$ sudo ./script.sh
?start_one() { sudo systemctl ...
?