Visit The New, Modern Unix Linux Community


Grepping the file into a variable after SSH into a server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping the file into a variable after SSH into a server
# 1  
Grepping the file into a variable after SSH into a server

Hi All,

When i am logged into a server , i am able to assign grep value to a variable as follows.
Code:
VAR=`grep 'Listener stopped' /logs/perf.log`

However, when i log out of the server, and try to execute the following command by first SSHing into server, it throws error.
Code:
$ VAR=`ssh Server grep 'Listener stopped' /logs/perf.log`
grep: stopped: No such file or directory

How to correct this?

Last edited by Franklin52; 06-19-2014 at 10:34 AM.. Reason: Please use code tags
# 2  
This way:
Code:
VAR=$(ssh Server "grep 'Listener stopped' /logs/perf.log")

# 3  
Thanks a lot . That worked perfectly.

I have another doubt. How to append 2 variables into a new variables adding new line.

i.e.
Code:
var1="x";
var2="y";

I want to display my o/p as
Code:
x
y

When i do this
Code:
var3=$var1$'\n'$var2;

I get o/p as :
Code:
x y


Last edited by Franklin52; 06-19-2014 at 10:35 AM.. Reason: Please use code tags
# 4  
Sounds like you're not quoting the variable on output.

Code:
$ var1="x";
$ var2="y";
$ var3=$var1$'\n'$var2;
$ echo $var3
x y
$ echo "$var3"
x
y

# 5  
Hey that worked great as well. Thanks. I have one more doubt.

I want to retain the result of this variable whenver i am logging into a server . When i am echoing the result of this variable inside server , it is showing blank.

i.e.
Code:
$ var1="x";
$ var2="y";
$ var3=$var1$'\n'$var2;

ssh Server
echo "$var3"
---blank---

Actually, i want to do this as due to some reason, i am not able to send mail from putty terminal. the following command is not working(not receiving mail). It is only working inside the server(receiving mail) . If anyone can shed light on this as well, it would be great.

Code:
echo "$VAR3" | mailx -s "subject" abc@xyz.com


Last edited by Franklin52; 06-19-2014 at 10:36 AM.. Reason: Please use code tags

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #734
Difficulty: Medium
Commodore BASIC was also known as PET BASIC.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grepping for one variable while using awk to parse an associated variable

Im trying to search for a single variable in the first field and from that output use awk to extract out the lines that contain a value less than a value stored in another variable. Both the variables are associated with each other. Any guidance is appreciated. File that contains the... (6 Replies)
Discussion started by: ncwxpanther
6 Replies

2. Shell Programming and Scripting

Delete line from remote file over ssh passing variable

I have a variable called $a1 which maps to something like "http://servername proxy1 count http" and a lots of entries in a file on remote server. If I have the following in my .sh script: sed -i "\%$a1%d" mylog.txtthe line is deleted from mylog.txt. Great. I'm trying now to remvoe this from a... (3 Replies)
Discussion started by: say170
3 Replies

3. Shell Programming and Scripting

How to handle grepping variable data containing wildcards?

I have a lot of files with keywords and unique names. I'm using a shell script to refer to a simple pattern file with comma separated values in order to match on certain keywords. The problem is that I don't understand how to handle the wildcard values when I want to skip over the unique names. ... (5 Replies)
Discussion started by: abercrom
5 Replies

4. Shell Programming and Scripting

Retrieving file size in a remote server using SSH

Hi, I have public and private keys and that's works fine for me. now I am sending files one by one on remote server and I want to check is file successfully delivered or not by comparing size of file on local machine and remote server using ‘stat -c%s'. Below operations need to be done on... (2 Replies)
Discussion started by: ketanraut
2 Replies

5. Shell Programming and Scripting

Grepping for variable in brackets

I have a script which reads a number out of a log file. The pertinent line is this: cat /tmp/listofnumbers When I run cat /tmp/listofnumbers what I am seeing is on each line. I am trying to make the script read from that file and grep for a variable like the following line should do: ... (4 Replies)
Discussion started by: newbie2010
4 Replies

6. Shell Programming and Scripting

Grepping file and returning passed variable if the value does not exist in file at all.

I have a list of fields that I want to check a file for, returning that field if it not found at all in the file. Is there a way to do a grep -lc and return the passed variable too rather then just the count? I am doing some crappy work-around now but I was not sure how to regrep this for :0 so... (3 Replies)
Discussion started by: personalt
3 Replies

7. Shell Programming and Scripting

use awk to ssh from variable in flat file

flat file looks like ooss-pfgg-1234,vol_name_1, mail-list decoded = hostname,volum_name,mail_list each line has diff info am trying to ssh into each fist field, check vol usage for second field, and if greater than 90% send mail to mail-list got the second and third part working, ... (1 Reply)
Discussion started by: riegersteve
1 Replies

8. Shell Programming and Scripting

Grepping Date Variable

Hello, I would like for the user to input the date for a particular log file, then have the input sent to a variable, which is then used via grep to extra the logs for the specific date the user request. I did some searching, but I still don't understand why I'm not seeing any result. ... (4 Replies)
Discussion started by: ravzter
4 Replies

9. UNIX for Dummies Questions & Answers

grepping a variable

I need to pass a parameter that will then be grepped. I need it to grep /paramater and then have a space so if 123 was passed my grep would be grep '/123 ' sample.log this works fine from the command line but when i try and set it searchThis="/$2 " and then run grep $searchThis... (6 Replies)
Discussion started by: magnia
6 Replies

10. Shell Programming and Scripting

ssh text into file on remote server

Hello, I have about 90 servers that I need to update snmp configs. I am trying to write a script that will echo 4 new lines of text into the snmpd.conf file. I have tested it locally and it works when the server ssh into itself but when I try to run the script to ssh into a remote server it logs... (5 Replies)
Discussion started by: mr_dthomas
5 Replies

Featured Tech Videos