I need to know how to grab output and store it in a variable. I don't want to send all the output to the file, just one of the commands output I am sending.. Basically

LIST  <---needs to go to file

I tried.. read $response but it isn't working.

  • Out of many output which one in specific you want to store. Only LIST for one time purpose or you want a script of it. – Pacifist Mar 13 '16 at 18:21

You have to eliminate $:

read response
|improve this answer|||||

nc isn't expect, so you have no control over which parts of the remote host's output are captured. You can redirect the entire output to a file (and process the file later with awk, sed, perl, or whatever to extract only what you need) or pipe it directly into awk (or whatever) before writing to a file or storing it in a shell variable (with $() command substitution).

However, you'd almost certainly be better off writing your script in perl (and use either the Expect.pm module or Net::Telnet, or Net::IMAP) or in python using pexpect (or python's imaplib2 library), or in some other language with an expect-like library or IMAP client library.

That would allow you to connect to the relevant port (IMAP, by the look of it) on the remote host, issue whatever commands you need, wait for responses, and do whatever you need with the response (including saving some of the responses to a file)

I've added the /expect tag to your question - click on it to get a list of expect-related questions and answers on this site.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.