8

I recently asked this question about adding a shell script to Dash:

In 14.04 how do I run a bash script I wrote without opening a terminal?

It worked perfectly.

Follow-up question: How can I pass command-line arguments through Dash? Example: In this case, my dash command is named "Panel". The script panels my screen with 8 terminals. Here's an example of the usage:

panel             # Tile the screen with 8 terminal windows.
panel --left      # Tile the left side with 4 terminals
panel --right     # Tile the right side with 4 terminals

And so on. How can I pass arguments like --left or --right through Dash? Ideally I'd like to have this workflow:

  • Press the SUPER key
  • Type panel --left (for example)
  • Dash goes away and the left side is paneled.

Right now it runs the right script, but ignores --left.

Tips?

  • possible duplicate of Specify parameters for launcher program – Radu Rădeanu Oct 24 '14 at 18:16
  • 1
    @RaduRădeanu: Thanks, but this is definitely not a duplicate of that. That tells how to permanently affix options to a launcher program. I want to be able to use different options whenever I want (without having multiple versions of the launcher). – Sir Robert Oct 24 '14 at 20:29
  • 5
    Why not just use Alt+F2? That'd probably be easier.. Another option would be creating separate .desktop files.. – Seth Oct 29 '14 at 1:07
  • 1
    ... or one .desktop file with a quicklist from the launcher. – Jacob Vlijm Oct 29 '14 at 8:03
16
+25

The problem is that you cannot "run" a .desktop file from Dash with arguments, so the setup exactly like you have in mind is impossible I am afraid. However, assuming your script does take arguments, there are a few elegant alternative options, maybe even better:


  1. Save your script in ~/bin

    • remove the extension
    • make it executable
    • run it by pressing AltF2, type the command

      <scriptname> <argument> 
      

  1. Create a quicklist in the Unity launcher:

    (assuming you saved the script in ~/bin, made it executable and removed the extension as in 1.)

    enter image description here

    [Desktop Entry]
    Name=name_of_your_script_like_you_see_it_in_Dash
    Exec=<scriptname> <default_argument>
    Icon=/path/to/some/icon
    Type=Application
    
    Actions=Panel;Panel -left;Panel -right;
    
    [Desktop Action Panel]
    Name=Panel
    Exec=<scriptname> <default_argument>
    OnlyShowIn=Unity;
    
    [Desktop Action Panel -left]
    Name=Panel -left
    Exec=<scriptname> <argument_1>
    OnlyShowIn=Unity;
    
    [Desktop Action Panel -left]
    Name=Panel -right
    Exec=<scriptname> <argument_2>
    OnlyShowIn=Unity;
    

    Save it as panel.desktop in ~/.local/share/applications and drag it on to the launcher.


  1. Create three different keyboard shortcuts, for example Alt+<, Alt+^, Alt+> to run your script+arguments:

    "System Settings" > "Keyboard" > "Shortcuts" > "Custom Shortcuts"

    Click "+" to add your commands: <scriptname> <argument>


  1. Not the most obvious one, but exploring the options, it should be mentioned: you can call a (zenity) option list from Dash:

    enter image description here

    Type the first character of your option, press return and your script will run with the chosen argument.

    enter image description here

    Again assuming that you saved the script in ~/bin, made it executable and removed the language extension as in 1.:

    • Copy the script below into an empty file, save it as panel_options.sh, make it executable.

      #!/bin/bash
      
      test=$(zenity --list "1. Panel" "2. Panel -left" "3. Panel -right" --column="Panel options" --title="Panel")
      
      if [[ "$test" = "1. Panel"* ]]; then
          <scriptname> <default_argument>
      elif [[ "$test" = "2. Panel -left"* ]]; then
          <scriptname> <argument_1>
      elif [[ "$test" = "3. Panel -right"* ]]; then
          <scriptname> <argument_2>
      fi
      
    • Create the .desktop file from the code below. In the Icon= line, set the path to your icon, in the Exec= line the path to pane_options.sh, save it as panel.desktop in ~/.local/share/applicatios

      [Desktop Entry]
      Name=Panel
      Exec=/path/to/panel_options.sh
      Icon=/path/to/some/icon
      Type=Application
      StartupWMClass=Zenity
      
  • Very thorough answer. +1 – Glutanimate Oct 31 '14 at 20:13
  • Very thorough; excellent answer. Worth the bounty! Thanks! – Sir Robert Nov 9 '14 at 2:00
0

From your original question, it looks like the right thing to do would be to move the script to ~/bin/, which should be in your $PATH by default.
If you do this, you'll be able to run your script using AltF2 again. You will also be able to pass arguments to it like you do in a shell.

You can also add any other folder where you keep your scripts to your $PATH. To do this, open ~/.profile in the text editor of your choice:

# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

At the end, add a line (replacing <folder> with your folder) and save the file:

PATH="<folder>:$PATH"

For example, I add $HOME/.bin to my $PATH so my binaries don't clutter my home folder, but you could add any directory to your path just be sure to not add anything dangerous (e.g. world-writeable).
The changes will take effect on the next login.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.