리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality...
-
Upload
elaine-black -
Category
Documents
-
view
218 -
download
0
Transcript of 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality...
![Page 1: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/1.jpg)
리눅스 : Lecture 6Shell Programming
![Page 2: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/2.jpg)
UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize the shell and user interface. To string together a series of Unix commands
to create new functionality. To create custom commands that do exactly
what we want.
Extension of Functionality
![Page 3: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/3.jpg)
Shell
Command Interpreter that turns text that you type (at the command line) in to actions
User Interface: take the command from user
Shell Programming We often want to do a number of commands together And bundle them up into one new command. Just like a batch file in MS-DOS
![Page 4: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/4.jpg)
Shell 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.
![Page 5: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/5.jpg)
Popular Shells
sh Bourne Shell ksh Korn Shell bash Bourne-Again Shell csh,tcsh C Shell (for this course)
Shell scripts among those shells are slightly different
![Page 6: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/6.jpg)
shell script
Creating a simple shell script A shell script is a file that contains commands that the shell
can execute. Any commands you enter in response to a shell prompt.
A utility A compiled program Another shell script
Control flow commands Run a shell script
Enter the script filename on the command line The shell interprets and execute the commands one after
another Why shell script?
Simply and quickly initiate a complex series of tasks or a repetitive procedure.
![Page 7: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/7.jpg)
Shell script example
#!/bin/csh
echo “Current Time - `date`”
echo I am `whoami`
C Shell
![Page 8: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/8.jpg)
Invoking scripts
There are two ways to launch scripts:1) Direct interpretationcsh scriptfile [args …]
2) Indirect interpretationThe first line of the file must be#!/bin/csh
and the file must be executable (permission).
C Shell
![Page 9: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/9.jpg)
Shell Variables
Environment Variables Used to provide information to programs
(Global) environment variable New programs and shells inherit
environment variables from their parent shell (Local) shell variable
Used only by that shell Not passed to other processes
![Page 10: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/10.jpg)
Environment Variables
“env” or “printenv” command Display current environment variables
DISPLAY The graphical display to use, e.g. nyssa:0.0 EDITOR The path to your default editor, e.g. /usr/bin/vi GROUP Your login group, e.g. staff HOME Path to your home directory, e.g. /home/frank HOST The hostname of your system, e.g. nyssa IFS Internal field separators, usually any white space (defaults to tab,
space and <newline>) LOGNAME The name you login with, e.g. frank PATH Paths to be searched for commands, e.g.
/usr/bin:/usr/ucb:/usr/local/bin PS1 The primary prompt string, Bourne shell only (defaults to $) PS2 The secondary prompt string, Bourne shell only (defaults to >) SHELL The login shell you're using, e.g. /usr/bin/csh TERM Your terminal type, e.g. xterm USER Your username, e.g. frank
![Page 11: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/11.jpg)
Set Shell Variables
Mostly set automatically when log in
setenv $ setenv NAME value # in C Shell
set $ set name = value # in C Shell
![Page 12: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/12.jpg)
Variables
To set variables:set X [= value] # processed as a string
To unset variables : unset X
Variable contents are accessed using ‘$’:echo $PATH
C Shell
![Page 13: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/13.jpg)
ArrayTo create lists:set Y = (abc 1 123)
To set a list element:set Y[2] = 3
To view a list element:echo $Y[2]
To count the number of variable elements:echo $#Y
set fname = prog1
rm ${fname}.c
C Shell
![Page 14: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/14.jpg)
Built-in Variables
$user -- who am I? $path -- my execution path (list of directories to be searched
for executables) $term -- what kind of terminal I am using $status -- a numeric variable, usually used to retun error
codes $prompt -- what I am currently using for a prompt $shell -- which shell am I using (usu. either /bin/csh or
/bin/sh)
% set
Will display the variable lists.
![Page 15: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/15.jpg)
Arithmetic (@) command
C shell provides arithmetic operaters must be used with the arithmetic (@)
command Arithmetic command works only with
integers.
set count = 5
@ count += 2
echo $count
90
![Page 16: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/16.jpg)
Shell Arithmetic
expr op1 math-operator op2
Example% expr 1 + 3
% expr 10 \* 3
% set A = `expr 3 + $B`
![Page 17: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/17.jpg)
Command arguments
A shell script to swap files:#! /bin/csh –f
cp $argv[1] tempfile
cp $argv[2] $argv[1]
cp tempfile $argv[2]
rm –f tempfile
Arguments : $argvThe number of arguments to a script: $#argv
-f option says we want fast startup (no read .cshrc) .
C Shell
![Page 18: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/18.jpg)
if-then-else
if ( expr ) simple-command
if ( expr ) then
commandlist-1
[else
commandlist-2]
endif
C Shell
![Page 19: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/19.jpg)
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
![Page 20: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/20.jpg)
Loops
while ( expr )
commandlist
end
foreach var ( worddlist )
commandlist
end
C Shell
![Page 21: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/21.jpg)
switch
switch ( str )case string1:
commandlist1breaksw
case string2:commandlist2breaksw
defaultcommandlist
endsw
C Shell
![Page 22: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/22.jpg)
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 HarmfulEdsger W. Dijkstra, CACM, March 1968
C Shell
![Page 23: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/23.jpg)
shift command
Moves the values in the parameters toward the beginning of the parameter list#!/bin/csh –f
echo “There are” $#argv “parameters\n”
while ($#argv > 0)echo –n “$argv[1] “shift
end
echo “\n”echo “There are now” $#argv “parameters”echo “end of script”
C Shell
![Page 24: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/24.jpg)
Input
Reading Line by Line
% set x = $<
This is a line.
% echo $x
This is a line.
![Page 25: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/25.jpg)
File Operators
-e file : True if file exists-r file : True if file is readable-l file : True if file exists and is a symbolic link-w file : True if file exists and is writable-x file : True if file exists and is executable-o file : True if the user owns it-f file : True if the file exists and is a regular file-d file : True if the file exists and is a directory-s file : True if file exists and has a size greater than zero-z file : True if file length is zero (empty)
![Page 26: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/26.jpg)
Logical operator
! : NEGATE && : logical AND || : logical OR
Ex)if (! -e somefile) then
# does not exist
![Page 27: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/27.jpg)
Debugging
%csh –vx somescript args
-v : vervose-x : echoes the commands after all substitutions are
made-n : syntax check. No execution
![Page 28: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/28.jpg)
example
#!/bin/csh
if (-e $argv[1]) then
echo $argv[1] exists
else
echo $argv[1] does not exist and cannot be opened
endif
# rest of script here
C Shell
![Page 29: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/29.jpg)
example#!/bin/csh
set sum = 0
echo –n “Enter a number: ”
set num = $<
while ($num != “”)
@ sum += $num
echo –n “Enter the next number: ”
set num = $<
end
echo “\nThe sum of the number is : $sum”
C Shell
![Page 30: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/30.jpg)
Guidelines
Shell script is better than C program if the problem can be solved by using UNIX commands
Why script? Easier to create and modify Easy to debug
Good thing to do Use redirection and pipe Do validity check (argument number , type) Check existence of files and directories Display error messages
![Page 31: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/31.jpg)
example
#!/bin/csh
set j = (1 2 3 4 5)
foreach i ($j)
echo $i Hello
end
C Shell
![Page 32: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/32.jpg)
example
#!/bin/csh
set ary = `cat ary.dat`
echo “The whole array : $ary”
echo “The number of elements : $#ary”
echo “The first element: $ary[1]”
echo “The last element: $ary[$#ary]”
C Shell
![Page 33: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/33.jpg)
Numeric validation example
#!/bin/csh
echo $argv[2] > temp
grep ‘^[0-9]*$’ temp > /dev/null
if ($status != 0) then
echo “Month argument is not numeric”
exit 1
endif
if ($argv[2] < 1 || $argv[2] > 12) then
echo “Month argument must be <1…12>”
exit 2
endif
echo “Validation is OK. We continue.”
C Shell
![Page 34: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/34.jpg)
example#! /bin/csh -fforeach name ($argv)
if ( -f $name ) thenecho -n "delete the file '${name}' (y/n/q)?"
elseecho -n "delete the entire dir '${name}' (y/n/q)? "
endifset ans = $< # $< means “read a line”switch ($ans)
case n: continue
case q: exit
case y: rm -rf $namecontinue
endswend:
C Shell
![Page 35: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/35.jpg)
Exercise 1
Write a shell script that displays the number of files and directories in a given directory
format% ./fd_count.csh directory_name
![Page 36: 리눅스 : Lecture 6 Shell Programming. UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize.](https://reader035.fdocuments.net/reader035/viewer/2022062519/5697bfdf1a28abf838cb2979/html5/thumbnails/36.jpg)
Exercise 2
Write a shell script that removes duplicate words from an input text file.
Format % remove_dup.csh in.txt out.txt
FourTwoOneOneFourTwoTwoThree
Four Two One Three
in.txt
out.txt