Thursday, October 18, 2012

The Pioneers of UNIX

Steve Jobs? Steve Jobs didn't do jack. If you want to know who is responsible for the modern world you have to look at the people working at Bell Labs in the 1970s and 1980s. The people who created UNIX. It was from that invention that we have the modern world. UNIX led to Linux which led to Android. UNIX led to the BSD family of operating systems which led to Apple OSX. UNIX led to the C programming language in which most system-level software today is written. Ever wonder why URLs use forward slashes? It's because UNIX was instrumental in the creation of the Internet.

Below is a 27 minute promotional video produced in 1982 by Bell Labs, where we meet some of the giants on whose shoulders we all stand today. Included in the video are:


In addition, you'll get a good look at some computer hardware from the early 1980s which is around the time I was starting my transition from TRS-80s to IBM PCs.

Sunday, October 14, 2012

new_script Version 3

The most popular script on the old version of LinuxCommand.org was new_script, a bash shell script that created bash shell scripts. That is, a script template generator. A few months ago I rewrote new_script to modernize it and make better use of bash features, including arrays. The new version is less than half the length of the previous version and yet it does more.

Using new_script will save you a lot of work by creating a customized template incorporating a parser for your script's command-line options and arguments. The template also includes a number of useful housekeeping routines such as error and signal handlers. The generated script is fully functional (though it does nothing) and is ready for you to insert your program-specific logic.

Synopsis


new_script [-h|--help] [-q|--quiet] [-s|--root] [script]

Description


new_script will, by default, prompt the user for details about the script to be generated:

Enter script output filename >

Choose a name for the generated script file.

Enter purpose of script >

Enter a one-line description of the script.

Include GPL license header [y/n]? >

Entering "y" will insert a GPL license header into the comment block at the beginning of the script.

Does this script require superuser privileges [y/n]? >

Entering "y" will insert a routine that verifies the user has superuser privileges before allowing the script to execute.

Does this script support command-line options [y/n]? >

If you intend to support command-line options for your script, answer "y".

Option 1:
Enter option letter [a-z] (Enter to end) >

If command line options are desired, a series of prompts will appear. First, you will be asked for the short (single letter) form of the option. Enter a letter in the range of a-z to define the option or enter nothing to end the option input loop.

Description of option ------------------->

Enter a short description of the option. This will be used in the "help" message.

Enter long alternate name (optional) ---->

Enter a single-word long option name if desired. For example, if you have an option "h" for a help function, you can specify an alternate long form name for the option such as "help." When your script is executed, you can invoke the help function by including either -h or --help on the command-line.

Note that new_script always includes the "h" and "help" options automatically by default.

Enter option argument (if any) ---------->

If your option requires an argument, enter a single-word descriptor here. For example, if you were creating a script named my_script supporting both an input and an output file, you might have an option for each with their corresponding arguments such as:

my_script -i infile -o outfile

After you finish entering your option information, new_script will generate your custom script template.

Your script will contain a handy error handling routine called error_exit, which is used like this:

error_exit error_message

where error_message is a string containing a descriptive error message. The error_exit function outputs the error_message string to standard error, calls the clean_up function to perform any necessary housekeeping, then exits with an exit status of 1. Here is an example:

error_exit "Something bad happened!"

You can see other examples within the new_script code itself.

Options


The new_script program supports the following command-line options:

-h, --help
Display a help message and exit.

-q, --quiet
Quiet mode. Do not prompt user for script information and output default script to standard output.

-s, --root
Require superuser privileges for execution of generated script.

You may also include the desired file name for the generated script on the command-line after any specified options.

Installation


You can find the script here. To install it, just highlight the text and copy it into a text editor and save the resulting file. If you're feeling clever, you can directly write it to a file via the command-line. Highlight the text as before, open a terminal and enter the following command (assuming you have a bin directory in your home directory, otherwise adjust as needed):

cat > ~/bin/new_script

Paste the script into the terminal by clicking the middle mouse button and type Ctrl-d.

Once you have written the script, give it execute permissions like so:

chmod +x ~/bin/new_script 

Thursday, October 4, 2012

My Raspberry Pi Adventure


Last Christmas, my friend +Norman Robinson gave me a BeagleBoard-xM computer to play with. The BeagleBoard is a small, single-board, ARM-based computer. As I started to work with it I was impressed with its relative performance and low-power consumption. However, due to its price (approx. $150), I didn't think it represented a tremendous value. After all, I only paid $199 for my HP Mini netbook at my local computer store and the netbook comes with a case and a keyboard!

I had been hearing a lot about the Raspberry Pi computer which appeared to be very similar to the BeagleBoard but only costs $35. That price, being clearly in impulse-buy territory, appealed to my computer buying impulses.


First Discovery: They're hard to get.



The Raspberry Pi computer is the brainchild of a group of British computer science educators. They set out to solve a problem:
The idea behind a tiny and cheap computer for kids came in 2006, when Eben Upton and his colleagues at the University of Cambridge’s Computer Laboratory, including Rob Mullins, Jack Lang and Alan Mycroft, became concerned about the year-on-year decline in the numbers and skills levels of the A Level students applying to read Computer Science in each academic year. From a situation in the 1990s where most of the kids applying were coming to interview as experienced hobbyist programmers, the landscape in the 2000s was very different; a typical applicant might only have done a little web design. 
Something had changed the way kids were interacting with computers. A number of problems were identified: the colonisation of the ICT curriculum with lessons on using Word and Excel, or writing webpages; the end of the dot-com boom; and the rise of the home PC and games console to replace the Amigas, BBC Micros, Spectrum ZX and Commodore 64 machines that people of an earlier generation learned to program on.
As they had modest objectives and resources, they only planned to produce a few thousand boards, however when they announced the computer and its price, they were flooded with hundreds of thousands of orders. Needless to say, this has led to some supply problems. It was many months before the boards became generally available in the U.S.. Even now, you should expect a long wait to receive a board.

I ordered mine from Allied Electronics and waited about 12 weeks for delivery.

If you're willing to pay more, there are scalpers out there (like this guy on Amazon) who will happily sell you a board albeit at a greatly inflated price.

Second Discovery: You'll need accessories.


For $35 all you get is a bare board, nothing else, not even documentation. That's available on-line. I ordered a case, power supply, and cables from Adafruit Industries which offers an extensive array of Raspberry Pi accessories. You'll also need a 4GB (or larger) SD card to act as its boot disk.

One interesting cable I bought from Adafruit was the USB TTL to Serial Cable. With this cable, you can both power the Raspberry Pi and interact with it via a serial terminal program on your computer. I installed GtkTerm on my Ubuntu box and was able to work with the board while I waited for my back-ordered power supply.

The Raspberry Pi is powered with 5 volts over a USB cable so many mobile phone chargers will work. My Nexus 7 charger worked fine for testing purposes.

Third Discovery: It ain't no slouch.


So how good is a $35 computer? Surprisingly good. In terms of computing horsepower, you can think of the Raspberry Pi as a late 1990s-era desktop PC, but with a better graphics card. The CPU performance is roughly equivalent to a 300 MHz Pentium II.

The board comes equipped with 256 MB of RAM, 2 USB ports, an Ethernet port, HDMI video out, RCA composite video out, and a speaker port. Notice that there is no VGA output. It's really designed to be hooked up to a digital TV or monitor via HDMI. If you have a monitor that supports DVI (as I do), you can use a HDMI-to-DVI cable. I only used a monitor with the board once and it worked fine. Be aware that the HDMI is not bi-directional, meaning that the board cannot detect the capabilities of the monitor it's attached to. You may need to adjust some configuration files to get the video the correct size on your monitor. Another limitation of the Raspberry Pi is that it does not have a real-time clock. It relies on a network connection and Network Time Protocol to keep accurate time.

Fourth Discovery: USB is an issue.


Due to its limited power supply, both my BeagleBoard and Raspberry Pi cannot meet the official specification for USB power output. This means you should not try to power devices via USB. My mouse and keyboard worked okay, but forget about USB powered hard disks and the like. Either use an externally powered device or use a good quality powered USB hub.

Fifth Discovery: There's lots of software.


I set up my Raspberry Pi as a headless media server. I downloaded and installed Raspbian, the version of Debian 7 for the Raspberry Pi. It is officially supported by the Raspberry Pi Foundation and you can download it from their site. To install the image, use dd to write the image file on to the SD card. After it's done, insert the SD card into the Raspberry Pi, attach a monitor and keyboard, plug in the power supply and it boots up. There are a few setup questions (start with setting the correct keyboard layout for your locale) and you're done.

Most every application that's available for Debian (more than 30,000) is also available for the Raspberry Pi. I installed Samba (Windows style file server), Apache (web server), OpenSSH-server (secure shell server), byobu (terminal screen multiplexer), apcupsd (UPS monitor), and the Logitech Media Server.

It is also possible to build an all-text Linux workstation as I describe in my blog series.

I attached a 200 GB 3.5 inch external hard drive and added a line to /etc/fstab to explicitly mount it since Raspian, in its standard form, does not automatically mount drives.  

I wrote a bash shell function for my .bashrc file (on the Raspberry Pi) to provide a quick snapshot of the system's health:


status() {
        { echo -e "\nuptime:"
        uptime
        echo -e "\ndisk:"
        df -h 2> /dev/null
echo -e "\nblock devices:"
blkid
        echo -e "\nmemory:"
        free -m
        echo -e "\nrsync_backup.log:"
        tail /var/log/rsync_backup.log
        echo -e "\nsyslog:"
        tail /var/log/syslog
        } | less
}


The rsync_backup.log file is created by a script of mine that performs nightly backups of the system.

Here is my Raspberry Pi in its final production configuration:


On the left we see the power cable, on the right, USB cables to the APC UPS and external hard drive, and 10 Base-T Ethernet.

Final Discovery: I like it.


The Raspberry Pi, along with my BeagleBoard, have replaced two old desktop machines that acted as servers on my home network. My office is now eerily quiet (and much cooler!).