1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Kỹ thuật lập trình >

Appendix D. Sample Korn Shell Scripts

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.08 MB, 487 trang )


Korn Shell: Unix and Linux Programming Manual, Third Edition, The

By Anatole Olczak



Table of Contents



Appendix D. Sample Korn Shell Scripts



Display Files - kcat

Here is a simple Korn shell version of the Unix cat command. It is only 3-4 times slower than the Unix

version (on a 100-line file), because it uses the exec command for the file I/O.



#!/bin/ksh

#

# kcat - Korn shell version of cat

#

# Check usage

if (($# < 1))

then

print "Usage: $0 file ..."

exit 1

fi

# Process each file

while (($# > 0))

do

# Make sure file exists

if [[ ! -f $1 ]]

then

print "$1: non-existent or not accessible"

else

# Open file for input

exec 0<$1

while read LINE

do



# Display output

print $LINE

done

fi

# Get next file argument

shift

done



Top



Korn Shell: Unix and Linux Programming Manual, Third Edition, The

By Anatole Olczak



Table of Contents



Appendix D. Sample Korn Shell Scripts



Interactive uucp - kuucp

Here is an interactive version of the uucp command. Instead of looking for a system name in the uucp

systems file using grep, the remote system name is verified by using file I/O substitution and Korn shell

patterns.



#!/bin/ksh

#

# kuucp - Korn shell interactive uucp

#

# Check usage

if (($# > 0))

then

print "Usage: $0"

exit 1

fi

# Set variables

PUBDIR=${PUBDIR:-/usr/spool/uucpublic}

# This sets UUSYS to the contents of the HDB-UUCP

# Systems file. It may be different on your system.

UUSYS=$(
# Get source file

read SOURCE?"Enter source file: "

# Check source file



if [[ ! -f $SOURCE ]]

then

print "$SOURCE: non-existent or not accessible"

exit 2

fi

# Get remote system name

read RSYS?"Enter remote system name: "

# Check remote system name. It looks for a pattern

# match on the system name in the UUSYS file

#

# For the Bourne shell or older versions

# of Korn shell, this could be given as:

# if [[ $(grep ^$RSYS $UUSYS) != "" ]]

if [[ $UUSYS != *$RSYS* ]]

then

print "$RSYS: Invalid system name"

exit 2

fi

print "Copying $SOURCE to $RSYS!$PUBDIR/$SOURCE"

uucp $SOURCE $RSYS!$PUBDIR/$SOURCE



Top



Korn Shell: Unix and Linux Programming Manual, Third Edition, The

By Anatole Olczak



Table of Contents



Appendix D. Sample Korn Shell Scripts



Basename - kbasename

This is the Korn shell version of the Unix basename command. It is used to return the last part of a

pathname. A suffix can also be given to be stripped from the resulting base directory. The substring

feature is used to get the basename and strip off the suffix.



#!/bin/ksh

#

# kbasename - Korn shell basename

#

# Check arguments

if (($# == 0 || $# > 2))

then

print "Usage: $0 string [suffix]"

exit 1

fi

# Get the basename

BASE=${1##*/}

# See if suffix arg was given

if (($# > 1))

then

# Display basename without suffix

print ${BASE%$2}

else

# Display basename

print $BASE



fi



Top



Korn Shell: Unix and Linux Programming Manual, Third Edition, The

By Anatole Olczak



Table of Contents



Appendix D. Sample Korn Shell Scripts



Dirname - kdirname

Here is the Korn shell version of the Unix dirname command. It returns a pathname minus the last

directory. As in kbasename, the substring feature does all the work.



#!/bin/ksh

#

# kdirname - Korn shell dirname

#

# Check arguments

if (($# == 0 || $# > 1))

then

print "Usage: $0 string"

exit 1

fi

# Get the dirname

print ${1%/*}



Top



Korn Shell: Unix and Linux Programming Manual, Third Edition, The

By Anatole Olczak



Table of Contents



Appendix D. Sample Korn Shell Scripts



Display Files with Line Numbers - knl

This is a simple Korn shell version of the Unix nl command. It displays line-numbered output.



#!/bin/ksh

#

# knl - Korn Shell line-numbering filter

#

# Initialize line number counter

integer LNUM=1

# Check usage

if (($# == 0))

then

print "Usage: $0 file . . ."

exit 1

fi

# Process each file

for FILE

do

# Make sure file exists

if [[ ! -f $FILE ]]

then

print "$FILE: non-existent or not readable"

exit 1

else

# Open file for reading



exec 0<$FILE

# Read each line, print out with line number

while read -r LINE

do

print "$LNUM: $LINE"

((LNUM+=1))

done

fi

# Reset line number counter

LNUM=1

done



Top



Korn Shell: Unix and Linux Programming Manual, Third Edition, The

By Anatole Olczak



Table of Contents



Appendix D. Sample Korn Shell Scripts



Find Words - match

The match command uses Korn shell pattern-matching characters to find words in a dictionary. It can be

used to help with crossword puzzles, or test your patterns.



#!/bin/ksh

#

# match - Korn shell word-finder

#

# Check usage

if (($# < 1 || $# > 2))

then

print "Usage: $0 pattern [file]"

exit 1

fi



# Check/set DICT to word dictionary

: ${DICT:=${2:-/usr/dict/words}}

# Open $DICT for input

exec 0<$DICT

# Read each word into WORD

while read WORD

do

# This command didn't work on all systems. If

# it doesn't on yours, use this instead of



# exec 0<$DICT:

# cat $DICT | while read WORD

#

# If WORD matches the given pattern,

# print the match

[[ $WORD = $1 ]] && print - $WORD

done



Top



Xem Thêm
Tải bản đầy đủ (.pdf) (487 trang)

×