Lab due at end of lab session during the 13th week

UNIX Shell Scripting

 

UNIX shell scripting is when you write a sequence of commands in a file on a UNIX machine.  This file needs to have its permissions set so that the eXecutable permission is on.  Then, by typing in the name of the script at the prompt, the sequence of commands will be executed as if the user had just typed in the sequence of commands.

 

Script Output: echo

The echo command is the main method for creating output from a script.  Another method is to use an existing UNIX command that already creates output.  The output will include a return character at the end (to force the next piece of outputted information on the next line), unless the -n option is used. 

 

                echo How are you?

                echo -n Please enter a response:

 

Script Variables:

A variable name must start with a letter, while the remaining characters can be letters or digits.  To access the value stored in the variable, the variable name must be preceded by a dollar sign, $.  You can assign a value to the variable with the use of the equal sign, =.  When assigning a value the equal sign must not be preceded or followed by a space or a tab character.  The following example will assign a value to a variable and print it out.

 

                person1=Mark

                echo The value of person1 is $person1

 

Script Input: read

The read command allows a script to read in information and store it in one or more variables.  The read command is followed by one or more variable names.  The first space-delineated word is stored in the first variable name.  The second space-delineated word is stored in the second variable. ETC.  The last variable will get the remaining words from the input.

 

                echo -n Please enter your name (first name followed by last name):

                read firstname lastname

 

Command line argument variables: $0, $1, $2, ... , $9

A command line argument is information typed immediately following the name of the script.  For example, assume we had the script called readin.

 

                ~> readin This is some information

 

The words "This is some information" would be the command line arguments for the script readin.  These values can be accessed by the command line argument variables.  The variable $0 always contains the name of the script (in this case "readin").  The variable $1 contains the first command line argument (in this case "This").  The variable $2 contains the second command line argument (in this case "is"). ETC.

 

Lab Assignment  - Submit the following shellscripts to the digital drop box before they are due.

 

     Write a script that will prompt for, read in, and print out a person's name.  The beginning of the script is given above.  The script is to print out the person's first name on one line and the person's second name on the next line.  This script is to read the information using the read command.

     Write another script that will get the person's name from the command line.  This script will not prompt for information.  It will assume the input is given correctly on the command line.