Stanford University School of Earth Science
 
Home
News
New Users
Policies
Network File Server
Web Server
Get Help
Net Connections
Your Macintosh
Your Windows PC
Your Unix/Linux System
Other School Resources
Using Unix
   Overview
   Documentation
   Accounts
   Using the Shell
   Using X-Windows
   File Manipulation
   Net Commands
   Editing Text
   Formatting Text
   Printing
   Filters & Utilities
   Programming

Other forms of input to shell variables or commands in a script

Last revision August 6, 2004

You can execute a command from the shell script and have it read the next few lines in the script as the command's standard input. You do this by redirecting the standard input with the special symbol << followed by a "marker" string. The shell reads the lines from the script following this command until it encounters a line that contains only the marker string. All the lines it has read (not counting the marker string line) are then fed to the command as its standard input.

Here is an example of a fragment of code from a shell script using this feature:

    cat > newfile << EOF
    This line from the script will be read by cat.
    So will this line.
    The next line has the 'EOF' marker string that terminates the cat input.
    EOF

You can read a line of input from the standard input of the shell script (usually the terminal, but could be redirected from a file) and set it as the value of a variable with this syntax:
      set variable=$<

This can be used to "prompt" for information from the user. First use the echo command to print a question; then read the answer typed by the user at the terminal with the syntax above.

 


Comments?

Stanford University    |