Capturing the output from an exec command

Hi,

I'm new to ksh - unix platform. I'm writing a small script which will search my current directory and will search for file names which it takes input from the users.

Here is the code I'm having.

1 #!/bin/ksh
2 echo "enter a file name to be searched in the current dir : "
3 read filename
4
5 file="find . -name "$filename""
6 exec $file

The Problem is when I put a valid file name which is present in my dir, it shows the path name, which is correct. But when I put an invalid path name it shows no messages. Since, I'm using an exec command it shows the output directly to the console. How can I trap the output from the exec command so that I can add custome messages.

Please help.

You can go serveral ways.

  • grap the exit code of find to determine if a file was found

    text find ....... test [ $? -gt 0 ] && exit 1
  • test if the file exists and is executable

    text test [ -x $file ] || exit 1
  • error messages are written to stderr (2) and you cannot grap them directly you must redirect stderr to stdout
    text exec $file 2>&1 | grep ....

consider what happens if find finds more than one match ?