Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
General
cancel
Showing results for 
Search instead for 
Did you mean: 

ksh script fails if runs from another dir, (for find:)

SOLVED
Go to solution
JianW
Occasional Advisor

ksh script fails if runs from another dir, (for find:)

Hello, all

Hope you can easely point me what it wrong witmy setup, I created .ksh script with command and I can't run it from one location and can run OK from another dir. what can be wrong ??
Tried absolutely everything, doen's matter what user, do it from command line with args.

if fails get this:
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]

command itself is:
find $DIRR -name $MASK -mtime +$TERM -exec ls {} \;
I"ve played with qoutes around DIRR, no result either. I attached all info in .txt attached.

Tx big to all
J
##################### my script:
#!/usr/bin/ksh

DIRR=/$1
MASK=*.$2
TERM=$3

echo " f11 gymscripts: $1 2:$2 3:$3 "
echo " DIRR= $DIRR term=$TERM for files with $MASK"
echo " .... find coming "

#ls -l $DIRR/$MASK

find $DIRR -name $MASK -mtime +$TERM -exec ls {} \;

echo "_______________ end"
######################################


find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]
_______________ end

2 REPLIES
Steven E. Protter
Exalted Contributor

Re: ksh script fails if runs from another dir, (for find:)

Shalom,

If your script sets the PATH environment to include where find command actually is, this kind of inconsistency can be avoided.

There also appears to be a possible problem with one of the variables on the find line, $DIRR $MASK or $TERM being wrong.

Also remember that / is interpreted differently by the shell and may need to be escaped.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor
Solution

Re: ksh script fails if runs from another dir, (for find:)

Hi:

If you use wildcard characters in 'find' commands you need to quote the argument containing them so that the shell doesn't interpret them. Rather, you want them passed to 'find' for it to interpret.

In your case, most notably the '$MASK' variable contains a "*" character. Depending on what's in or not it your starting directory this can cause different behavior. To see what is really happending, change to your various directories and simply do:

# echo ${MASK}

For that matter, do :

# echo *

Instead of doing:

# find $DIRR -name $MASK -mtime +$TERM -exec ls {} \;

...do:

# find $DIRR -name "$MASK" -mtime +$TERM -exec ls {} \;

As an aside, it is also better (faster) to do:

# find $DIRR -name "$MASK" -mtime +$TERM -exec ls {} +

...which causes multiple arguments to be collected for each process spawned by '-exec'. Thus, in this case, instead of running 'ls' for *each* object found, you run 'ls' for multiple arguments at a time.

You might also want to limit your output to only files:

# find $DIRR -type f -name "$MASK" -mtime +$TERM -exec ls {} +

Regards!

...JRF...