Computing.Net > Forums > Unix > Listing FileNames put into List cha

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Listing FileNames put into List cha

Reply to Message Icon

Name: NewbieKsh
Date: January 14, 2007 at 20:33:30 Pacific
OS: XP
CPU/Ram: 512
Product: Toshiba
Comment:

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
fi

can anyone tell me why it doesn't work?
First argument is for pattern, $2 is for file

thanks again



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: January 15, 2007 at 12:47:09 Pacific
Reply:

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.


0

Response Number 2
Name: NewbieKsh
Date: January 16, 2007 at 13:08:57 Pacific
Reply:

$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


0

Response Number 3
Name: NewbieKsh
Date: January 17, 2007 at 20:32:15 Pacific
Reply:

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
done

script: 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



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Test for FileName sco 5.0.5 losing ip conne...



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


Sponsored links

Ads by Google


Results for: Listing FileNames put into List cha

unix mailing list www.computing.net/answers/unix/unix-mailing-list/1816.html

list of users www.computing.net/answers/unix/list-of-users/3425.html

Delete files from textfile listing? www.computing.net/answers/unix/delete-files-from-textfile-listing/7218.html