Run scipt like sudo with Gksudo

470 Views Asked by At

I'm new in linux. I'm try to mount and umount .iso with nemo-script. I put the script /home/user/.gnome2/nemo-scripts/. My problem is, with gksudo I only run 1 command like su. Who I can run all this script like su?

#!/bin/bash

gksudo -k /bin/echo "got r00t?"
BNAME=$(basename "$NEMO_SCRIPT_SELECTED_FILE_PATHS")

sudo mkdir "/media/$BNAME"

zenity --info --title "ISO Mounter" --text "$BNAME e $NEMO_SCRIPT_SELECTED_FILE_PATHS"

if sudo mount -o loop -t iso9660 $NEMO_SCRIPT_SELECTED_FILE_PATHS "/media/$BNAME"
then
    if zenity --question --title "ISO Mounter" --text "$BNAME Successfully Mounted. Open Volume?"
    then
        nemo /media/$BNAME --no-desktop
    fi
    exit 0
else
    sudo rmdir "/media/$BNAME"
    zenity --error --title "ISO Mounter" --text "Cannot mount $BNAME!"
    exit 1
fi

Thanks!!

1

There are 1 best solutions below

0
On

There are a couple of ways to do this. In your script above, you can execute the entire script with root permission if you call the scripts itself with su -c script or sudo script. Before doing this, edit your script and remove the calls to gksudo and sudo. Then at the top of your script add a test to insure the script is run with an effective root UID. E.g.:

#!/bin/bash

if test "$UID" -eq 0 ; then
    printf "Running script as root\n"
else
    printf "error, this script can only be run by root\n"
    exit 1;
fi

Then just make sure you run your script as sudo scriptname or su -c scriptname. Additionally, make sure you have the execute bit set of your script file permissions. You can do it simply with chmod 0775 scriptname which will give owner and group read-write-execute permissions and give world read-execute permissions. If you don't want it world executable, then just chmod 0774.

If you don't set execute permission on your script, you will get a Permission denied or command not found error. You can execute the script by specifying the shell in your sudo or su call, e.g. sudo bash scriptname or su -c "bash scriptname", but it is preferred to make the script executable.