Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using multiple arguments to find and grep

I am trying to make a script so that I can give command with variable number of arguments myfind one two three and it finds all files in the folder, then applies grep -i one then grep -i two, and grep -i three and so on.

I tried following code:

#! /bin/bash
FULLARG="find . | "
for arg in "$@"
do 
    FULLARG=$FULLARG" grep -i "$arg" | "
done
echo $FULLARG
$FULLARG

However, though the command is created but it is not working and giving following error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$ ./myfind one  two three
find . | grep -i one | grep -i two | grep -i three |
find: unknown predicate `-i'

Where is the problem and how can it be solved?

>Solution :

You could store the result of find . and keep filtering that out till you have command line arguments:

#!/bin/bash

result="$(find .)"
for arg in "$@"
do
    result=$(echo "$result" | grep -i "${arg}")
done

echo "$result"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading