How to capture the result in a subprocess

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I'm working with telnet memcache and I'm going to use it as example, but I suppouse it can be extended in other subjects.
In the shell the process is



telnet <ip> <port> #this start the subroutine
connected to <ip>
Escape charecter ...
flush_all #My command
OK #the response /ERROR in case it fails
quit #my command
Connection closed by foreign host.
$ echo $?
1


I want to get the OK/ERROR message, but I don't know how. I'm execute the code in a script:



#!/bin/sh
ssh user@ip '
$(
(sleep 2;

FILE="$HOME/example.log"
DATE=$(date "+%d-%m-%Y %H:%M:%S")
echo flush_all

if [ $? == 0 ]; then
RES="Success "
else
RES="Fail "
fi;

echo "$DATE - $RES" >> $FILE;

sleep 2;
echo quit;
) | telnet <ip> 11211);' 2>/dev/null


But it always return a success, even if the command is añskjdflña, and I don't know how to get the difference between OK and ERROR.










share|improve this question





















  • What command are you running again?
    – Raman Sailopal
    Oct 9 '17 at 11:21










  • I want the response to 'flush_all'
    – oootramas
    Oct 9 '17 at 11:30










  • Does the system have expect installed?
    – thrig
    Oct 9 '17 at 14:05










  • No, and I can't install it
    – oootramas
    Oct 9 '17 at 14:20










  • I suspect that this doesn't do what you think it's doing. Note that the test that sets RES runs on the host into which you logged into via ssh and that $? in that context is the exit status of the echo flush_all command when run on that host? That's not checking the exit status of flush_all on the host into which you logged in via telnet.
    – Andy Dalton
    Oct 9 '17 at 18:00














up vote
0
down vote

favorite












I'm working with telnet memcache and I'm going to use it as example, but I suppouse it can be extended in other subjects.
In the shell the process is



telnet <ip> <port> #this start the subroutine
connected to <ip>
Escape charecter ...
flush_all #My command
OK #the response /ERROR in case it fails
quit #my command
Connection closed by foreign host.
$ echo $?
1


I want to get the OK/ERROR message, but I don't know how. I'm execute the code in a script:



#!/bin/sh
ssh user@ip '
$(
(sleep 2;

FILE="$HOME/example.log"
DATE=$(date "+%d-%m-%Y %H:%M:%S")
echo flush_all

if [ $? == 0 ]; then
RES="Success "
else
RES="Fail "
fi;

echo "$DATE - $RES" >> $FILE;

sleep 2;
echo quit;
) | telnet <ip> 11211);' 2>/dev/null


But it always return a success, even if the command is añskjdflña, and I don't know how to get the difference between OK and ERROR.










share|improve this question





















  • What command are you running again?
    – Raman Sailopal
    Oct 9 '17 at 11:21










  • I want the response to 'flush_all'
    – oootramas
    Oct 9 '17 at 11:30










  • Does the system have expect installed?
    – thrig
    Oct 9 '17 at 14:05










  • No, and I can't install it
    – oootramas
    Oct 9 '17 at 14:20










  • I suspect that this doesn't do what you think it's doing. Note that the test that sets RES runs on the host into which you logged into via ssh and that $? in that context is the exit status of the echo flush_all command when run on that host? That's not checking the exit status of flush_all on the host into which you logged in via telnet.
    – Andy Dalton
    Oct 9 '17 at 18:00












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm working with telnet memcache and I'm going to use it as example, but I suppouse it can be extended in other subjects.
In the shell the process is



telnet <ip> <port> #this start the subroutine
connected to <ip>
Escape charecter ...
flush_all #My command
OK #the response /ERROR in case it fails
quit #my command
Connection closed by foreign host.
$ echo $?
1


I want to get the OK/ERROR message, but I don't know how. I'm execute the code in a script:



#!/bin/sh
ssh user@ip '
$(
(sleep 2;

FILE="$HOME/example.log"
DATE=$(date "+%d-%m-%Y %H:%M:%S")
echo flush_all

if [ $? == 0 ]; then
RES="Success "
else
RES="Fail "
fi;

echo "$DATE - $RES" >> $FILE;

sleep 2;
echo quit;
) | telnet <ip> 11211);' 2>/dev/null


But it always return a success, even if the command is añskjdflña, and I don't know how to get the difference between OK and ERROR.










share|improve this question













I'm working with telnet memcache and I'm going to use it as example, but I suppouse it can be extended in other subjects.
In the shell the process is



telnet <ip> <port> #this start the subroutine
connected to <ip>
Escape charecter ...
flush_all #My command
OK #the response /ERROR in case it fails
quit #my command
Connection closed by foreign host.
$ echo $?
1


I want to get the OK/ERROR message, but I don't know how. I'm execute the code in a script:



#!/bin/sh
ssh user@ip '
$(
(sleep 2;

FILE="$HOME/example.log"
DATE=$(date "+%d-%m-%Y %H:%M:%S")
echo flush_all

if [ $? == 0 ]; then
RES="Success "
else
RES="Fail "
fi;

echo "$DATE - $RES" >> $FILE;

sleep 2;
echo quit;
) | telnet <ip> 11211);' 2>/dev/null


But it always return a success, even if the command is añskjdflña, and I don't know how to get the difference between OK and ERROR.







shell-script ssh






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Oct 9 '17 at 10:58









oootramas

115




115











  • What command are you running again?
    – Raman Sailopal
    Oct 9 '17 at 11:21










  • I want the response to 'flush_all'
    – oootramas
    Oct 9 '17 at 11:30










  • Does the system have expect installed?
    – thrig
    Oct 9 '17 at 14:05










  • No, and I can't install it
    – oootramas
    Oct 9 '17 at 14:20










  • I suspect that this doesn't do what you think it's doing. Note that the test that sets RES runs on the host into which you logged into via ssh and that $? in that context is the exit status of the echo flush_all command when run on that host? That's not checking the exit status of flush_all on the host into which you logged in via telnet.
    – Andy Dalton
    Oct 9 '17 at 18:00
















  • What command are you running again?
    – Raman Sailopal
    Oct 9 '17 at 11:21










  • I want the response to 'flush_all'
    – oootramas
    Oct 9 '17 at 11:30










  • Does the system have expect installed?
    – thrig
    Oct 9 '17 at 14:05










  • No, and I can't install it
    – oootramas
    Oct 9 '17 at 14:20










  • I suspect that this doesn't do what you think it's doing. Note that the test that sets RES runs on the host into which you logged into via ssh and that $? in that context is the exit status of the echo flush_all command when run on that host? That's not checking the exit status of flush_all on the host into which you logged in via telnet.
    – Andy Dalton
    Oct 9 '17 at 18:00















What command are you running again?
– Raman Sailopal
Oct 9 '17 at 11:21




What command are you running again?
– Raman Sailopal
Oct 9 '17 at 11:21












I want the response to 'flush_all'
– oootramas
Oct 9 '17 at 11:30




I want the response to 'flush_all'
– oootramas
Oct 9 '17 at 11:30












Does the system have expect installed?
– thrig
Oct 9 '17 at 14:05




Does the system have expect installed?
– thrig
Oct 9 '17 at 14:05












No, and I can't install it
– oootramas
Oct 9 '17 at 14:20




No, and I can't install it
– oootramas
Oct 9 '17 at 14:20












I suspect that this doesn't do what you think it's doing. Note that the test that sets RES runs on the host into which you logged into via ssh and that $? in that context is the exit status of the echo flush_all command when run on that host? That's not checking the exit status of flush_all on the host into which you logged in via telnet.
– Andy Dalton
Oct 9 '17 at 18:00




I suspect that this doesn't do what you think it's doing. Note that the test that sets RES runs on the host into which you logged into via ssh and that $? in that context is the exit status of the echo flush_all command when run on that host? That's not checking the exit status of flush_all on the host into which you logged in via telnet.
– Andy Dalton
Oct 9 '17 at 18:00















active

oldest

votes











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f396984%2fhow-to-capture-the-result-in-a-subprocess%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f396984%2fhow-to-capture-the-result-in-a-subprocess%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?