1

I would like to improve a simple script i made.

While it works fine for a single argument and does what i want i have issues making it to run in parallel or at the same time for all my argument values. I would like to improve it to run on multiple arguments and output my grep findings at the same time and not in sequence, while is not the good choice ? Any help to make this output work would be really appreciated. Thank you very much.

  • i have multiple files file1.log file1.txt file2.log file2.txt
  • need to grep something from *.log and something from *.txt
  • output the grep lines for all arguments into the same echo.

my script so far looks like this :

#!/bin/bash

filename=$@



error=$(grep  'ERROR' ${filename}.l)
phone=$(grep 'phone'  ${filename}.e)
invalid=$(grep  'invalid' ${filename}.l)

while true ; do 

echo -e  " Start of message \n :  
         $error \n
         $invalid \n
        $phone \n
          End of message \n "

break 
done 
exit

This is how i would like the output to look

Start of message 

error form  file1
Phone number from file1
Invalid from file1

error form  file2
Phone number from file2
Invalid from file2

error form  file3
Phone number from file3
Invalid from file3 

etc 

End of message 
  • You probably want to accept one of the two equivalent answers provided to you below. Both work and essentially are the same. Marking an answer as accepted by checking the green check box on its left, (a) helps other users identify solved issues and (b) provides them with a way to identify valid elements of the knowledge-base on SE. (c) It also functions as a reward to the individual who gives you the best answer. – Cbhihe Aug 25 '16 at 9:29
1

$@ is an array not a string so what you really want to do is use a loop to iterate through the array. Try this:

#!/bin/bash
for filename in "$@"; do
   error=$(grep  'ERROR' "${filename}.l")
   phone=$(grep 'phone'  "${filename}.e")
   invalid=$(grep  'invalid' "${filename}.l")
   echo -e  " Start of message \n :
      $error \n
      $invalid \n
      $phone \n
      End of message \n "
done 
exit 
  • You proably want to take the last echoed line in yr for-block echo -e "End of message \n" OUT of the block. Otherwise you'll get multiple "End of message", i.e. one for each filename. Not what OP asked for. – Cbhihe Aug 25 '16 at 9:35
1
echo "Start of message "

for file in "$@"
do

error=$(grep  'ERROR' ${file}.l)
phone=$(grep 'phone'  ${filee}.e)
invalid=$(grep  'invalid' ${file}.l)

echo -e  "${error} from ${file}\n ${phone} from ${file}\n  $invalid from ${file}\n\n"

done
echo -e "End of message \n"
  • such a smooth workaround. :D this is exactly what i wanted! thank you! – foreverlearner Aug 24 '16 at 19:14
  • small typo in phone=$(grep ...${fil_e_}.e) – Cbhihe Aug 25 '16 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.