0

I am trying to match the email address from list of files and below is the script i created

#!/bin/bash
for varFile in  `ls /var/mail/vhosts/penguin.com/kolosky/cur`;
do
  grep "kolosky@penguin.com" $varFile;
if [ $? -eq 0 ];
then
echo $i > /tmp/match.txt;
fi
done

the files in the /var/mail/vhosts/penguin.com/kolosky/cur looks like below (50K + files)

1501303677.VdeI4b4601fM34360.vps47619.domain.local:2,
1501327088.VdeI4b46274M887245.vps47619.domain.local:2,
1501350893.VdeI4b46676M714819.vps47619.domain.local:2,
1501351810.VdeI4b466dcM483006.vps47619.domain.local:2,
1501352354.VdeI4b466f6M691159.vps47619.domain.local:2,
1501355883.VdeI4b4675bM598246.vps47619.domain.local:2,
1501360533.VdeI4b4678fM767116.vps47619.domain.local:2,
1501362598.VdeI4b467c2M944374.vps47619.domain.local:2,S
1501366933.VdeI4b46827M6394.vps47619.domain.local:2,
1501371004.VdeI4b468b3M654492.vps47619.domain.local:2,
1501381688.VdeI4b470aaM394586.vps47619.domain.local:2,
1501383441.VdeI4b471dcM297322.vps47619.domain.local:2,
1501385824.VdeI4b4723fM284699.vps47619.domain.local:2,
1501386623.VdeI4b47247M170748.vps47619.domain.local:2,

But this is giving me below error

grep: 1502103891.VdeI4b56e59M181359.vps47619.domain.local:2,: No such file or directory
grep: 1502103891.VdeI4b56e5aM235352.vps47619.domain.local:2,: No such file or directory
grep: 1502103891.VdeI4b56e5bM258839.vps47619.domain.local:2,: No such file or directory
grep: 1502103891.VdeI4b56e5cM390465.vps47619.domain.local:2,: No such file or directory
grep: 1502103891.VdeI4b56e5dM439473.vps47619.domain.local:2,: No such file or directory
grep: 1502103891.VdeI4b56e5eM479629.vps47619.domain.local:2,: No such file or directory

I tried to modifiy the script to ls /var/mail/vhosts/mailservice935.com/info/cur | sed 's/:/\\:/g' | sed 's/,/\\,/g' to escape the ":" and "," but it gave the same error

jww
  • 97,681
  • 90
  • 411
  • 885
sherpaurgen
  • 3,028
  • 6
  • 32
  • 45
  • 2
    Btw.: Please take a look: http://www.shellcheck.net/ – Cyrus Aug 30 '17 at 16:25
  • 2
    See also [Why you shouldn't parse the output of `ls`](http://mywiki.wooledge.org/ParsingLs), and [BashPitfalls #14](http://mywiki.wooledge.org/BashPitfalls#echo_.24foo). – Charles Duffy Aug 30 '17 at 16:30
  • daayum!, i had been using output of ls this whole time . Thanks @CharlesDuffy i simply replaced first line with `for i in /var/mail/vhosts/mailservice935.com/info/cur/*;` and it worked – sherpaurgen Aug 30 '17 at 16:40
  • 2
    You should probably also quote your expansions -- `"$varFile"`, not `$varFile`. And consider just `for varFile in /path/to/files/*; do if grep -q "kolosky@penguin.com" "$varFile"; then echo "$i"; done >match.txt` -- if you put the redirection on the whole loop, then it'll only open the file once, not re-open it every time you want to write a single line. And `grep -q` is more efficient than just `grep`, since it can stop as soon as it finds a match. – Charles Duffy Aug 30 '17 at 16:41
  • 1
    Using `if cmd` rather than `cmd; if [ $? = 0 ]` also makes your code more robust against breaking if you add debug printing or something else that changes `$?`, and reduces the amount of vertical space to be covered by someone trying to read it. (`$?` is really rather rarely needed; `exit $?` or `return $?` are both needless, for instance, because `$?` is the default return/exit value for functions and scripts). – Charles Duffy Aug 30 '17 at 16:44

0 Answers0