3

I want to write shell script, in which i am using ssh command. Whatever output i will get through ssh command i want save this in text file or varibale, so i can use this in my shell script. Currently i am saving output in a variable , but when i used that variable outside ssh command , value is showing blank. Code is

ssh hostname -c "'
`pwd`;
var=$(ps -ef | grep Consumer | cut -f6 -d' ')
'";
echo $?;

echo "vbar  $var";

var value is blank when i print.

2
  • im not sure, but i think this form might help: ssh XXXXXXX > outfile.txt; cat outfile.txt Commented Oct 8, 2014 at 5:49
  • It's blank, because var gets assigned a value in a bash session running on the remote machine (ssh). However, your variable evaluation happens at the local session. Commented Oct 8, 2014 at 7:44

1 Answer 1

7

To save ssh's output in local file "file.log":

ssh hostname > file.log << EOF
pwd
ps -ef | grep Consumer | cut -f6 -d' '
EOF
Sign up to request clarification or add additional context in comments.

2 Comments

How can use one ssh for two commands to save 2 different files. Like one command is ps -ef | grep Consumer | cut -f6 -d' ' and save this output in file.log, second command is ps -ef | grep Test | cut -f7 -d' ' and save output in test.log
Please ask this as a new question. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.