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 |
Hi,
try this one: Code:
ssh root@192.168.30.220 'arp -a | grep ' $M_A_C ' > /root/.ssh/bad_MAC; exit' |
Variables will not expand inside single quotes, that's the advantage of single quotes.
Code:
$ mac="12:34:56:78:9A" |
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. |
` 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` |
Quote:
|
All times are GMT -5. The time now is 03:47 PM. |