Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am trying to create a script that will list search for a pattern. My script work fine for listing if in current directory but fails if the file is not in the current directory. This is what I have for the latter portion.
elif [ -d $2 ] #if directory is argument 2
then
cd $2 # change directory to argument 2
wc -f* | listing #take file names put in list
for entry in $listing # do a loop
do
if [ -f $entry ] #if entry is a file
then
../srchfile $1 $entry #perform subscript
fican anyone tell me why it doesn't work?
First argument is for pattern, $2 is for filethanks again

If the $2 directory isn't a full path or if you don't reside in $2's parent directory, the change command will fail.

$2 is for a file or a directory. So check if it is a file first. If it is a file than the script works. If it is not a file but a directory than the script doesn't work. Maybe it is my loop that isn't correct. $2 would be the full path typed into the command line. However it might be one diretory over. So this works:
./srch if /export/home/tests/practice with practice being the file, this does work
./srch if /export/home/tests
at this point the loop is suppose to list all the files in the directory test and search for the pattern if. However it does nothing.thanks again

This is my code so far. I don't seem to be able to get it to run. My other script srchfile runs fine. This code does not seem to pick up the pattern at all. Any suggestions would be great. The error I keep getting is second argument should be a file. It echos the file but does not give the pattern listing.
original=$PWD
file=$2
if [ $# -gt 2 ]
then
echo "Please enter only two arguments"
exit 0
fi
if [ $# -lt 2 ]
then
echo "Please enter atleast two arguments"
exit 0
fi
if [ -f $file ]
then
./srchfile $1 $file
elif [ -d $file ]
then
cd $file
fi
for testfile in *
do
if [ -f $testfile ]
then
cd /export/home/jansen03/itec400/homework
./srchfile $1 "$2/$testfile"
echo "$testfile"
fi
donescript: srchfile
if [ $# -gt 2 ] #test if arguments are greater than 2
then
echo "Please enter only 2 arguments"
exit 0
fi
if [ $# -lt 2 ] #test if arguments are less than 2
then
echo "Please enter at least 2 arguments"
exit 0
fi
if
[ -f $2 ] #test for file name
then
echo "File=$PWD/$2/$1" #print out directory file, file name, and pattern
OUTPUT=`egrep -in $1 $2` #filter ignore case prefix line in number
echo "$OUTPUT"
else
echo "error: second argument must be a file"
echo "usage: $0 search-pattern file"
exit 0
fi
This script seems to work just fine.
thanks in advance for any help

![]() |
Test for FileName
|
sco 5.0.5 losing ip conne...
|

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