a simple loop

Does any body can help me with a loop in this example?

if [ "$i" -eq 1 -a "$#" -ge 1 ]
then
    if [ "$#" -ne 1 ]
       then
        runner=$(grep "$1" "$2")
    runne=$(grep "$1" "$3")
        run=$(grep "$1" "$4")
    fi
fi

#                                                                                
# Message on screen or mail or both                                              
#                                                                                

if [ "$mailopt" = FALSE ]
    then
      echo "*****************************"
      echo "$2:$runner"
      echo "$3:$runne"
      echo "$4:$run"
      echo "*****************************"
    else
      echo "You have mail!"
      run=$(whoami | cut -c1-8)
      echo "pattern $1 found in file $2:\                                        
            $runner" | mail $run
fi

Instead of specifying $1 $2 $3 with different variables (runner, runne, run). Can you guys help me to change it with a loop so it accepts several files and the output would show the pattern dog in different files
Just what grep does (grep dog file1 file2 file3).
file1: cat dog house
file2: dog cat house
file3: house dog cat
Thanks I just cannot find out how to make that little detail. Thanks.

Please, use code tags.
Somthing like
if (($#<2)); then
echo "There must be at least 2 parameters (pattern, file)"
exit 1
fi
Pattern="$1"
shift
Response=$(grep "$Pattern" "$@")
# Message on screen or mail or both
if [ "$mailopt" = FALSE ]; then
echo ""
echo "$Response"
echo "
"
else
echo "You have mail!"
run=$(whoami | cut -c1-8)
echo "pattern $1 found:
$Response" | mail ...
fi

1 Like

If you change frans' first line to:

if [ $# -lt 2 ]; then

it will run in many shells other than bash

1 Like

Thank you very much guys. Happens that some times I want to reinvent the wheel. Thank you thank you.