Bash it up!

Published Date
01 - May - 2007
| Last Updated
01 - May - 2007
 
Bash it up!
Bash-an acronym for Bourne-again Shell, is a Unix shell written for the GNU Project and is the default shell for Linux and Mac OS X. The beauty of Bash scripts is that they let you accomplish a group of tasks quickly and without much intervention from the user-much like batch files in Windows. The syntax is similar to structured programming languages like C, and will be easier to understand for people with prior knowledge of any such programming language. We assume that you are familiar with GNU/Linux commands and with the fundamental concepts of programming.

Saving And Running Your Scripts
You can use any text editor to write your Bash scripts-any Unix-based OS you work on should have Vim installed. Save the scripts with the .sh extension anywhere on your computer. After saving it, open the terminal (as root, if necessary) and go to the folder where you saved the file. To run your script, type bash yourscriptname.sh in the terminal prompt.

A Very Simple Script
The first thing that anyone new to programming learns is how to print "Hello World" on the screen:
echo Hello World
This is a one line script where the action performed by the script is to print "Hello World" on the computer screen.

Variables, Functions and If

Variables can be used in Bash as in any other programming language. It is simpler here, since there are no data types to worry about-a variable can be a number, character or string. They need not be declared as in structured languages; they're created as soon as you assign a value to them.

Variables are of two types: global, which are recognised throughout the program, and local, whose recognition is limited to a function. If you have a block of code that you may want to execute a number of times, you write a function instead of the entire block all over again, saving time and resources in the bargain. Take a look at the example below:
#!/bin/bash
HELLO=Hello
function hello
{
local HELLO=World
echo $HELLO
}
echo $HELLO
hello
echo $HELLO

In the second line, the global variable HELLO is assigned a string value "Hello". The third line declares a function called "hello", which runs the code between the braces every time the function is called. Here a local variable HELLO is assigned a string value "World" which is valid only during the execution of the function "hello". Notice that we have used the common name HELLO for both Local and Global variables just to show that this is possible. In the sixth line, the function declaration is complete. Line 7 displays "Hello" on the screen; in the next line, the function "hello" is called which prints "World" on the screen. The ninth line, however, prints "Hello" again. The string "World" is stored in the local variable HELLO but does not overwrite the value of the global variable of the same name.

You can add comments to your script to make it easier others to understand-any line starting with a "#" is interpreted as a comment and is not processed.

The "if" command lets you decide the direction in which the script should proceed depending on conditions you set. The syntax is as below:
if condition
then
#condition is true
execute all commands up to else statement
#The else block is optional
else
 #condition is not true
 execute all commands up to fi
fi

Some Basic Tasks
1. Renaming multiple files
The following script renames all the files with extension .txt to .bak.
#Use the "for" structure to run through all files
for f in *.txt;
do
mv "$f" "${f%.txt}.bak";
done
Similarly, to remove the prefix "prefx" from a group of files, the following script is used.
for f in prefx-*;
do
mv "$f" "${f#prefx-}";
done
Here ${f#prefx-} removes the prefix "prefx" from the filename.

2. Backing up files modified within the past 24 hours
Here, we will back up the files created in the current directory within the past 24 hours in a ".tar.gz" file.
#!/bin/bash
FILEBACKUP=backup-$(date %m-%d-%Y)
incrback=${1:-$ FILEBACKUP}
tar cvf-`find . -mtime -1 -type f -print` > $incrback.tar
gzip $incrback.tar
echo "The directory $PWD has been backed up in archive file "$incrback.tar.gz"."
Line 2 of this script embeds the date in the backup filename. Line 3 introduces the string variable incrback as the filename of the archive. Line 4 tar condenses the current directory into a single tar file, while the fifth line gzip compresses the resultant tar file to the final tar.gz output file. The last line prints to the console that the current directory has been backed up to the archive file.

3. Mount and unmount drives as you please
You may wish to mount and/or unmount your drives swiftly and at one go:
# mounting the drives
# This mounts a partition of your hard drive
mount /dev/hda2 /hd -t ext3
# This mounts the floppy
mount /dev/fd0 /mnt/floppy
# This mounts the CD-ROM
mount /dev/cdrom /mnt/cdrom

And for unmounting:
# unmounting the drives
umount /dev/hda2
umount /dev/fd0
umount /dev/cdrom

Ready to Bash?
We hope that you've warmed up to the concept of Bash scripting by now. This is barely the surface-Bash scripting becomes more interesting and powerful as you delve deeper, and practice is the best way to take your Bash programming skills to new levels.  


Beyond All Recognition

Start with foobar2000's Notepad-like interface and work your way up to a media player that's right up there with the rest

Foobar2000 has been around for a while, but people tend to be turned off by the interface, which looks a lot like Notepadwith a few buttons. Here, we'll give you a gist of the possibilities and things to do to reduce your Winamp withdrawal symptoms.

One step closer to Audiophilia
One of things you might hear from users of Foobar2000 is that it's far better than Winamp when it comes to quality of music. One of the reasons for these claims could be the 32-bit output depth ceiling-which, however also needs to be supported by your hardware. Higher bit depths mean more information can be recorded and played back. 32-bit is still pretty far-fetched even now, and most cards like the Audigy can only do upto 24 bits. Winamp is limited to a maximum of 24 bits as well but foobar2000 works with 32-bit float point, which along with dithering and noise shaping can give the tiny advantage that hardcore audiophiles want. To do this, go to File > Preferences and click on Output under Playback. You can alter the output data format to the bit depth supported by your hardware.

Note: Your sound card needs to support the bit depths you choose to use.

Equalizer
You also have an 18-band equalizer at your disposal, as opposed to Winamp's 10-band equalizer, which lets you get more accurate audio results. You can access the equalizer by going to File > Preferences > Playback > DSP Manager. Add Equalizer to Active DSPs and click on Save All. Click on Configure to open the Equalizer. Once added, you can access the equalizer from the main window by clicking on View > Equalizer.

Kernel Streaming
Kernel Streaming skips the Windows mixer and is supposed to give better sound results than DirectSound. It also means that any volume control change in Windows will not alter foobar's volume.

The Kernel Streaming plugin does not come with Foobar2000 any more. You can download it from the Optional components section on Foobar2000's site-www.foobar2000.org. To install this plugin, download the zip files and unzip them into the components directory: foobar2000components. To enable Kernel Streaming, click on File > Preferences > Playback > Output. Click on the Output Device dropdown and choose KS: "Your Sound device". Click on Save All.

Enhancing The Interface
The biggest drawback of Foobar2000 is the dull interface. There are plenty of interface-enhancing plug-ins to give foobar2000 a complete makeover-one of the most famous is ColumnsUI.

ColumnsUI
Get ColumnsUI from http://yuo.be/ columns.php. The installation of the plugin is as before-unzip the file into foobar2000's components directory, restart foobar2000 and you'll be prompted to choose between the default foobar2000 interface and ColumnsUI. Volume control can be used by clicking on the current volume on the bottom-right of the window.
You can get readymade themes from the foobar2000 forums.

ReplayGain
Normalization is a process by evening out the differences in volume in either single tracks or albums. ReplayGain is a feature to do just this. ReplayGain is built into Foobar. You can alter the ReplayGain of tracks by right clicking on a particular track and clicking on ReplayGain > Edit ReplayGain Info (advanced). Here you can manually enter the amount of amplification you want. Normalization for multiple tracks or albums can be done in a similar manner.

Keyboard Warrior
It's best to use the keyboard shortcuts to quickly change volume or track or start playing a random track. There is a default set of shortcuts already, and you can change or add more in File > Preferences > General > Keyboard Shortcuts. Create as many global shortcuts as possible-they will let you do tasks even while foobar2000 is minimised to the system tray.

There is a lot more to Foobar-a good place to start finding out is on the Foobar forums-http://forums.foobar 2000.org. You'll find plenty of plugins and customised layouts to try out and modify for yourself. You can spend plenty of hours having fun with on Foobar-all you'll need is some patience and a willingness to experiment. 




Team DigitTeam Digit

All of us are better than one of us.