Sie sind auf Seite 1von 4

LECTURE

UNIX II Shell Programming Basics


OBJECTIVE: Be able to write simple Unix shell scripts containing the
commands introduced in Lecture "Unix Basics".
REFERENCE: Unix in a Nutshell, O'Reilly (less than $20).
C Shell Programming Language Overview
CONSTRUCT
----------------#
echo xxxxx
echo -n xxxxx
exit
repeat n command

EXPLANATION/EXAMPLE
----------------------------Comment line
Display xxxxx; skips to next line.
Displays xxxxx but does not skip.
Exit from the shell script.
Repeat command n times (e.g., to skip 3 lines:
repeat 3 echo " ").

formal arguments

Arguments are passed when you invoke the script.


they are named $1, $2, etc. The collection is
named $argv. $2 is also $argv[2].
Local variables inside your shell script. Their value
is referenced as $varname.
Assignment statement: sets value of shell variable

variables
set var=expression
`command`
set var=$<

"Command substitution" expression; value is the


result returned by the command.
Read input value from keyboard into shell variable.

switch ...

C switch statement.

foreach (<list>)
<body>
end

Loop for each value in list.

while (<condition>)
<body>
end

Loop while <condition> is TRUE.

if (<condition>) <body> 1-way if.


if (<condition>) then
2-way if: if-else.
<body1>
else
<body2>
endif
csh -xv <scriptfile>

Execute shell script in DEBUG/TRACE mode.

A Shell Script is a file that contains a mixture of Unix commands and


C-like programming constructs like variables, loops, decisions.
Unix II Shell Programming Basics
2000, E.L. Jones, Ph.D.

Page 1 of 4

Shell Script Documentation Requirements:


#!/bin/csh
# -----------------------------------------------------# File Name: scriptname.csh
#
# Function:
#
# Invocation: scriptname.csh arg1 arg2 ... argn
#
# Arguments:
#
(1) arg1 - description
#
(2) arg2 - description
#
...
#
# Inputs:
#
(1) Input1 description
#
...
#
# Outputs:
#
(1) Output1 description
#
# Input Files Used:
#
infilename1 - what it contains
#
...
#
# Output Files:
#
outfilename1 - what it contains
#
...
#
# Author: xxxx
# Date:
xxxxxx
# ======================================================
...
exit

NOTES:
(1)
(2)
(3)
(4)

The first line directs Unix to use the C shell (there are others,
such as Korn, Bourne, etc.)
Giving the file name ensures the wrong file is not used.
Briefly state the purpose played by the shell script.
Invocation gives the pattern for how to call the script. Give
symbolic names that clarify what the arguments are.
Example:

(5)
(6)
(7)

ComputePay hours rate not ComputePay num1 num2

Describe each argument carefully as to meaning, data type and value


range. If there are no arguments, say NONE.
Identify the author and date.
WARNING: This is the standard for documentation. Do not omit any
section or give erroneous or non-informative data.

Unix II Shell Programming Basics


2000, E.L. Jones, Ph.D.

Page 2 of 4

Preparing your Unix account for shell scripts:


(1)

The shell script file must have EXECUTE permissions set (using
chmod).

(2)

The shell script file should be placed in directory ~/bin:


a. If you don't have this directory, create it.

(3)

For a brand new shell script to be invokable from ANY directory,


do the following.
a. Place the script in the ~/bin directory
b. Set the EXECUTE permission: % chmod 755 filename
c. Tell Unix you've added a new script: % rehash

(4)

Make sure your Unix environment executes shell scripts within the
directory in which the script is invoked:
a. View the ~/.cshrc file: % more ~/.cshrc
b. If the line "cp " is near the end of the file, you need to
COMMENT IT OUT ("#cp"). Otherwise, all shell scripts would
execute in your HOME DIRECTORY.

Sample Shell Script #1: Display arguments.


#!/bin/csh
# --------------------------------------------------------------# File Name:
ShowNames.csh
# Function:
Show the Unix login and the given name of a user.
# Invocation: ShowNames.csh first [last]
# Arguments:
#
(1) first - the first name
#
(2) last - the last name; optional.
# Inputs: NONE.
# Outputs:
#
(1) Unix login
#
(2) first and last names passed as arguments.
# Input Files: NONE.
# Output Files: NONE.
# Author: EL Jones
# Date:
25 June 1997
# --------------------------------------------------------------echo My Unix login ... $user
echo My given name ... $1 $2
exit

Example: When logged in under cis4910joe,


User command: % ShowNames.csh Ed Jones
outputs: My Unix login ... cis4910joe
My given name ... Ed Jones

Unix II Shell Programming Basics


2000, E.L. Jones, Ph.D.

Page 3 of 4

Sample Shell Script #2: Display inputs.


#!/bin/csh
# --------------------------------------------------------------# File Name:
ShowNames.csh
# Function:
Show the Unix login and the given name of a user.
# Invocation: ShowNames.csh
# Arguments: NONE.
# Inputs:
#
(1) first - the first name
#
(2) last - the last name.
# Outputs:
#
(1) Unix login
#
(2) first and last names (read from keyboard).
# Input Files: NONE.
# Output Files: NONE.
# Author: EL Jones
# Date:
26 December 1999
# --------------------------------------------------------------echo n "Enter your first name: "
set first=$<
echo n "Enter your last name: "
set last=$<
echo My Unix login ... $user
echo "My given name ... $first $last"
exit

Example: When logged in


User command:
prompt/input:
prompt/input:
outputs:

under cis4910joe,
% ShowNames.csh
% Enter your first name: Billy
% Enter your last name: Bob
My Unix login ... cis4910joe
My given name ... Billy Bob

Unix II Shell Programming Basics


2000, E.L. Jones, Ph.D.

Page 4 of 4

Das könnte Ihnen auch gefallen