View Full Version : Entering su password in a script

23rd January 2008, 02:51 PM
my script needs to be ran as root. In the script i have su to change to root but how do i say what the password is, in the script...i.e. automatically enter the root password?

or at least once the root password has been entered, continue with the rest of the script.

23rd January 2008, 02:58 PM
Probably best to use 'sudo' instead and add the scripts and/or commands to the /etc/sudoers file.

See my example in this thread to give you an idea.

Sudo scripts (http://forums.fedoraforum.org/showthread.php?t=178850)

23rd January 2008, 03:03 PM
thanks a lot :)

23rd January 2008, 03:43 PM
Ok. I have a script which imply pings google (im lazy, i cant be bothered to open a terminaal then manually ping it). The script works fine, apart from when i add it to a custom application launcher, it does not work.

When i double click the script, it asks me how i want to open it (run in terminal, display, cancel run), even though i have checked the box to make it an executable and said i want it to open in the gnome terminal (Properties > Open with > Add > Use custom command. The custom command i use is gnome-terminal which is the correct command to run the gnome terminal.)

Does anybody know how to make a script open in the terminal, once you have added it to a custom launcher on the taskbar?

23rd January 2008, 03:59 PM
Did you read the whole example, including Step 3?

23rd January 2008, 04:04 PM
gnome-terminal -e or -x should work, doesn't seem to work for me.....

23rd January 2008, 04:08 PM
yes i did. I tried adding sudo at the start of the command but that did not do anything.

I thought of this but it does not run the ping, nor does it change the directory, it only opens the terminal.

cd /home/Antonio/documents/misc/scripts

23rd January 2008, 04:18 PM
you should look at these as batch files, it does step one, when it gets an exit or completesignal it moves on

you first tell it to run gnome-terminal, it does that but since it it still running it won't move down the ladder to the next commands

it might be better to implement it in your ping.sh and then call that in a terminal

23rd January 2008, 04:19 PM
If I read you right, you're trying to open terminal within the script? You don't need this - just the guts of the script (as if you were typing it in a terminal that's already open).

"sudo" is for scripts (or other commands) to be run as root. There must be a corresponding entry in /etc/sudoers.

Did you select type "Application in Terminal" in the launcher properties?

So, the basic steps:
1. Create the script, put it somewhere accessible, make it executable/SELinux-compliant
2. If run as root, add to sudoers
3. Create launcher to run the script (and only the script), add sudo only if a root script

23rd January 2008, 09:51 PM
thanks for the info people :)
gnome-terminal -e or -x does not work.

I have it working now though. Thanks for all your help guys :)

One more question....the icons are .svg files? does anybody know of any good sites for these? or where i can find them on my system apart from the limited supply in:

23rd January 2008, 10:08 PM
well firstly, post your solution so others can be helped from it to ;)

secondly, try Gimp for saving something in a different format, there is a chance it will allow for svg, if not, try yum search svg or something like that ;)

Mariano Suárez-
23rd January 2008, 10:12 PM
What do you mean by "gnome-terminal -e or -x does not work."?

23rd January 2008, 10:59 PM
sej7278 sugessted it

23rd January 2008, 11:33 PM
You can also use PNG files as icons .. possibly even others. There are other icons in /usr/share/pixmaps and /usr/share/icons (and its sub-folders). Probably other places as well. :)

Mariano Suárez-
23rd January 2008, 11:36 PM
I know. When you said that it does not work, what did you try?

For example, both

gnome-terminal --working-directory /tmp -x ping www.google.com


gnome-terminal -e "sh -c 'cd /tmp;ping www.google.com'"

work, AFAICT.

24th January 2008, 07:11 PM
The working code i have is:

[CODE]ping -i 0.2 -c 5 www.google.com
sleep 8/CODE]

which pings google.com 5 times at intervals of 0.2 seconds. I put the sleep for eight second part in since as soon as it has done its last ping, it closes without giving me a chance to see the ping report.

Il check out gimp and the directories sugessted by Bslagowski

24th January 2008, 07:18 PM
For running things as root or su ing you could try an expect script:


25th January 2008, 12:26 AM
insert a "pause" or ("PAUSE") or something like that

Mariano Suárez-
25th January 2008, 01:08 AM
Or create a gnome-terminal profile which does not close the window when the script is finished, and use it from the command line.

25th January 2008, 03:32 PM
how do you create a terminal that does not close....out of interest?
i like the fact it closes automatically since im lazy haha!

Mariano Suárez-
25th January 2008, 05:00 PM
You need to create a profile first:

Open gnome-terminal, go to File -> New Profile...
Choose a name, say, 'test', and base it on the Default profile, now click Create.
The Profile editor pops ups, go to the 'Title and Command' tab. At the bottom you'll see a combobox labeled `When command exits': choose the `Hold the terminal open'.
Close the Profile Editor window.

Now you can say:

gnome-terminal --window-with-profile=test -x ping -c 1 www.gnome.org

25th January 2008, 05:56 PM
Cheers people :D