Score:0

How to put sudo operations (apt-get) on a shell script?

cn flag

I am doing several operations installing packages etc and one of those are

sudo apt-get update && apt-get install -y curl

obviously it asks my password.

Now, I would like to put all the operations I did in a script so that other people can do it too.

But how do I put this operation in a script if it requires that the user enters the sudo password?

(before in a dockerfile what I did was to mark first the USER as root and RUN the command, but I am not using docker this time)

guiverc avatar
cn flag
FYI: You do realize `apt-get install` won't work without `sudo` (*possibly a typo in your question*)
Will avatar
id flag
This isn't elegant and it's not secure, but you can write: echo "your-password" | sudo -S apt-get update && echo "your-password" | sudo -S apt-get upgrade -y && echo "your-password" | sudo -S <any other commands you want to run as SU> -S with sudo makes it read the password from standard input. I haven't put this an an answer as it means your password is there in plain text in the script, and it'll save your password in the shell history. I don't think that's a great idea - there must be a more secure way (eg password read from a secure file?).
muru avatar
us flag
"the sudo password" ... There isn't a specific "sudo password". Either it's asking for your user's password (the default) or root's password (if you configured `targetpw` or `rootpw`. If they don't have sudo access, put the commands in a script, give them sudo access to that script and tell them to run that script with sudo.
gb flag
Please tell us what you exactly trying to accomplish, i.e. why other people need to do that, too? Is it on the same machine or are you sharing that script with i.e. your coworkers?
Score:0
tn flag

A similar question was answered here .

To summerise, you should not be using sudo inside a shell script, rather run the whole script with sudo privilage.

sudo ./script.sh

Otherwise, if you still want, you can do,

sudo -u username command
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.