[NTLUG:Discuss] Shell script help 2

. Daniel xdesign at hotmail.com
Mon May 15 11:31:30 CDT 2006


You rule.  That works perfectly.  I had already written the parameter 
testing code and left it out as irrelevant, but thanks for that too.

>". Daniel" writes:
>- Using "find" I'd like to be able to read in all the file and
>- directory names line-by-line and process a file copy based on
>- 'whatever' criteria.  Here's what I have come up with so far:
>-
>- --------------------------------
>- echo ""; echo -n "Reading file structure..."
>- cd $1
>- echo "Processing..."
>- for line in $(find)
>- do
>-   if [ ${#line} -ne 1 ]
>-   then
>-     echo "${line#'./'}"
>-   fi
>- done
>- --------------------------------
>
>	[....]
>
>- Here's the problem.  As I mentioned, the for loop reads the output
>- of "find" "word by word" and not "line by line."  This presents
>- a problem when filenames have spaces or other delimiter characters
>- in them.  Is there another way to read input line-by-line the
>- way I need?
>
>	Take a look at read and while on the shell manual page.
>
>	A thought, be more explicit about the arguments you're
>passing to find.  You've defaulted everything, which  may not
>really what you want..
>
>	Anyway, an example solution for you (if I remember the
>original problem correctly:
>
>	#!/bin/sh
>	#
>	if [ -n "$1" ]; then
>	    if [-d "$1" ] ; then
>		cd $1
>	    else
>		echo "$0: not a directory"
>		exit 1
>	    fi
>	fi
>
>	find . -type f -print | while read fn ; do
>	    dir="${fn%/*}"
>	    file="${fn##*/}"
>	    echo "file: \"${file}\"; dir: \"${dir}\""
>	done





More information about the Discuss mailing list