How to use grep command in shell script?

Hi,

I am new to shell scripting.
I had written a small code which accepts config file as parameter. In config file I mentioned a path for input files,

 
INPUT_FILE_FOLDER=/project/dev/con/InputFile
SEARCH_MASK_INPUT_FILE=ABC_CR_PQR*.*

I want to use this path to get all file names from folder.

 
INPUT_LIST_FILES=`ls $INPUT_FILE_FOLDER | grep -i $SEARCH_MASK_INPUT_FILE`
 for file in $INPUT_LIST_FILES
  do
     echo $file
  done

but i am getting below message as,

 
Usage: grep [-E|-F] [-c|-l|-q] [-insvxbhwy] [-p[parasep]] -e pattern_list...
        [-f pattern_file...] [file...]

Could you please help me out.

try make use of egrep

egrep -i "$SEARCH_MASK_INPUT_FILE"

I used egerp, but still I am getting same message.

Try with double quote over search_mask_input_file, it'll work

INPUT_LIST_FILES=`ls $INPUT_FILE_FOLDER | egrep -i "$SEARCH_MASK_INPUT_FILE" `
 for file in $INPUT_LIST_FILES
  do
     echo $file
  done

The pattern is a globbing pattern, not a regex. Try this:

for file in "$INPUT_FILE_FOLDER/"$SEARCH_MASK_INPUT_FILE; do
  echo $file
done

I am executing script from another location and my input files are in different location.

So given solution is not working.
If I write "$SEARCH_MASK_INPUT_FILE", then I am getting list of all files and folders present in the current location.

Do you mean on a remote system or in a different directory. If the latter is the case then my suggestion should work.

Using you solution, i am getting list of file present in the same directory, from where i am executing script. I need to look for files in specified directory.
If I use

for file in "<path>/ABC_CR_PQR*.*"; do
  echo $file
done

I am getting list of files with path as well. I just want the file names.
So instead of doing "<path>/ABC_CR_PQR*.*"; I want to use one command so that, i'll get output in one variable.

You can either do this:

cd "$INPUT_FILE_FOLDER"
for file in $SEARCH_MASK_INPUT_FILE; do
  echo $file
done
for file in "$INPUT_FILE_FOLDER/"$SEARCH_MASK_INPUT_FILE; do
  echo ${file##*/}
done

BTW do not put double quotes around the pattern

Thanks a lot. Its working fine :slight_smile: