• 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:
signature-file=/home/USER/path/to/random-signature.sh|
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>
<pre>
$signature
</pre>"
else
echo "-- 
$signature"
fi
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
5,25,55
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:
5-10
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:
#code
echo "<div>-- </div>" > /home/USER/path/to/signature.txt
echo "<pre>
$signature
</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

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:
10:51PM
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:
\date
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
fi
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

Bookmark the permalink.

Comments are closed

  • Tip Jar