I have the following case :-
I write a batch file bbb
in Windows 2003 and put a return value = 3 by exit /b 3
then I execute this batch file from Unix by this command :- ssh -l admin host 'cmd /c start bbb'
but when I print the return value I get ( 0 ) not ( 3 ). I print this value by echo $?
. Now how can I get a return value "exit code" from Windows batch?
You can't use the exact code for bash and batch at the same time.
My guess would be the script follows a conditional, something like:
That's all pseudocode, I'm not sure what works for both systems.
Edit: Or you could go the easier route and write up two entirely different script files; job.bat and job.sh.
I was intrigued by this so I did a bit of research. I think that using the
start
command is causing you problems - start causes the command to run in another shell. When youexit /b 3
the return value gets passed to the shell that is run bycmd /c
and when it exits you just get a0
returned to yourssh
and therefore$?
is 0.I created a simple batch file in c:\temp\b.bat
running the batch file like this
returned 3 to $? as required.
Note: the use of
/
instead of\
in the command above.My test environment was copssh 3.1.4 on Windows Vista but I would expect it to work the same in your 2k3 environment.
I think what is happening is your command is returning the exit code from ssh itself, and not the command ssh is executing. I could be wrong, but I don't think there's a way to make ssh pass through the return code of anything it executes remotely.
You probably need to make your "bbb" bash file echo some text if something went wrong, and then collect and check for that text.