How to use sudo in a script

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Robin2

How to use sudo in a script

Post by Robin2 »

When I do the following at a terminal it asks for the password and opens the file manager as ROOT.

Code: Select all

sudo rox-filer
But when I put it in a shell-script nothing happens. Almost everything Google turns up is about how to get around the need for the password. But I want my script to prompt for the password and then run the rox-filer program as root.

What am I missing?

...R
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
austin.texas
Level 20
Level 20
Posts: 12003
Joined: Tue Nov 17, 2009 3:57 pm
Location: at /home

Re: How to use sudo in a script

Post by austin.texas »

Anytime you run a graphical program (rox-filer) use gksudo - not sudo
Mint 18.2 Cinnamon, Quad core AMD A8-3870 with Radeon HD Graphics 6550D, 8GB DDR3, Ralink RT2561/RT61 802.11g PCI
Linux Linx 2018
deepakdeshp
Level 20
Level 20
Posts: 12341
Joined: Sun Aug 09, 2015 10:00 am

Re: How to use sudo in a script

Post by deepakdeshp »

Code: Select all

 echo mypassword | sudo -S command  
If you want the padsword to be read from the script.
Or

Code: Select all

 sudo -S command  
if you want to input the password from the terminal.

For gui commands use gksudo instead of sudo as advised by Austin.Texas
If I have helped you solve a problem, please add [SOLVED] to your first post title, it helps other users looking for help.
Regards,
Deepak

Mint 21.1 Cinnamon 64 bit with AMD A6 / 8GB
Mint 21.1 Cinnamon AMD Ryzen3500U/8gb
Robin2

Re: How to use sudo in a script

Post by Robin2 »

Thanks very much.

When I put

Code: Select all

gksudo rox-filer
in my script it works fine - asks for the password and then open rox-filer as root

...R
Locked

Return to “Scripts & Bash”