2

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?

3 Answers 3

3

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:

if WINDOWS
  exit /b 3
else
  exit 3

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.

3

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 you exit /b 3 the return value gets passed to the shell that is run by cmd /c and when it exits you just get a 0 returned to your ssh and therefore $? is 0.

I created a simple batch file in c:\temp\b.bat

@echo off
exit /b 3 

running the batch file like this

ssh -l admin host 'cmd /c c:/temp/b.bat'

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.

1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .