23
UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology [email protected]

UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology [email protected]

Embed Size (px)

Citation preview

Page 1: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

UNIX Shell Script

Dr. Tran, Van Hoai

Faculty of Computer Science and Engineering HCMC Uni. of Technology

[email protected]

Page 2: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

UN

IX S

hel

l Sc

rip

t

What is a shell script?

A series of OS commands for execution Stored in a text file

#!/bin/shrm -f /tmp/listing.tmp > /dev/null 2>&1touch /tmp/listing.tmpls -l [a-z]*.doc | sort > /tmp/listing.tmplpr -Ppostscript_1 /tmp/listing.tmprm -f /tmp/listing.tmp

Page 3: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

UN

IX S

hel

l Sc

rip

t

Components of a script

#!/bin/shrm -f /tmp/listing.tmp > /dev/null 2>&1touch /tmp/listing.tmp# This is a commentls -l [a-z]*.doc | sort > /tmp/listing.tmplpr -Ppostscript_1 /tmp/listing.tmprm -f /tmp/listing.tmp

Shell in use (sh, bash, csh)

Comment

Command

Page 4: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

UN

IX S

hel

l Sc

rip

t

How to invoke a script

Correct way$ /bin/bash my_script arg_1 arg_2

Simple way$ my_script arg_1 arg_2

The first line specifying the shell must be provided in simple way

Page 5: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

UN

IX S

hel

l Sc

rip

t

How to be helped Man pages

hoai@moon:~> man bash

...

hoai@moon:~> man sh

...

Page 6: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

UN

IX S

hel

l Sc

rip

t

Definitions

Blank = chunk of tab or space Name = sequence of ASCII letters, digits,

underscores, beginning with a letter or underscore

Argument = string supplied on command-line

Page 7: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

UN

IX S

hel

l Sc

rip

t

Example (argument) (1)

#!/bin/sh##############################echo "Script name is [$0]"echo "First argument is [$1]"echo "Second argument is [$2]"echo "This process ID is [$$]"echo "This argument count is [$#]"echo "All arguments [$@]"

Page 8: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

UN

IX S

hel

l Sc

rip

t

Example (argument) (2)

hoai@moon:~> my_script.sh hoai 1 university

Script name is [my_script.sh]

First argument is [hoai]

Second argument is [1]

This process ID is [5401]

This argument count is [3]

All arguments [hoai 1 university]

Page 9: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

UN

IX S

hel

l Sc

rip

t

Filename metacharacters* Match any string of zero or more characters

? Match any single character

[abc...] Match any one of the enclosed characters; a hyphen can specify a range (e.g., a-z, A-Z, 0–9)

[!abc...] Match any character not enclosed as above

~ Home directory of the current user

~name Home directory of user name

~+ Current working directory ($PWD)

~- Previous working directory ($OLDPWD)

Page 10: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

UN

IX S

hel

l Sc

rip

t

Simple regular expressions (Korn shell)?(pattern) Match zero or one instance of pattern

*(pattern) Match zero or more instances of pattern

+(pattern) Match one or more instances of pattern

@(pattern) Match exactly one instance of pattern

!(pattern) Match any strings that don't match pattern

Pattern = sequence of patterns separated by “|”

Page 11: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

UN

IX S

hel

l Sc

rip

t

Example (metacharacters)

List files having prefix new$ ls new*

Cat files having prefix ch and one more letter$ cat ch?

Vi files starting by letters from D to E$ vi [D-R]*

Print files not *.o and core (Korn shell)$ pr !(*.o|core) | lp

Page 12: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

UN

IX S

hel

l Sc

rip

t

Quoting

"" Everything taken literally, except $ (variable substitution) ` (command substitution) “ (ending mark)

'' Everything taken literally

\ Charater following \ taken literally

Page 13: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

UN

IX S

hel

l Sc

rip

t

Example (quoting)

$ echo 'my class is "unix and tools"'

My class is "unix and tools"

$ echo "Well, isn't that \"good\" ?"

Well, isn't that "good" ?

$ echo "You have `ls | wc –l` files in `pwd`"

You have 34 files in /home/hoai

$ echo "The value of \$x is $x"

The value of $x is 100

Page 14: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

UN

IX S

hel

l Sc

rip

t

Variables (1)

var=value… Set variable to value

${var} Use value of var

${var:-value} Use var if set, otherwise, use value

${var:=value} Use var if set, otherwise, user value and assign it to var

${var:?value} Use var if set, otherwise, print value and exit

${var:+value} Use value of var is set, otherwise use nothing

${#var} Use the length of var

${#*} or ${#@} Use the number of positional arguments

Page 15: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

UN

IX S

hel

l Sc

rip

t

Variables (2)

${var#pattern} Use value of var after removing pattern from the left. Remove shortest matching

${var##pattern} Same as #pattern. Remove longest matching

${var%pattern} Use value of var after removing pattern from the right. Remove shortest matching

${var%%pattern} Same as %pattern. Remove longest matching

Page 16: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

UN

IX S

hel

l Sc

rip

t

Command formscmd1 ; cmd2 Multiple commands on the same line

{cmd1 ; cmd2} Commands as a group in current shell

(cmd1 ; cmd2) Commands as a group in a subshell

cmd1 | cmd2 Pipe

cmd1 `cmd2` Command substitution

cmd1 $(cmd2) POSIX Command substitution (nesting is allowed)

cmd $((expression))

POSIX shell arithmetic substitution

cmd1 && cmd2 AND; cmd1, then cmd2 if (cmd succeeds)

cmd1 || cmd2 OR

! cmd NOT; change exit status

Page 17: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

UN

IX S

hel

l Sc

rip

t

Example (command forms)

$ nroff file > file.txt & Format in the background $ cd; ls Execute sequentially $ (date; who; pwd) > logfile All output is redirected $ sort file | pr -3 | lp Sort file, page output, then print $ vi 'grep -l ifdef *.c' Edit files found by grep $ grep XX file && lp file Print file if it contains the pattern; $ grep XX file || echo "XX not found" otherwise, echo an error message

Page 18: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

UN

IX S

hel

l Sc

rip

t

Simple commands

sort Sort lines

grep Search for regular expressions

basename Get file name from path string

dirname Get directory name from path string

cut Chop up a text by strings or characters

[(test)] Predicate or conditional processor

tr 'a' 'b' Transform characters

expr Simple arithmetic processor

eval Evaluate variables

date Create date strings

head/tail Access lines in files

Page 19: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

UN

IX S

hel

l Sc

rip

t

Example (script) (1)

#!/bin/bash

alphabet="a b c d e" # Initialise a stringcount=0 # Initialise a counterfor letter in $alphabet # Set up a loop controldo # Begin the loop count=`expr $count + 1` # Increment the counter # Display the result echo "Letter $count is [$letter]"done

Page 20: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

UN

IX S

hel

l Sc

rip

t

Example (script) (2)

alphabet="a b c d e" # Initialise a stringcount=0 # Initialise a counterwhile [ $count -lt 5 ] # Set up a loop controldo # Begin the loop count=`expr $count + 1` # Increment the counter # Position of next letter position=`bc $count + $count - 1` letter=`echo "$alphabet" | cut -c$position-

$position` # Get next letter # Display the result echo "Letter $count is [$letter]"done

Page 21: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

UN

IX S

hel

l Sc

rip

t

Homeworks (1)

Write a script for C compiler Objective: use gcc by default, if gcc is not availablle,

find another compiler (ending with cc) instead.

Write a script to convert filenames to lowercase letters Input: a directory path string Output: all filenames in the directory in lowercase

Page 22: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

UN

IX S

hel

l Sc

rip

t

Homeworks (2)

Write a script to warn users using too much spaceObjective: find all users of the system

(/etc/passwd, cut), check if someone uses > 1GB, mail him a warning message

Page 23: UNIX Shell Script Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

Dr. Tran, Van Hoai2007

UN

IX S

hel

l Sc

rip

t Conditional structure Loop structure Function File input/output Array

are NEXT