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.

muru
  • 197,895
  • 55
  • 485
  • 740
Mersault
  • 287

1 Answers1

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?.

Radu Rădeanu
  • 169,590
  • 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 '14 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. – comrademike Jan 14 '14 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 '14 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. – Radu Rădeanu Jan 14 '14 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 '14 at 17:24
  • @mo This is not a normal behavior. See, for example, this post: pass password to su/sudo/ssh; maybe you messed something. – Radu Rădeanu Jan 14 '14 at 18:00
  • I got it now...I accidentaly lost -S (before my command)...password works without quotes. Thank you! Now I have one more question - this script automates process of copying files from my sportwatch's usb to computer. If you use this usb in windows, it authomaticaly gets files from usb (when you insert usb) and uploads them to online site. In ubuntu I have to manualy start the script and upload files to internet site. Do you think those two steps could be automated as well (that inserting usb would trigger script and that script would include sending transfered file to internet)? – Mersault Jan 14 '14 at 21:17
  • @mo I think this is another discussion. Anyway, see Autorun a script after I plugged or unplugged a USB device. If you don't get your answer, don't hesitate to ask a new question. And if this answered your question, you may mark this answer as accepted, by clicking the green check mark next to it. That will indicate that your problem is solved. – Radu Rădeanu Jan 14 '14 at 21:41