Artificial intelligent assistant

Returning a value from a script running on a slave machine I have a script (on my local machine). In the script, a connection is established via ssh to a slave machine and it runs a build script on the slave: ssh $user@$slave_ip bash $dest_root/$dest_dir/slave_run Is there a way to capture the exit code that is returned from the slave_run script in a parent script variable?

Your command (with added double quotes):


ssh "$user@$slave_ip" bash "$dest_root/$dest_dir/slave_run"


The capture the exit code will be in `$?` after the execution of that command.

Example:


$ ssh someserver sh -c 'false'
$ echo $?
1


To capture it:


$ ssh someserver somecommand
$ code=$?


Alternatively, get the command on the server to output the exit code and capture it as a string:


$ code=$( ssh server sh -c "somecommand; echo \$?" )


In your case:


$ code=$( ssh "$user@$slave_ip" sh -c "$dest_root/$dest_dir/slave_run; echo \$?" )


It's necessary to escape the `$` of `$?` as the command is within double quotes and we'd like `$?` to be evaluated by the shell on the server side, not by the shell on the client.

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 029b42585497cb8ed7185ce5518e976f