LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-26-2018, 09:27 PM   #1
kumarjohn
LQ Newbie
 
Registered: Oct 2012
Posts: 26

Rep: Reputation: Disabled
grep exact string over ssh with variables


I am trying to execute a script which grep the mount from fstab. when script is executed it gives multiple mounts matching the words instead of exact word matching.

Code:
#!/bin/bash
FNAME=$1
NFSSERVER=$2
NFSSHARE=$3
FILEPATH=/home/kumar/files

[ $# -ne 3 ] && { echo "Usage: ./$0 FILENAME NFSSHARE NFSSERVER "; exit 1; }

for I in `cat $FILEPATH/$FNAME|tr ":" "\n"`
do
  echo | tee -a $FILEPATH/$NFSSERVER-$NFSSHARE-fstab.txt
  echo -e "$I ~ \c" | tee -a $FILEPATH/$NFSSERVER-$NFSSHARE-fstab.txt
  sshpass -f $FILEPATH/.pass ssh -q $I grep -w $NFSSHARE /etc/fstab | grep -i $NFSSERVER | tee -a $FILEPATH/$NFSSERVER-$NFSSHARE-fstab.txt
done
If I search for "apps_stage" NFSSHARE then the output comes for apps_stage, apps_stage_new, apps_stage_log. But the expected output is only for apps_stage.

When I run the same as a command in shell it works perfectly right.

Code:
NFSSHARE=apps_stage
NFSSERVER=nfs-1
I=testserver-1

sshpass -f ./files/.pass ssh -q $I  "grep -w $NFSSHARE /etc/fstab | grep -i $NFSSERVER"
The above command gives me the exact required output as below.

Quote:
nfs-1:/apps_stage /apps nfs _netdev,nolock,rsize=1048600,wsize=1048600 0 0
Not sure why it's not working in a script and working in the shell. Request you to guide me to make if work in script.

Regards, KJ.
 
Old 11-27-2018, 12:59 AM   #2
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,778

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Quote:
Originally Posted by kumarjohn View Post

Code:
  sshpass -f $FILEPATH/.pass ssh -q $I grep -w $NFSSHARE /etc/fstab | grep -i $NFSSERVER | tee -a $FILEPATH/$NFSSERVER-$NFSSHARE-fstab.txt
Code:
sshpass -f ./files/.pass ssh -q $I  "grep -w $NFSSHARE /etc/fstab | grep -i $NFSSERVER"
Those two commands are not the same...pretty sure the entire command following the ssh should be quoted...(but then, when I want to run a command/script on a remote server I usually put it ON the remote server)
 
Old 11-27-2018, 01:07 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,070

Rep: Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132
you can move tee outside of the loop.:
Code:
for I in `cat $FILEPATH/$FNAME|tr ":" "\n"`
do
  echo
  echo -e "$I ~ \c"
  sshpass -f $FILEPATH/.pass ssh -q $I "cat /etc/fstab" | grep -i "${NFSSERVER}.*${NFSSHARE}"
done | tee -a $FILEPATH/$NFSSERVER-$NFSSHARE-fstab.txt
 
Old 11-28-2018, 12:45 AM   #4
kumarjohn
LQ Newbie
 
Registered: Oct 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by scasey View Post
Those two commands are not the same...pretty sure the entire command following the ssh should be quoted...(but then, when I want to run a command/script on a remote server I usually put it ON the remote server)
I tried making both same. still, have the same issue. Output has all the nfs shares starting with apps_stage
 
Old 11-28-2018, 12:48 AM   #5
kumarjohn
LQ Newbie
 
Registered: Oct 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
you can move tee outside of the loop.:
Code:
for I in `cat $FILEPATH/$FNAME|tr ":" "\n"`
do
  echo
  echo -e "$I ~ \c"
  sshpass -f $FILEPATH/.pass ssh -q $I "cat /etc/fstab" | grep -i "${NFSSERVER}.*${NFSSHARE}"
done | tee -a $FILEPATH/$NFSSERVER-$NFSSHARE-fstab.txt
I tried this suggestion also still have the same output. It matches all the entries starting with apps_stage in fstab instead of one line.
 
Old 11-28-2018, 01:00 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,070

Rep: Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132
would be nice if you could investigate yourself a bit. Or, if you wish to get a complete solution please post some examples what is ok, what went wrong ....
 
Old 11-28-2018, 07:11 AM   #7
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 854

Rep: Reputation: 286Reputation: 286Reputation: 286
Can we have a look at $FILEPATH/$FNAME?
 
  


Reply

Tags
bash, linux


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
Exact string match in multi line string Arun Shankar Programming 2 06-19-2014 09:54 AM
grep question - match exact string Panagiotis_IOA Linux - General 2 01-20-2014 04:34 AM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
grep the exact string only ZAMO Linux - General 11 08-28-2008 05:08 AM
grep ?? can grep us variables? DaFrEQ Linux - Software 4 09-14-2005 12:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:21 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