LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-03-2013, 03:02 PM   #1
mkanwischer
LQ Newbie
 
Registered: Dec 2013
Posts: 2

Rep: Reputation: Disabled
Unhappy 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
 
Old 12-03-2013, 04:03 PM   #2
geeky404
LQ Newbie
 
Registered: Nov 2013
Distribution: Debian, Ubuntu
Posts: 2

Rep: Reputation: Disabled
Hi,
try this one:

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

Last edited by geeky404; 12-03-2013 at 04:07 PM.
 
Old 12-03-2013, 04:20 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,453

Rep: Reputation: 2051Reputation: 2051Reputation: 2051Reputation: 2051Reputation: 2051Reputation: 2051Reputation: 2051Reputation: 2051Reputation: 2051Reputation: 2051Reputation: 2051
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.
 
Old 12-03-2013, 07:55 PM   #4
mkanwischer
LQ Newbie
 
Registered: Dec 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Smile

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.
 
Old 12-03-2013, 08:06 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,453

Rep: Reputation: 2051Reputation: 2051Reputation: 2051Reputation: 2051Reputation: 2051Reputation: 2051Reputation: 2051Reputation: 2051Reputation: 2051Reputation: 2051Reputation: 2051
` 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.

Last edited by suicidaleggroll; 12-03-2013 at 08:09 PM.
 
Old 12-03-2013, 09:36 PM   #6
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 664

Rep: Reputation: 81
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] assign full name to variable then grep a file for the variable socalheel Programming 3 09-16-2013 11:04 AM
how to pass this as a variable dinakumar12 Linux - Newbie 17 01-16-2012 02:34 PM
Pass Variable to smbclient artvarck Linux - Newbie 5 02-23-2010 12:49 PM
pass the variable contain lrios Programming 7 04-18-2008 04:56 PM
Pass text to variable Zed Linux - Software 6 05-12-2003 03:02 PM


All times are GMT -5. The time now is 12:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration