| Lesson 4 || Tools to manage input and output |
| Objective || List options for controlling input and output |
Communication using input and output in Unix
List options for controlling input and output in a shell script.
To really accomplish anything useful in a script, you must use the script to communicate with a user or with other programs.
Shell programming provides many different techniques for collecting information and communicating it to others. Some of these techniques will be familiar to you from working on a UNIX command line; others are normally used only within shell scripts. This communication is called input and output.
Some of the techniques used to read information in a shell script (collect input) include:
- Using the read command to collect input from the keyboard
- Reading information directly from a file
- Starting another command and use its output as input for something else
- Using a command to receive input from a program over the network
Some of the techniques used to communicate results of a shell script (process output) include:
- Writing information to the screen
- Saving information directly to a file
- Sending information over the network to another program that’s waiting for it
- Sending the output to another command that is waiting for input
You may recognize that many of these options are related to redirecting the Standard Input and Standard Output channels of UNIX (STDIN
Shell scripts also can use specific commands to read input or write output where they need to.
Later in the course, you will learn in detail the different ways that a shell script can handle input and output to communicate with the rest of the world.
- STDIN: The standard input channel used to retrieve data for a program, normally from the keyboard.
- STDOUT: The standard output channel used to send output from a program, usually to the screen.
Users utilize tests to control which parts of a script are executed. The next lesson explains this.
The directories in this output are separated from one another by a colon. These are all the directories that bash will check when you ask it to run a program or command. If your command is not stored in any of these directories, bash cannot run it. Also, note that bash will check these directories in the order they appear in the PATH. This order is important because it may make a difference if you have two commands of the same name in two directories in your PATH. If you are having trouble finding a particular command, you can use the which command with the name of that command to see its PATH in the shell, as in the listing 3-4 below.
$ which ruby
$ which echo
Listing 3-4: Using which to find a command in PATH
Now armed with this information, you could move or copy the file in question to one of the directories listed by the echo $PATH command, as in the
Listing 3-4, and then the command will run. We use which throughout the course to determine the full path to commands. It is a useful tool for debugging a broken PATH
Shell Programming Concepts - Quiz