LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to pass a variable to grep (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-pass-a-variable-to-grep-4175486737/)

mkanwischer 12-03-2013 03:02 PM

how to pass a variable to grep
 
Good day all...

I've done a lot of searching on this and have yet to stumble across a solution for my situation. I have the following code I'm trying to run in a bash shell:


M_A_C=`cat /etc/ssh/Looking4MAC`
echo "The variable M_A_C got set to $M_A_C"
ssh root@192.168.30.220 'arp -a | grep "$M_A_C" > /root/.ssh/bad_MAC; exit'

The file Looking4MAC has a single MAC address that I'm searching for and the echo line echos the MAC perfectly. However in the ssh line I can't seem to land on a technique to pass the MAC to the grep command. I've tried a combination of single quotes, backslashes and <> symbols to no avail as of yet. Any help would be greatly appreciated.

Regards,
Mark

geeky404 12-03-2013 04:03 PM

Hi,
try this one:

Code:

ssh root@192.168.30.220 'arp -a | grep ' $M_A_C ' > /root/.ssh/bad_MAC; exit'

suicidaleggroll 12-03-2013 04:20 PM

Variables will not expand inside single quotes, that's the advantage of single quotes.
Code:

$ mac="12:34:56:78:9A"
$ echo $mac
12:34:56:78:9A
$ echo "$mac"
12:34:56:78:9A
$ echo '$mac'
$mac

So your ssh command is passing the literal string '$M_A_C' to the arp -a | grep command on the remote machine. Now the remote machine will see $M_A_C and try to expand it like a variable, but that variable doesn't exist on the remote machine, it only exists on the local machine. You need to expand the variable on your local machine in the ssh call, before you get to the remote machine, which means no single quotes around the variable.

mkanwischer 12-03-2013 07:55 PM

Thanks geeky404 Your script works great. I had tried using the following single quote method (with backward slanting single quotes):
ssh root@192.168.30.220 'arp -a | grep `$M_A_C` > /root/.ssh/bad_MAC; exit'
instead of:
ssh root@192.168.30.220 'arp -a | grep '$M_A_C' > /root/.ssh/bad_MAC; exit'

Can someone explain why the backward slanting single quotes didn't work and what the difference is between ` and ' quote usage? Pointing me to a good document would be okay.

Thanks a lot.

suicidaleggroll 12-03-2013 08:06 PM

` and ' are completely different.

` is not a quote, `` induces a subshell, runs the command within ``, and dumps the output in its place.

For example:
Code:

echo `date`
Anything inside the `` will run in a subshell, and the output of that subshell will take its place. It's similar to $(), but $() provides a few notable advantages, namely nesting.

SAbhi 12-03-2013 09:36 PM

Quote:

grep `$M_A_C` > /root/.ssh/bad_MAC; exit'
using backticks (``) to expand a vaiable is not suggested, you are calling a subshell just to expand a variable. use ("") double quotes instead.


All times are GMT -5. The time now is 03:47 PM.