• Tip Jar

Commanding the Command Line (Part 3)

It’s finally here! The third installment of Commanding the Command Line. And this time I have some great goodies for you like…

Fun With Scripting

Bash scripting could and probably should have a religion devoted to its glory. It’s powerful, simple for the most part, and if you know anything about using the command line you already know some about bash scripting. If you want to read a great book that is free and will teach you all you need to know and more about scripting with bash then read the Advanced Bash-Scripting Guide. So now you want a good use for all this knowledge? How about a signature script for your email client like Evolution or Thunderbird. Here’s a script that will change part of your signature while keeping some things the same each time. the html tags are necessary to make it display correctly in Evolution. You can right click the link and select "save link as" to download it. For Evolution save the script in ~/.evolution/signatures/random-signature.sh and set the permission to executable with:
chmod 700 ~/.evolution/signature/random-signature.sh
then start Evolution, and under Edit, Preferences, Composer Preferences select Signatures. Select the Add Script button, add it as random-signature, then select ~/.evolution/signatures/random-signature.sh as the signature script. Finally, to make it the default signature for your e-mail. From the Preferences page select your email account, press enter on edit then select random-signature from the drop-down box.
If you happen to be a fan of the CLI email client Alpine you can use the random signature by editing the file ~/.pinerc. Search for the line that reads signature-file= and put the path to the signature file:
Notice the | at the end of the file name so Alpine knows to execute it as a script.
With Alpine though, you do not need the div and pre tags, in fact they just show up in the message and ruin your otherwise beautiful signature. Bash to the rescue again. All you have to do is check for a variable that is set when you are in Gnome or your prefered desktop that isn’t set in the console. In Ubuntu and Vinux, using Gnome, this works:
if [[ -n $COLORTERM ]] ; then
echo "<div>-- </div>
echo "-- 
Thunderbird it is a little more tricky. You have to point Thunderbird to your signature.txt and change the above script to write its contents to the txt file every time it is ran. But how do you get the script to run and change the file thereby making it random? Simple, you schedule the signature script to run at intervals using…

Scheduling with Crontab

Cron jobs are great things. They run a command at specified times. It can be once a day, once a week, once a month, multiple times a day, month, etc. Each job takes 6 parameters. In order they are:

  1. minute from 0 to 59
  2. hour from 0 to 23
  3. Day of month varies depending on the month
  4. Month 1 through 12
  5. Day of Week 0 through 7 0 and 7 are both Sunday
  6. Command

To specify that the task should run for each occurance use the * in place of the number. To have something run at several different occurances but not every one use a , between each number like
used in the minute field would run the task at 5, 25, and 55 minutes in the hour or hours specified. To make it run the command for a specific period of time say every minute from 5 to 10 in the hour use a – like this:
The above example will run the task at 5, 6, 7, 8, 9, and 10 minutes after the specified hour.
Now, for some working examples:
0 6,18 * * * espeak "hello world"
This not so useful example will make your computer say hello world using the Espeak synthesizer at 6:00AM and 6:00PM every day. while the following:
0 12 * * * crontab -l > /home/USER/crontab.bak
will back up your crontab jobs to your home directory under the name crontab.bak every day at 12:00PM. So, finally, if we wanted to run the signature script which we have modified to write to the .txt file we use as our thunderbird signature we could do:
* * * * * /home/User/path/to/random-signature.sh
This cause the script to run every minute of every day changing your signature to a new random saying each time it happens.
Of course you need to change the signature slightly to write to the .txt file you use as your signature in Thunderbird. It’s not a big change, just edit these lines in the original script:
echo "<div>-- </div>" > /home/USER/path/to/signature.txt
echo "<pre>
</pre>" >> /home/USER/path/to/signature.txt
#end code
Another great use, if you have TTYtter set up, is to use Cron to wish your Twitter contacts happy birthday. this way you remember to tell them happy birthday and you get a reminder that it is their birthday all at the same time.


Aliases are shortcuts to commands. Basically you can say one thing and mean something much longer. You can even change how commands behave by aliasing the command. A great example of this is the date command. If you open terminal and type date you will get something like this:
Mon Jan 17 22:50:47 EST 2011
But by creating an alias for date you can get something you may find easier to read like this:
Monday, January 17, 2011
To set up these aliases open a file in your home directory called .bash_aliases:
gedit ~/.bash_aliases
and put aliases in the form:
alias shortcut="command"
Here is the date command aliased to the format I have above:
alias date="date +'%I:%M%p%n%A, %B %d, %Y'"
After you save the file your date alias will be nearly ready to go. the only thing you have to do after adding an alias before you can use it is source your .bashrc. So, type the command:
source ~/.bashrc
Now when you type the word date you should get the more readable format. If you want the previous version for some reason just prepend it with a \ character:
If it does not work you may want to check your .bashrc file for the correct lines to load the alias file:
gedit ~/.bashrc
If you do not find the following code then you may want to add it to the file:
if [ -f ~/.bash_aliases ] ; then
. ~/.bash_aliases
If you find the code but each line begins with a # character it means the code has been commented out. Code that is commented will not run, so all you have to do to fix it is remove the # from the 3 lines of code.

Bash One Liners

To further show off the power of bash here are a collection of one line commands that can do some really impressive things. This section will grow as I find or think of good examples. And now, some truly awesome code:
#Although I strongly believe everyone should use .ogg files I know that is not always possible.
#Convert files in current directory to mp3
#Using unrestricted version of sox:
for i in ./*.*;do sox "$i" "${i%.*}.mp3";done
#Using ffmpeg from medibuntu or compiled:
for i in ./*.*;do ffmpeg -i "$i" "${i%.*}.mp3";done
#Get current weather condition and 24 hour forecast:
zipcode="YourPostalCode";echo "$(elinks -dump "http://weather.yahooapis.com/forecastrss?p=${zipcode}&u=f" | grep -A 4 "Current Conditions:")"
#Run a script on the web, make sure you read the source first or trust the site:
curl http://trustedsite.com/bashscript.sh | bash

Video In The console

There are two ways to do this, you can use aaxine:
sudo apt-get install xine-console
and then:
aaxine path/to/video.avi
You may need to specify an audio device as in:
aaxine -A pulseaudio path/to/video.mpg
You can accomplish the same thing using libcaca and mplayer. to get libcaca:
sudo apt-get install libcaca0
then play a video with:
mplayer -af volnorm -vo caca path/to/video.avi

Commanding the Command Line (Part 1)

In the event that you decide to, or for some reason have to go completely command line for a while, you will likely be wondering how to do certain things. There are some things that have to be done on any computer to make the computer in question worth it’s plastic. So, here is part one of a discussion on doing these important tasks from the command line. Some of this may be covered elsewhere here but I am including it again for the sake of completeness. To get to the console from gnome, press control+alt+f1 through f6. You have six consoles to choose from, but when you wanna go back to gnome, it is control+alt+f7. If you want to use the console you will need a screen reader for it like Speakup. For information on installing Speakup in Ubuntu read "Speakup Revisited".
If you are completely new to the command line, you can use these two tutorials to get started with it quickly:
Into the Linux Command Line Interface (CLI) and Introduction to Command Line Interface (CLI) 2.

Multiple Terminals With Screen

I am pretty sure that Ubuntu comes with screen installed. I don’t ever remember installing it, but it could have sneaked in as a dependency for something else though I doubt it. If you don’t have screen, you need it. To launch it, type screen. You will have to press enter after it starts to get your prompt back. Screen makes it so you can have several terminals all at once. To create a new one, press control+a followed by c. You can have as many as you want, one for music, one for email, one for instant messaging, etc. To switch between terminals, press control+a followed by n for the next one or p for the previous one. To close a terminal that you no longer need, type exit. Another great feature that screen has is the ability to copy and paste. When you need to copy something, press control+a followed by either the escape key or [. Use the arrow keys to position the cursor on the first of the text you would like to copy and press space. Find the last part you want to copy and position the cursor on it with the arrow keys and press space again. Now, when you need to past the copied information press control+a followed by the ] key.
The use of a ~/.screenrc file can help Orca users to copy from the terminal. Installing xclip is helpful for this. To get it:
sudo apt-get install xclip
Here is the .screenrc file that I use:
#Begin .screenrc
startup_message off
defscrollback 1024
bind b eval "writebuf" 'exec !!! xclip -selection "clipboard" -i /tmp/screen-exchange'
termcapinfo xterm|xterms|xs|rxvt ti@:te@
#end .screenrc
If you want to download this file, enter the following command in terminal:
wget -O ~/.screenrc http://stormdragon.tk/scripts/screenrc
Using this .screenrc file you can press control+a b after following the above instructions to copy text to place the copied text into the clipboard. If you are using screen in the console where the X clipboard is not available, you can still easily get the copied information from the file:
To move up and down through the terminal’s buffer press shift+pageup and shift+pagedown. This is slightly different from gnome-terminal with out screen in which you press control+shift+pageup and control+shift+pagedown to move through the buffer.

Twitter From The Command Line

The program twidge is an excellent command line Twitter client. A lot of distros include it in the repositories. In Ubuntu, for example, you can get it by typing in terminal:
sudo apt-get install twidge
If your distro doesn’t include it you can download it from http://software.complete.org/software/projects/show/twidge.
After installing Twidge, you need to configure it. To do this, type:
twidge setup
There are only two questions and they should be very easy to answer. After twidge is configured, you can get the latest tweets very easily. First though, you should run:
twidge lsrecent -su
This gets the last 20 tweets and also sets a place marker so you won’t see them or anything before them if you do not wish to do so. After running this command, you can then get all new tweets by typing:
twidge lsrecent -asu
To post an update type twidge update and press enter. Type your update and press enter again, just remember not to go over the 140 character limit. For more detailed instructions on Twidge and its uses, type:
man twidge
You may also be interested in TTYtter.

Weather Information

I wrote an article on weather a while ago. There is a great program called weather-util for the console. To find out how to install and configure it, please read "…And Weather For All".

Command Line Music Player

I have tried several terminal based music players. None of them worked well with Speakup. I was about to lose hope in ever finding a good one to use with a screen reader when I heard of cmus. In Ubuntu, get it by typing:
sudo apt-get install cmus
Launch it with the command:
and close by typing:
After the first time you start and close the music player open the ~/.cmus/autosave file. Find the line that reads:
set softvol=false
Change it to true and save the file. softvol allows you to change the music players volume without changing volume for the whole computer. There are a lot of other settings that can be changed in this file including repeat and shuffle. To add music to the library use :add and the directory where the music is stored. You can use :a as a short cut if you prefer. To add all of the songs in ~/Music and its sub directories, for example, you would type:
:a ~/Music
Use – to decrease the volume and = to turn it up. Previous track is z, play is x, pause is c, stop is v, and next track is b. Use the up and down arrows to move through the tracks in your library and enter to play the currently selected song.. For more info on cmus, type man cmus.

Sox, the Swiss Army Knife of Sound Processing Utilities

Sox is one of my favorite command line programs. It can play audio, record, convert from one file type to another, and add effects to audio. Unfortunately it is not packaged with all of its powerful abilities enabled by default. The best way to deal with this little problem is to download and install it yourself. The only thing it is lacking is the ability to write mp3 files. It can play them, but by default, in Ubuntu and I assume other distros, it can not write mp3 files. So, if you do not care about this functionality, just do the normal sudo apt-get install sox. If you want to be able to convert to mp3, you will need to download it from http://sourceforge.net/projects/sox/ and install it. Before installing though, you will need to get some other libraries. In Ubuntu type:
sudo apt-get install libmp3lame0 libmp3lame-dev libsox-fmt-all
sudo apt-get build-dep sox
Next extract the sox files with the following command. The version may have changed after this writing, so be sure to use the current numbers in the file name:
tar xzvf sox-14.3.0.tar.gz
Change to the newly created directory with:
cd sox-14.3.0/
./configure --with-lame
sudo make install
For more information on the options that can be used when compiling sox read the included INSTALL file.
Sox can do so many different things that it would take a long time to write them all down here. Here are a few things to get you started though:
sox filename.wav filename.ogg
Convert filename from a wav to an ogg file. Don’t worry, it doesn’t delete the wav file. You can convert from any type to any other type of audio.
play filename.ogg
Plays a file it can have effects added and the volume can be adjusted as well.
rec filename.ogg
Creates a file and starts recording to it. for all of the incredible things sox can do, type man sox.

  • Tip Jar