The UNIX Shell script is similar to the DOS batch processing command, but it is more powerful than the UNIX Shell. In some respects, Shell is more than some advanced languages.
The Shell script below demonstrates how to use Shell scripts to write recursive Programs.
Run the following preparation command before running:
Ln tree. sh/usr/bin/tree
Ln tree. sh/usr/bin/wtree
Ln tree. sh/usr/bin/dtree
Rm tree. sh
# Tree. sh
# Depth first Directory list
Dtree (){
PWD = 'pwd | sed's/\ $ //'
For d in $ *
Do
Echo "$ {PWD}/$ d"
[-D "$ d"-a-x "$ d"] & {
Cd "$ d"
Dtree *
Cd ..
PWD = 'pwd | sed's // \ $ // '# restore pwd
}
Done
}
# Depth first Directory list
Wtree (){
PWD = 'pwd | sed's/\ $ //'
For d in $ *
Do
Echo $ {PWD}/$ d
Done
For d in $ *
Do
[-D "$ d"-a-x "$ d"] & {
Cd $ d
Wtree *
Cd ..
}
Done
}
# Directory list
Tree (){
PWD = 'pwd | sed's/\ $ //'
For d in $ *
Do
Echo $ {PWD}/$ d
Done
}
# Main
TREE = 'basename $0'
If ["$1"]
Then DIR = "$1"
Else DIR = "."
Fi
If cd $ DIR
Then $ TREE *
Else echo "$0: Directory $1 read fail ."
Fi
# (End)