Your brain is your body's most neglected organ...nurture it here.

You use Command Lines all the time and don't know it!!!

Date/Time Permalink: 08/01/06 11:10:18 pm
Category: HOWTOs and Guides

command lines

Man, the heat Linux catches over command lines. The flames! The trolls! The clueless screaming for help! And all the while, command lines are right under your nose the whole time! Yes, even on the most mousy, GUI-driven, WIMPy, point-n-drool interface, whether it be Macs or Microsofties, you're typing commands into prompts every day.

The box you type your search query into when you Google? Command line. Doing a Google search for "pots and pans" requires you to type "pots & pans" into the one-line command-line search box on Google's page, which it then turns into the BIGGER command line:

http://www.google.com/search?q=pots+%26+pans&start=0...

With a few dozen characters on the end of it I left off. In fact, you could get the same results by typing the whole search query as a string into your web-browser's address bar, which is *another* command-line in itself. For each address you type into the address bar and click "go" (or hit enter - a sure sign of a command line!), somewhere there's a server that had that address scrolling up a text screen.

"Oy Vey, you schlemiel!", I hear you say, "You can't compare a Bash shell to the Google search box, because it's so intuitive what the search box does, any schnook knows to type search words!" Good, then you also know that this string:

-inurl:htm -inurl:html intitle:"index of" "Last modified" mp3

will find you oodles of free mp3 downloads, since the Google command line is so blooming intuitive.

mo command lines

In fact, all Javascript text-entry fields on web pages, such as this story-submission-box on Digg.com, are command lines in GUI clothing. Like it or not, anywhere on any electronic device where you enter text is a command line - even your cell phone. Even when you're setting cooking time on your microwave.

See, the GUIs (Graphical User Interface) are what the geek elite call a "front end - sense 3" Many, many programs are just a front-end for another program behind them. We have to do it this way, because all computers understand is binary. We have to have assembly code as a front-end to manipulate binary strings, a high-level language as a front-end for assembly, and a GUI-window library or language to be a front-end for the high-level language. That way, when you type a string or click a button in a GUI, that GUI executes a text-program in the background, which will in turn (several levels later) translate your command into a binary string which the computer can then digest.

Here, let's take a command-line program and build a front-end for it. Every Linux command-line user knows the 'fortune' program quite well. So here's the code to make a GUI front-end for it, using the Tcl/Tk/wish, the "windowing shell":


#!/usr/bin/wish

# Tcl/Tk/wish dialog, prints fortunes. 

proc get_fortune {} {
   .txt insert 1.0 ""
   set quote [exec fortune]
   .txt insert end "$quote \n\n\n"
}

button .again -text "another" -command "get_fortune"
button .stop -text "quit" -command "exit"
frame .textarea
text .txt -width 80 -height 20 
     -yscrollcommand ".srl_y set"
     scrollbar .srl_y -command ".txt yview" -orient v
grid .txt -in .textarea -row 1 -column 1
grid .srl_y -in .textarea -row 1 -column 2 -sticky ns
pack .again
pack .stop
pack .textarea
get_fortune


Note the fifth line of this program - that's where it executes the fortune program in the background and puts its result in the variable 'quote' which it will then pipe into the text area. The result:

a fortune dialog

Note that our usage of buttons just executes commands in the background. The button 'another' executes the 'get_fortune' function and the button 'quit' simply executes wish's built-in 'exit' command. And you thought this would be hard! Well, believe it. Every GUI program works in exactly the same fashion. The buttons don't do diddly unless you tell it to link the execution of some process to that button.

UPDATE 5/7/07: Speaking of command lines and Google, here's a page with the Google command line itself!

Follow me on Twitter for an update every time this blog gets a post.
Stumble it Reddit this share on Facebook

suddenly the moon