0
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Shells Scripts
Andrew Nashel
nashel@cs.unc.edu
Department of Computer Scie...
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Course topics
• Shell scripts
• Homework
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Shells scripts
Any collection of shell commands can be
stored in a file ca...
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Invoking scripts
There are two ways to launch scripts:
1) Direct interpret...
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Variables
To set variables:
set X [= value]
Variable contents are accessed...
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Variables cont’d
To create lists:
set Y = (abc 1 123)
To set a list elemen...
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Command arguments
A shell script to swap files:
#! /bin/csh –f
set tmp = $...
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
if-then-else
if ( expr ) simple-command
if ( expr ) then
commandlist-1
[el...
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
if-then-else cont’d
An example:
if ($#argv <> 2) then
echo “we need two pa...
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Loops
while ( expr )
commandlist
end
foreach var ( worddlist )
commandlist...
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
switch
switch ( str )
case string1:
commandlist1
breaksw
case string2:
com...
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
goto (Considered harmful!)
To jump unconditionally:
goto label
A label is ...
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
An example script
#! /bin/csh -f
foreach name ($argv)
if ( -f $name ) then...
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Homework
Due Monday:
Write a shell script that reads a file and
replaces a...
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Homework cont’d
Example:
$ ./myscript
File to read: news.txt
Search text: ...
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Hmmmm.
Okay, shell scripts are useful,
but a little boring.
Upcoming SlideShare
Loading in...5
×

Shell Script

1,115

Published on

PPT

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,115
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
34
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Shell Script"

  1. 1. The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Shells Scripts Andrew Nashel nashel@cs.unc.edu Department of Computer Science February 6, 2004 COMP 121-401: UNIX Programming
  2. 2. The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Course topics • Shell scripts • Homework
  3. 3. The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Shells scripts Any collection of shell commands can be stored in a file called a shell script. Scripts have variables and flow control statements like other programming languages. There are two popular classes of shells: C shell (csh) and variants (tcsh) Bourne shell (sh) and variants (bash, ksh)
  4. 4. The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Invoking scripts There are two ways to launch scripts: 1) Direct interpretation csh scriptfile [args …] 2) Indirect interpretation The first line of the file must be #!/bin/csh and the file must be executable. C Shell
  5. 5. The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Variables To set variables: set X [= value] Variable contents are accessed using ‘$’: echo $PATH To count the number of variable elements: echo $#Y C Shell
  6. 6. The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Variables cont’d To create lists: set Y = (abc 1 123) To set a list element: set Y[2] = 3 To view a list element: echo $Y[2] C Shell
  7. 7. The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Command arguments A shell script to swap files: #! /bin/csh –f set tmp = $argv[1] cp $argv[2] $argv[1] cp $tmp $argv[2] The number of arguments to a script: $#argv C Shell
  8. 8. The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL if-then-else if ( expr ) simple-command if ( expr ) then commandlist-1 [else commandlist-2] endif C Shell
  9. 9. The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL if-then-else cont’d An example: if ($#argv <> 2) then echo “we need two parameters!“ else set name1 = $argv[1] set name2 = $argv[2] endif C Shell
  10. 10. The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Loops while ( expr ) commandlist end foreach var ( worddlist ) commandlist end C Shell
  11. 11. The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL switch switch ( str ) case string1: commandlist1 breaksw case string2: commandlist2 breaksw default commandlist endsw C Shell
  12. 12. The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL goto (Considered harmful!) To jump unconditionally: goto label A label is a line such as: label: The classic paper on why not to use goto: Go To Statement Considered Harmful Edsger W. Dijkstra, CACM, March 1968 C Shell
  13. 13. The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL An example script #! /bin/csh -f foreach name ($argv) if ( -f $name ) then echo -n "delete the file '${name}' (y/n/q)?" else echo -n "delete the entire dir '${name}' (y/n/q)? " endif set ans = $< switch ($ans) case n: continue case q: exit case y: rm -r $name; continue endsw end C Shell
  14. 14. The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Homework Due Monday: Write a shell script that reads a file and replaces a particular text string with another, all input given interactively. Hint: Use sed for text replacement. This is your final graded assignment!
  15. 15. The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Homework cont’d Example: $ ./myscript File to read: news.txt Search text: Duke Replacement text: Dookie Dookie University Department of Mathematics Weekly Calendar …
  16. 16. The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Hmmmm. Okay, shell scripts are useful, but a little boring.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×