M80
M80

Reputation: 994

Using * in find command in a shell program

I have a simple program where I was to get a list of files which were created over 5 minutes earlier. I'm taking few parameters and one of the parameter is the pattern to match. I'm using that variable in find command, and its replacing * with blank as expected , but I tried with ' , " , and all possible things to get the below simple command executed in shell. Please advise.

Expected output

find /bicmpd/EVENTS/data/event_header -name 'part*.csv' -cmin +5

/bicmpd/EVENTS/data/event_header/part.csv
/bicmpd/EVENTS/data/event_header/part123.csv
/bicmpd/EVENTS/data/event_header/part_789.csv
/bicmpd/EVENTS/data/event_header/part_preavenn.csv

Command use to run the shell program

ksh -x file_list.ksh -lgdir /bicmpd/EVENTS/logs -folder /bicmpd/EVENTS/data/event_header -pattern part*.csv -prefix header_

file_list.ksh

#!/bin/ksh

_this=`basename $0`;

while [[ $1 == -* ]]
do
   case $1 in
        -lgdir)
            _logfileDir=$2
            shift 2
           ;;
        -folder)
            _folderName=$2
            shift 2
           ;;
         -pattern)
             _pattern=$2
             shift 2
            ;;
         -prefix)
            _prefix=$2
             shift 2
            ;;
         *)
          break
           ;;
         esac
done

list=`find $_folderName -name ${_pattern} -cmin +5 2>/dev/null `
cnt=`echo $list | wc -l`

if [ "$cnt" != "0" ]; then

for entry in $list
do
    echo $entry
done

exit $?
fi

exit 0

The output always lists the part.csv file only because the find command in the program is find /bicmpd/EVENTS/data/event_header -name part.csv -cmin +5 the * vanished

Console output

-bash-4.1$ ksh -x file_list.ksh -lgdir /bicmpd/EVENTS/logs -folder /bicmpd/EVENTS/data/event_header -pattern part*.csv -prefix header_
+ basename file_list.ksh
+ _this=file_list.ksh
+ [[ -lgdir == -* ]]
+ _logfileDir=/bicmpd/EVENTS/logs
+ shift 2
+ [[ -folder == -* ]]
+ _folderName=/bicmpd/EVENTS/data/event_header
+ shift 2
+ [[ -pattern == -* ]]
+ _pattern=part.csv
+ shift 2
+ [[ -prefix == -* ]]
+ _prefix=header_
+ shift 2
+ [[ '' == -* ]]
+ find /bicmpd/EVENTS/data/event_header -name part.csv -cmin +5
+ 2> /dev/null
+ list=/bicmpd/EVENTS/data/event_header/part.csv
+ echo /bicmpd/EVENTS/data/event_header/part.csv
+ wc -l
+ cnt=1
+ [ 1 '!=' 0 ]
+ echo /bicmpd/EVENTS/data/event_header/part.csv
/bicmpd/EVENTS/data/event_header/part.csv
+ exit 0
-bash-4.1$ view file_list.ksh
-bash-4.1$

Upvotes: 0

Views: 75

Answers (1)

dtanders
dtanders

Reputation: 1845

You need to enclose your parameter in single quotes so the shell doesn't process the * before it gets to the script. -pattern 'part*.csv' instead of -pattern part*.csv

Upvotes: 1

Related Questions