3

Is it possible to create desktop shortcut, that would do as follows:

  1. open Terminal
  2. change directory
  3. make a command inside that directory
  4. type password (sudo)

I tried with Terminal profiles, but I didn't find option for 2. and 4.

1 Answer 1

2

First you need a script like one from the following example:

#!/bin/bash

commands () {
    cd / # change directory, in this case to `/`
    echo "your_password" | sudo -S mkdir -p "test" # execute a command as root for which the password is automatically filled
    $SHELL # keep the terminal open after the previous commands are executed
}

export -f commands

gnome-terminal -e "bash -c 'commands'"

Don't forget to make the script executable!

Then, if you are not familiar about how to execute a script using a desktop shortcut, see Execute sh script from *.desktop file?.

8
  • Thank you, but I think I need some more help. Do I type this script in terminal or make a document (with .sh ending)? Do I start with #!/bin/bash? Do I type password with or without ""? What about "test" in the echo line - it's marked red, do i have to insert something else there? Same question with "bash -c 'commands'"? Where do I type my actual command, that will be executed?
    – Mersault
    Jan 14, 2014 at 13:05
  • 1
    Use a text editor. Include '#/bin/bash' - won't work without! No password necessary - you created the file. Don't worry about the red. You need to insert your own code where it says 'sudo -S mkdir -p "test"'. Read the links for how to make it executable and how to run from desktop. Jan 14, 2014 at 13:24
  • I almost made it:)...it executes the command, but I still have to type my root password...where in a script I put my password? I tried after echo (with or without ") and it doesn't work...
    – Mersault
    Jan 14, 2014 at 16:11
  • @mo echo "your_password" - replace your_password with your password; keep quotes and your password shouldn't contain another quotes. This command: mkdir -p "test" will create a directory called test in / if there doesn't exist another one with the same name; it's just an example and you can change the command as you wish. Jan 14, 2014 at 16:50
  • I did that, but it doesn't work...it starts Terminal, but it wants my sudo password...when I type it, then the command is executed.
    – Mersault
    Jan 14, 2014 at 17:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .