# grep issue?



## lbl (Apr 7, 2011)

Hi

I'm trying to write a tool to grep syslog messages and filter out all the lines I know.

But I'm running into some problems with extra single qoutes and it dosen't seem to be working.


```
[root@mon ~]# cat sna.conf 
jsavald: ===> dfwdlib_send_msg
jsavald: <=== dfwdlib_send_msg(0)
jsavald: ===> dfwd_ev_client_dispatch
jsavald: <=== dfwd_ev_client_dispatch(0)
[root@mon ~]# cat syslog-alert.bash 
#!/usr/local/bin/bash
# Syslog notification pickup.

conf="/root/sna.conf"

grepline()
{
cat $conf | while read line ; do
	printf " -e \"$line\""
done
}

grepattri=`grepline`

echo "$grepattri"

echo "test line" | grep -v$grepattri
[root@mon ~]# ./syslog-alert.bash 
 -e "jsavald: ===> dfwdlib_send_msg" -e "jsavald: <=== dfwdlib_send_msg(0)" -e "jsavald: ===> dfwd_ev_client_dispatch" -e "jsavald: <=== 
dfwd_ev_client_dispatch(0)"
grep: ===>: No such file or directory
grep: dfwdlib_send_msg": No such file or directory
grep: <===: No such file or directory
grep: dfwdlib_send_msg(0)": No such file or directory
grep: ===>: No such file or directory
grep: dfwd_ev_client_dispatch": No such file or directory
grep: <===: No such file or directory
grep: dfwd_ev_client_dispatch(0)": No such file or directory
[root@mon ~]#
```

When i run the script with -x I get the following output:


```
[root@mon ~]# ./syslog-alert.bash 
+ conf=/root/sna.conf
++ grepline
++ cat /root/sna.conf
++ read line
++ printf ' -e "jsavald: ===> dfwdlib_send_msg"'
++ read line
++ printf ' -e "jsavald: <=== dfwdlib_send_msg(0)"'
++ read line
++ printf ' -e "jsavald: ===> dfwd_ev_client_dispatch"'
++ read line
++ printf ' -e "jsavald: <=== dfwd_ev_client_dispatch(0)"'
++ read line
+ grepattri=' -e "jsavald: ===> dfwdlib_send_msg" -e "jsavald: <=== dfwdlib_send_msg(0)" -e "jsavald: ===> dfwd_ev_client_dispatch" -e 
"jsavald: <=== dfwd_ev_client_dispatch(0)"'
+ echo ' -e "jsavald: ===> dfwdlib_send_msg" -e "jsavald: <=== dfwdlib_send_msg(0)" -e "jsavald: ===> dfwd_ev_client_dispatch" -e "jsavald: 
<=== dfwd_ev_client_dispatch(0)"'
 -e "jsavald: ===> dfwdlib_send_msg" -e "jsavald: <=== dfwdlib_send_msg(0)" -e "jsavald: ===> dfwd_ev_client_dispatch" -e "jsavald: <=== 
dfwd_ev_client_dispatch(0)"
+ echo 'test line'
+ grep -v -e '"jsavald:' '===>' 'dfwdlib_send_msg"' -e '"jsavald:' '<===' 'dfwdlib_send_msg(0)"' -e '"jsavald:' '===>' 
'dfwd_ev_client_dispatch"' -e '"jsavald:' '<===' 'dfwd_ev_client_dispatch(0)"'
grep: ===>: No such file or directory
grep: dfwdlib_send_msg": No such file or directory
grep: <===: No such file or directory
grep: dfwdlib_send_msg(0)": No such file or directory
grep: ===>: No such file or directory
grep: dfwd_ev_client_dispatch": No such file or directory
grep: <===: No such file or directory
grep: dfwd_ev_client_dispatch(0)": No such file or directory
[root@mon ~]#
```

It seems like it's adding single qoutes to the grep command but I don't get why, any advice would be great.

/lbl


----------



## wblock@ (Apr 7, 2011)

99% of the time, it's preferable to use /bin/sh instead of bash.  They are not the same thing on FreeBSD.

It appears you're trying to recreate the -F or -f options for grep(1), but remember there's a difference between a literal string and a regexp pattern.


----------



## lbl (Apr 7, 2011)

Hi,

Thanks for the reply. But it's exactly the same fault and debug (-x) info I get for both sh and bash. I have tested with the different flags and it is the -F flag that I thought about using for a start. I don't get why the extra quotes still get in, it must be some bash/sh thing.

Any more suggestions ?

/lbl


----------



## wblock@ (Apr 7, 2011)

```
#!/bin/sh
conf="sna.conf"

IFS=""
echo "jsavald: <=== dfwd_ev_client_dispatch(0)" | grep -F `cat $conf`
```


----------



## lbl (Apr 8, 2011)

Hi

I dont quite get the empty variable and bash will just make the entire file to one large line if you put it in like that, so it doesn't work.

Why are the single qoutes added?


```
[root@mon ~]# cat syslog-alert.bash 
#!/bin/sh -x
# Syslog notification pickup.

conf="/root/sna.conf"

grepline()
{
cat $conf | while read line ; do
	printf " -e \"$line\""
done
}

#grepattri=`grepline`

#echo "$grepattri"

echo "test line" | grep -F -v `cat $conf`
[root@mon ~]# ./syslog-alert.bash 
+ conf=/root/sna.conf
+ echo 'test line'
+ cat /root/sna.conf
+ grep -F -v jsavald: '===>' dfwdlib_send_msg jsavald: '<===' 'dfwdlib_send_msg(0)' jsavald: '===>' dfwd_ev_client_dispatch jsavald: '<===' 
'dfwd_ev_client_dispatch(0)'
grep: ===>: No such file or directory
grep: dfwdlib_send_msg: No such file or directory
grep: jsavald:: No such file or directory
grep: <===: No such file or directory
grep: dfwdlib_send_msg(0): No such file or directory
grep: jsavald:: No such file or directory
grep: ===>: No such file or directory
grep: dfwd_ev_client_dispatch: No such file or directory
grep: jsavald:: No such file or directory
grep: <===: No such file or directory
grep: dfwd_ev_client_dispatch(0): No such file or directory
[root@mon ~]#
```

Any clues?

/lbl


----------



## wblock@ (Apr 8, 2011)

lbl said:
			
		

> I dont quite get the empty variable and bash will just make the entire file to one large line if you put it in like that, so it dosent work.



Hint: it isn't just a random variable stuck in there.  It works if used as shown; I did test it.  sh(1) almost certainly has more information.



> Why is the single qoutes added ? ...



I think it's just grep showing the expressions with them so the patterns are easier to see in the error message.  The whole while-loop approach has several problems, though.


----------



## phoenix (Apr 8, 2011)

When using *sh -x* to get the shell commands output to the screen, it will add ' ' around different things to show you what is grouped together.  The ' ' are not actually part of the command that gets executed.

If you want to see exactly what is being run, then you need to put your own *echo* commands into the script, or learn to ignore the extra ' ' in the *-x* output.

WYSI*N*WYG when it comes to -x.


----------

