Computing.Net > Forums > Unix > Script in ksh

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

Script in ksh

Reply to Message Icon

Original Message
Name: Bodhies
Date: September 18, 2002 at 18:30:59 Pacific
Subject: Script in ksh
OS: UNIX
CPU/Ram: Pentium lll 800 MHz
Comment:

Hello everybody!!

I hope anyone can help me with this problem:

I need to elaborate a script that evaluate de content on the actual directory and print an executive resume on what is inside about files, directories, links and special objects, like:
/user/home/director: 10 files, 8 directories, 5 links, 0 special objects.

If there are included arguments these has to correspond to a system directories. In this case the script should evaluate each one of those directories and show a resume of each one of them


Report Offensive Message For Removal


Response Number 1
Name: Jerry Lemieux
Date: September 18, 2002 at 20:21:36 Pacific
Reply: (edit)

Here is a tree script that was sent to me a while back. I modified it to use Korn shell and took out all the `expr` stuff and typeset some integer variables so the script would run a little faster.

#!/bin/ksh

###################
function search { #
###################

for dir in *
do
if [ -d $dir ] ; then # ==> If it is a directory (-d)...
dirLevel=0 # ==> Temp variable, keeping track of directory level.
while [ $dirLevel != $depth ] # Keep track of inner nested loop.
do
print -n "| " # ==> Display vertical connector symbol,
# ==> with 2 spaces & no line feed in order to indent.
(( dirLevel += 1 )) # ==> Increment dirLevel.
done
if [ -L $dir ] ; then # ==> If directory is a symbolic link...
print "+---$dir" `ls -l $dir | sed 's/^.*'$dir' //'`
# ==> Display horiz. connector and list directory name, but...
# ==> delete date/time part of long listing.
else
print "+---$dir" # ==> Display horizontal connector symbol...
# ==> and print directory name.
if cd $dir ; then # ==> If can move to subdirectory...
(( depth += 1 )) # ==> Increment depth.
search # with recursivity ;-)
# ==> Function calls itself.
(( numDirectories += 1 )) # ==> Increment directory count.
fi
fi
fi
done
cd .. # ==> Up one directory level.
if [ $depth ] ; then # ==> If depth = 0 (returns TRUE)...
dirFinished=1 # ==> set flag showing that search is done.
fi
(( depth -= 1 )) # ==> Decrement depth.

} # end of the search function

# - Main -
if [ $# = 0 ] ; then
cd `pwd` # ==> No args to script, then use current working directory.
else
cd $1 # ==> Otherwise, move to indicated directory.
fi

print "Initial directory = `pwd`"

dirFinished=0 # ==> Search finished flag.
typeset -i depth=0 # ==> Depth of listing.
typeset -i numDirectories=0
typeset -i dirLevel=0

while [ $dirFinished != 1 ] # While flag not set...
do
search # ==> Call function after initializing variables.
done

print "Total directories = $numDirectories"


This is not my script, although I could write it myself. The copy I got doesn't have the author's name on it, but he was a pretty good script writer. This is a nice shell. You can add some additional counters, remove the tree stuff, test for a link test -L and a file test -f and you should be able to get a decent report.

Jerry


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge
Poll History




Data Recovery Software