Bash Shell Programming in Linux

| Friday, December 25, 2009

Bash what?
Okay, I grant that this page might represent a leap from the familiar to the alien without much warning. Here are some explananatory notes:

* Under Linux, there are some powerful tools that for all practical purposes are unavailable under Windows (I can imagine all the old Linux hands saying "Duh!").
* One of these tools is something called "shell programming". This means writing code that a command shell executes.
* There is something like this under Windows, but as usual, the Windows version is a weak imitation.
* The most common Linux shell is named "Bash". The name comes from "Bourne Again SHell," which, in turn ... (imagine a lengthy recursion terminating in a caveman's grunt).
* There are many other shells available. Unless there is a compelling reason not to, I recommend that people stick to the Bash shell, because this increases the chance that your scripts will be portable between machines, distributions, even operating systems.
* I'll be showing some very basic examples of Bash shell programming on this page, and I want to say at the outset that shell programming is an art, not a science. That means there is always some other way to do the same thing.
* Because shell programming is an art, please don't write to say, "Wow, that was a really inefficient way to do such-and-such." Please do write (message page) to report actual errors.
* If this page seems too sketchy and elementary for your taste, you can choose from among the more advanced resources in this list.

Introduction
# Early computers had a teletype machine with a keyboard for I/O. Later, glass terminals became the norm, but the behavior was much the same — a keyboard, a screen, a text display. A program was responsible for mediating the transaction between the operator and the machine, and as the years passed this program (the command interpreter or shell) became more sophisticated.

# At this stage the command shell has become rather too sophisticated, typically having a dozen ways to do any particular thing. In this page I will try to limit myself to describing a handful of useful operations, based not on listing everything that can be done, but on solving specific problems. There are some links at the bottom of this page for those wanting more depth.

Preliminaries
* There are two primary ways to use the shell: interactively and by writing shell scripts.

o In the interactive mode, the user types a single command (or a short string of commands) and the result is printed out.
o In shell scripting, the user types anything from a few lines to an entire program into a text editor, then executes the resulting text file as a shell script.
o It is often the case that an interactive session becomes a shell scripting session, once things get too complicated for simple interactive line entries, or because a specific sequence of commands appears to be generally useful and worth preserving.

* In a modern Linux environment the user can have more than one shell open at a time, either by moving between a sequence of independent "virtual terminals" in a text-only environment, or by opening any number of shell windows in the X Windows environment.
* The advantage of having more than one shell available is that one shell can be used for testing one command at a time, while another might provide a text editor for assembling single commands into a shell program.
* I don't want to get too distribution-specific, but if you are not hosting X Windows and want more than one simultaneous shell session, with many current distributions you can switch between "virtual terminals" by pressing Ctrl+Alt+F(n), n typically between 1 and 6.
* In an environment that supports X Windows, simply open any desired number of command shell windows and move between them.
to see more programming Bash

0 comments:

Post a Comment