![]() |
| |||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Greater Than the Sum of Its Parts The Unix environment provides a huge number of command line tools - small programs that do a specific job. One of the most powerful aspects of the command line is the ability to combine these tools so that they work together to perform a more complex task - a task that is very specific to your particular requirements at that time. Commands can be combined in two ways. Directing the output of one command to form the input of the next, thus chaining a small number of commands together. This is called piping. More complex combinations of commands are written to a file and form a script, usually using the control framework afforded by the shell. This is called a shell script. We will consider a useful subset of commands that form a core, and which can:
First we must learn how to use the individual commands effectively. Then we are in a position to combine commands using piping. Finally, by learning the shell scripting language we can combine commands in a programmatic manner to create a bespoke solution. |
Tell Me More...
|
|
In the Bin Executables are traditionally held in 'bin' directories - that's bin as in binary. The term comes from a programmer's perspective. A program starts life as source code in ASCII text, and is then compiled into machine code to form an executable. Executable code is in binary form - loads of 1's and 0's. Which Bin? Recall that the command 'which' will tell you where any given command can be found. % which cd /bin/ls |
|
|
What exactly are the command line, the Terminal, and the shell? The shell is simply a program; one whose job it is to listen to what you type at the keyboard, to launch other programs like 'ls', and to display the output from those programs. The shell is your main method of interaction with Unix itself. A shell will offer features to make your time at the command line more productive, like displaying a prompt, keeping a command line history, and auto completion. Most importantly the shell provides the framework to enable piping, and a scripting language to allow scripting. There are many different shell programs in use in the Unix world, and each brings its own interactive experience and scripting language. The default shell for Mac OS X is called the t-shell ('tcsh') and it's the one I will assume you are using. Mac OS X provides most of the alternative shells too, and you are free to choose which you use. Most shells will work for this tutorial, but occasionally something may not work, or may behave differently. You can think of a shell's abilities in terms of (a) its interactive experience, and (b) the scripting language it provides. A Terminal Situation In the days before personal computers, the shell's input and output went via a serial line between the computer and a 'dumb terminal' - a big lump of hardware that could not do much more than send characters down the line and display received characters on a screen (or on paper if it was a teletype). Now we have a graphical interface and use a program that emulates those old lumps of metal. The Mac OS X Terminal is simply a program (an Aqua program) that does just this, and of course you can have as many of them as you like. When you open the Terminal, or a new Terminal window, it automatically starts the t-shell (or your default shell) for you. The Command Line The command line is a concept; a way of referring to the line of text you type in order to enter a command. |
The Bourne Shell The Bourne Shell (or 'sh') is the standard Unix shell that all installations must include. 'sh' scripts are therefore portable to any Unix system, and it is essential to lean 'sh' scripting. Many system features such as 'cron' run their scripts using it. 'sh' is very good as a scripting shell, but its interactive experience is lacking. bash 'bash' is a popular shell with the Linux crowd. It is upward compatible with 'sh' and so can run all 'sh' scripts. A system with bash does not need 'sh', and I think the 'sh' of OS X is actually 'bash'. Bash greatly improves on the interactive experience of 'sh'. The name 'bash' comes from Bourne Again SH. Sorry :-) |
You will find Unix commands in one of eight places.
/bin contains a minimum set of essential user commands.
/usr/bin contains the rest of the user commands that are delivered with your machine.
/usr/local/bin is for user commands that you (or the system administrator) have added locally.
~/bin is for user commands that you personally have installed and are not intended for other users.
For each of the above there is a corresponding 'sbin' directory for system, as opposed to user, commands. These are commands that control the Unix system and its configuration.
The Right Path
The shell does not implicitly know which directories to search in order to find an executable. It must be told via the 'path' shell variable.
Type:
echo $pathto see which directories are in your search path.
It should include at least the following:
echo $path /usr/bin /bin /usr/sbin /sbin
and in that order.
Ideally, it should include:
echo $path /Users/your-user-name/bin /Developer/Tools /Developer/Applications /usr/local/bin /usr/bin /bin /usr/local/sbin /usr/sbin /sbin .
You can set up 'path' from a login script in the file '.login' in your home directory. Use an editor to add these lines to the file:
## System-wide tcsh login file #
# Set paths set path = ( \ ~/bin \ /Developer/Tools /Developer/Applications \ /usr/local/bin /usr/bin /bin \ /usr/local/sbin /usr/sbin /sbin \ . \ )
You only need the line:
/Developer/Tools /Developer/Applications \if you have installed the developer tools.
The next time you start the Terminal, or create a new Terminal window, the shell running in that window will automatically execute '.login' and your new path should be active.
The environment variable 'PATH' also defines the search path. The shell automatically keeps 'PATH' and 'path' synchronized so you need only maintain one of these. I have not dealt with shell or environment variables before, but I shall do so in Part 8.
Note: If you add the 'set path' command to your login file, ensure that there are no characters (not even a space) after the '\' character at the end of each line.
Next Page
This page has set the scene, describing the environment in which commands are executed, and the potential for combining them.
Page two will consider some commands in more detail - head, tail, locate, and find.
|
|
Part 5 - Working With Unix (page 1 of 2) |
|