Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Korn shell script doesn't change directory 4

Status
Not open for further replies.

varocho

Programmer
Dec 4, 2000
238
US
Maybe one of you can help me figure out what's wrong with the following shell script I wrote earlier today, it's supposed to change directory to the 'ams' application subdirectory, and then print out the name of every file in this subdirectory, as well as display any and all lines within each file containing the word 'reference' (I try running this script from my home directory, but the script doesn't change the directory to the 'ams' subdirectory for some reason):

# shell script to list names of scripts containing the phrase 'reference'

$dirName = "/hpnpages/cgi-bin/applinks/ams"
cd $dirName

for fileName in `ls`
do
if (test -f $fileName)
then
print "Current file name is $fileName\n"
cat $fileName | grep 'reference'
print "\n"
fi
done

cd $HOME
 

Not:
$dirName = "/hpnpages/cgi-bin/applinks/ams"
but:
dirName="/hpnpages/cgi-bin/applinks/ams"

(take spaces, no $ sign)

eugene
 
Thanks, Eugene, much appreciated. If you and/or anyone else here has any recommendations on a good book on Korn shell programming, I'm all ears, er, eyes.
 
A couple of other suggestions:

[tt]for fileName in *[/tt]

Saves an extra shell and /usr/bin/ls invocation.

[tt]grep 'reference' $fileName[/tt]

Saves an unnecessary cat process.

A good starting point would be the site of David Korn himself:
Annihilannic.
 
For a beginner:

Unix Shell Programming by Stephen Kochan & Patrick Wood

Get the 3rd edition.
 
list names of scripts containing the phrase 'reference'
A one line way:
Code:
grep -l 'reference' /hpnpages/cgi-bin/applinks/ams/*

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Many thanks to all of you who took the time to answer the question of a Unix newbie. :)
 
Also check out the ksh man page, some good stuff, though a little difficult to read sometimes.

Martin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top