Thursday, January 8, 2009

A bash script I wrote and love

I, like many Linux geeks before me, love to use the terminal for just about everything. However when I need to use programs with GUIs (Firefox, amarok, okular, etc), I wanted to execute them silently and to get them to return focus to the terminal. Normally if you type
$ firefox

You get a bunch of X and Gtk warnings during execution of Firefox, and you can't use that shell anymore. You have to open another tab in konsole. So, this script I wrote is called "sex" for "silent execute." Mainly for the brevity and the humor of having
$ sex dolphin

be a valid command. This script also allows parameters to be sent correctly to an executable, so
$ sex okular A\ document\ with\ spaces.pdf

opens "A document with spaces.pdf" in okular correctly.
CONCAT="${1}"; shift
for ARG in "$@"; do
eval "$CONCAT >> /dev/null 2>&1 &"

Simply put this in /usr/bin/sex (need root) and do a chmod +x /usr/bin/sex. I recommend adding
alias ff='sex firefox'

to your ~/.bashrc or your /etc/bash.bashrc too. It's a nice shortcut.

1 comment:

Peeter Joot said...

If you want a way to run something without it cluttering your current terminal you may be interested in the screen command. It is kind of like a bg/fg or nohup on steriods, allowing multiple windows within one terminal, and a detach reattach capability.

screen -q

gets you started. You can kill that terminal and

screen -ls

gets back all your screen windows.

For what you are describing you can create aliases that start stuff in their own screen windows such as:

# cat `which stv`
screen -t $1 ctsetview -cdtop $1

(something I use at work). I've been meaning to write a blog post on this great tool since I've been blogging about perl and shell stuff as well as my math and physics play... I managed to go 8 years working with Unix every day without knowing about screen, and can't imagine life without it anymore!