While loop, input from find command

Hello nix Experts,

I am a *nix rookie and have run into this issue, can some one help me here and let me know what I am doing wrong.

/home/user1> while read n
> do
> echo $n
> done < <(find . -type f -ctime -1 | grep abc)

I am getting the below error:
-sh: syntax error near unexpected token `<'

When I do a shell check to see what Shell I am using by default, i get this:

/home/user1>which shell
/usr/bin/which: no shell in (/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/opt/ibm/director/bin)

Any suggestion is greatly appreciated.

Try echo $SHELL to find your shell. Or just ps. "which shell" looks for a command named shell, which isn't much help.

How about this, with -name to avoid the grep. This checks in the filename. -path would check in the file path instead.

find . -type f -ctime -1 -name '*abc*' | while IFS="" read -r FILE
do
        echo "$FILE"
done

The IFS avoids having read split on anything, and the -r prevents it from trying to evaluate backslashes. It's a good habit when you want read to give you literal, unchanged input...

One caveat: Variables you set inside the "while" won't be set outside the loop. The | puts the loop into a separate, independent subshell.

Depends on the shell.. :wink:

$
$ cat kshlooptest
#! /bin/ksh
LAST="nothing"
find /etc -name 'w*' 2> /dev/null | while IFS="" read name ; do
        LAST=$name
done
echo outside of while loop LAST = $LAST
exit 0
$
$
$ ./kshlooptest
outside of while loop LAST = /etc/gconf/schemas/window-list.schemas
$

Yes, I know. ksh yes, absolutely-anything-other-than-ksh no.

This cuts both ways incidentally. It's possible for ksh to break a script from variables behind a pipe having a different scope than specified by POSIX.

Thanks for the reply guys, that was helpful

The current standard says:

So, variable assignments in a while loop in a pipeline are allowed, but not required, to be visible in the current shell execution environment after the pipeline terminates.

The last element of a pipeline is run in the current shell execution environment in ksh .
The last element of a pipeline is run in a subshell environment in bash .
On this point, both are OK according to the standard.