Topic: adding terminal command to keybinding

I want to add a shortcut to do the following
$ su
$ apt-get update
$apt-get upgrade

with a shortcut (super + s)

I know the keybind, but I dont know if I need to make a script in order to dothis?

thanks!

Re: adding terminal command to keybinding

when you separate terminal commands with a ; they will be done in order. so

sudo apt-get update; sudo apt-get upgrade

what would also work is to add in a simple check for whether the previous command executed successfully. which is better imo.

sudo apt-get update && sudo apt-get upgrade

afaik this is possible in the terminal as well as in a bash script.

making a script for this isn't more difficult than this either. you just do

gedit ~/myupdatescript

add this:

#!/bin/bash
# my update script
sudo apt-get update && sudo apt-get upgrade

save that, then

chmod +x ~/myupdatescript

(i might have some typos or other wrong stuff there, all this from the top of my head, never copy stuff mindlessly smile)

Re: adding terminal command to keybinding

I see what you mean, but how does the sudo actually work? won't the script stop after asking for my password?

and how do I set it up in my commands? don't I need to put the script in /usr/bin? (no, I have never been able to call my scripts from throughout the system)

thank you!!!

Re: adding terminal command to keybinding

jms53 wrote:

I see what you mean, but how does the sudo actually work? won't the script stop after asking for my password?

and how do I set it up in my commands? don't I need to put the script in /usr/bin? (no, I have never been able to call my scripts from throughout the system)

thank you!!!

No, the script won't stop after asking for your password; "sudo <dosomething>" means "temporarily give me superuser privileges and do <dosomething>, and is preferred over "su" wherever possible.

You could put the script in /usr/bin, but many of us have created a local folder for scripts; mine is ~/.scripts (read that as "/home/bsh/.scripts" where "bsh" is my username) then edit ~/.bashrc  so that the PATH points to the scripts folder.

Add this to the end of ~/ .bashrc:

PATH=$PATH:~/.scripts
export PATH

this appends the ~/.scripts folder at the end of the PATH so no matter where I am the script will run.

In rhowaldt's example, he put the script in his home folder, so you'd add

PATH=$PATH:~
export PATH

to .bashrc to add the home folder to the path.

Don't forget to make the scripts executable, as rhowaldt mentioned.

Last edited by 2ManyDogs (2012-01-26 01:21:35)

(Un)official #! mascot | Man pages are your friends. | It's all in a .config file somewhere.

Re: adding terminal command to keybinding

ok, so I have the script and it works when I call it from the terminal...

now how do I set it up to the keybind?

This is what I actually have:

<keybind key="S-u">
        <action name="update">
            <command>update</command>
        </action>   

What am I missing? do I need to call up a terminal?

Re: adding terminal command to keybinding

Sorry, I should have been more clear about the PATH. When you add the PATH to .bashrc, it only adds your scripts folder when you run a terminal. You need to specify the full path in rc.xml. For example, I have a script that changes the wallpaper to a random image. This code calls it from rc.xml, bound to Shift-u:

<keybind key="S-u">
    <action name="Execute">
        <command>~/.scripts/random-wallpaper</command>
      </action>
</keybind>

I also think you need to use "Execute" for the action name in rc.xml (rather than "update"). And I'd personally bind it to the windows key ("W-u"), rather than shift -- I think there's less chance of typing that sequence at the wrong time.

Last edited by 2ManyDogs (2012-01-26 04:28:30)

(Un)official #! mascot | Man pages are your friends. | It's all in a .config file somewhere.

Re: adding terminal command to keybinding

jms53 wrote:

ok, so I have the script and it works when I call it from the terminal...

now how do I set it up to the keybind?

This is what I actually have:

<keybind key="S-u">
        <action name="update">
            <command>update</command>
        </action>   

What am I missing? do I need to call up a terminal?

I think you have to add a terminator command like this:

    <keybind key="S-u">
      <action name="Execute">
        <command>terminator -e update</command>
      </action>
    </keybind>

Already mentioned by 2ManyDogs: Don't change action name to update. It's not just a name, it's a value for openbox.
More info about openbox keybindings:
https://urukrama.wordpress.com/openbox-guide/#Key_mouse
http://openbox.org/wiki/Help:Bindings

Last edited by Tunafish (2012-01-26 10:50:40)

TOSHIBA C660-22V #! R20120207 AMD64 WHEEZY
Privacy & Security on #! - Application List

Re: adding terminal command to keybinding

thanks 2manydogs, when i wrote that post last night i knew i was forgetting something, and thet PATH-stuff was it. i think you and Tunafish cleared all that up now.

Re: adding terminal command to keybinding

Tunafish wrote:

I think you have to add a terminator command like this:

    <keybind key="S-u">
      <action name="Execute">
        <command>terminator -e update</command>
      </action>
    </keybind>

Thanks Tunafish.

I tried this and I still needed to specify the full path to the script.

<command>terminator -e ~/.scripts/update</command>

     
You don't always need to run scripts in a terminal (my random wallpaper script works fine without one, and other non-interactive tasks can be run without a terminal, using gksudo if you need su privileges). You do need a terminal for this script though, as it's nice to see what's happening and you may need to interact to answer questions from apt-get (like "do you want to continue").

Last edited by 2ManyDogs (2012-01-26 13:19:24)

(Un)official #! mascot | Man pages are your friends. | It's all in a .config file somewhere.

Re: adding terminal command to keybinding

2ManyDogs wrote:

I tried this and I still needed to specify the full path to the script.

Probably because ~/.scripts/ is not in your path.

TOSHIBA C660-22V #! R20120207 AMD64 WHEEZY
Privacy & Security on #! - Application List

Re: adding terminal command to keybinding

^ Indeed.  I usually put scripts in ~/bin (although I suppose this is improper, because they're interpreted scripts and not compiled binaries...but have a look at all the scripts in /bin. roll)

while ( ! ( succeed = try() ) );

Re: adding terminal command to keybinding

pvsage wrote:

^ Indeed.  I usually put scripts in ~/bin

Me too.

It might still be necessary to put the whole path in the keybinding anyway.

TOSHIBA C660-22V #! R20120207 AMD64 WHEEZY
Privacy & Security on #! - Application List

Re: adding terminal command to keybinding

I keep my scripts in a .scripts folder on a partition I share between a testing install and a sid install on the same machine, so I only need to maintain them in one place. I also keep them above /home because I back that up regularly and don't have to fish them all out of /bin if I need to reinstall. And when I installed a different distro on another machine it was easy to move the entire .scripts folder to that machine. I also don't like to modify anything not on /home unless absolutely necessary.

As always, there are many ways to accomplish the same end in Linux. smile

(Un)official #! mascot | Man pages are your friends. | It's all in a .config file somewhere.

Re: adding terminal command to keybinding

Allright, so now it works (after restarting openbox), but the terminal doesn't stay...

this is the action line: <command>terminator -e ./home/juan/.scripts/update</command>

Thanks!

Re: adding terminal command to keybinding

does this work if you type it in a terminal

terminator -e ./home/juan/.scripts/update

or do you need to take out the dot

terminator -e /home/juan/.scripts/update

?

(Un)official #! mascot | Man pages are your friends. | It's all in a .config file somewhere.

Re: adding terminal command to keybinding

2ManyDogs wrote:

does this work if you type it in a terminal

terminator -e ./home/juan/.scripts/update

or do you need to take out the dot

terminator -e /home/juan/.scripts/update

?

Rhetorical that one.
jms53 says it works with the dot. So it works smile

TOSHIBA C660-22V #! R20120207 AMD64 WHEEZY
Privacy & Security on #! - Application List

Re: adding terminal command to keybinding

Sort of solved:
Added a menu item that executes

 gksudo apt-get update && gksudo apt-get upgrade.

Perhaps not the most subtle way to do this, but it works, so, I'm happy. big_smile