Bash script: issue changing directories in script

I am working on a script that checks two arguments at the command line. The first argument is a search pattern, the second can be a file or a directory, if it is a file a second script is called that checks it for the search pattern. If the second argument is a directory, it checks for the search pattern in all files in the that directory.
The script works ok if the second argument is a sub-directory of the working directory of the script. The issue occurs when the script needs to change directories. I don't think it likes the "/" if I specify a directory, but i'm stuck as far figuring out what to do next.

Here is the snippet of code I am having the trouble with:

elif [ -d $2 ]   #test the command line argument for a directory
then
#change directories to the the second command line argument
        cd $2
        filename=`ls -l | cut -c59-78`
        for entry in $filename
        do
        # Verify that the name read in the directory is a file
                if [ -f $filename ]
                then
        # Search the files for the pattern specified by $1, then call search.sh
            ./search.sh $1 $listing

Hi

Instead of writing -- ls -l | cut -c59-78 this might not give you the correct file name.

write : ls -l | awk '{print $NF}' to pull the file name.

-if you will provide a "directory name" as argument - It will by default look into your current directory as subdirectory only. and can't intelligently cd to the directory you are expecting.

-You can provide full path name in the argument so that the cd will work properly if it is directory else if it will be a file you can search on it.

Thanks, I will try it with the awk statement and see what I get.

Something I left out the first time, the script returns the error
"[: ash: unexpected operator/operand" when I try to use the full path of a directory.

Thanks again.

first off: you don't need the "cd" statement at all. And secondly, you should NEVER EVER iterate through an unknown directory with a for-loop: if the number of directory entries becomes higher you will execeed the maximum command line length and your script will fail. write it the following way:

ls ${2}/* | while read filename ; do
     do_something "$filename"
done

Another problem is the "./search.sh" you mention at the end of your code snippet. You know that you are in some arbitrary directory and not the one you have started the script in, yes?

I hope this helps.

bakunin

You may be safer with ls -1 (ls hyphen one) which just lists the names for the files, then pipe to a "while read". This is in case you have filenames containing space characters which will break the "for" statement or you have so many files that the "for" statement becomes too long.
You have an issue with $entry which is not referred to at all and is probably surplus.
It would also be better to place $2 in to a named variable for readability.
Notice how we put double quotes round any parameter (including filenames) to preserve any embedded space characters.

I like your use of comment lines. Very good.

elif [ -d "${2}" ]   #test the command line argument for a directory
then
#change directories to the the second command line argument
        cd "${2}"
        ls -1 | while read filename
        do
        # Verify that the name read in the directory is a file
                if [ -f "${filename}" ]
                then
        # Search the files for the pattern specified by $1, then call search.sh
            ./search.sh "${1}" "${listing}"

Bump: bakunin has made a valid point. The cd could cause the "./search" statement to fail unless the script called "search" existed in that directory.

At the start if the script (or in your .profile) you can change $PATH to ensure that you find your scripts.

Let's say that your scripts are in directory /home/Breakology/scripts .
If we want to refer to our scripts by name we can adjust the PATH.

PATH=${PATH}:/home/Breakology/scripts
export PATH

The script called "search" can then be called as "search" not "./search".

Addition Information Added:

(I have absolutely no idea where the "bump" came from and I am sending this post to test a theory).

Thanks guys, I am re-writing the script using your suggestions. My original problem was a syntax error, I was [ -f filename ] the wrong variable and thats why I got the errors.
I am glad I posted it here because of the good suggestions I received. I am starting to catch on, there is just A LOT to learn. Thanks again.