Operating System - Linux
1753317 Members
5091 Online
108792 Solutions
New Discussion юеВ

Re: 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 2
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...