0

I'm trying to write a shell script so that when I reinstall my Ubuntu again. I could just run script and retain all the packages. And I'm planning to pass password in form of argument.

My question here is how can i enter into sudo domain without manually entering password. As I'm also planning to design a UI where it can't access the terminal there.

Ex:

./recover.sh password
Braiam
  • 67,791
  • 32
  • 179
  • 269
Pavan
  • 119

2 Answers2

2

Why not just run the script as superuser?

sudo sh recover.sh
Ryan Doherty
  • 1,029
  • ya that works when you directly access from terminal. But in UI you can only give a command .. watever you do it shoud be in the command – Pavan Apr 30 '16 at 17:07
  • Look at this http://askubuntu.com/questions/496204/specify-password-for-sudo-in-script?rq=1 – Ryan Doherty Apr 30 '16 at 17:11
  • I have clearly mentioned that i want to pass password in form of argument. There is no such thing there – Pavan Apr 30 '16 at 17:22
  • That question is basically a duplicate of this and he explained why its a bad idea but he said "You can echo the password to sudo using the -s option." – Ryan Doherty Apr 30 '16 at 17:29
  • No you have misunderstood my question. Here my logic is not only limited to recovery, but it is also covering UI. And consider you are creating a UI and if u link a button with your script. Then you can only pass a command like ./install.sh password . But you cant expect the any output from terminal. – Pavan Apr 30 '16 at 17:34
  • Consider if i take your advice there and link command sudo ./install.sh you just can't expect the system to pop out a terminal to ask password. – Pavan Apr 30 '16 at 17:36
  • That' s why I'm concatenating the command with password and passing the command – Pavan Apr 30 '16 at 17:37
  • I did misunderstand your question. Sorry if i'm being a noob but wouldn't running the program with gksudo also make whatever command run by that program to also be run by the superuser. – Ryan Doherty Apr 30 '16 at 17:51
1

You can use...

echo password | sudo -S recover.sh

Password being your sudo password.

From sudo manpage..

-S, --stdin Write the prompt to the standard error and read the password from the standard input instead of using the terminal device.