
#!/bin/bash
RET=0
export RET
{
ssh -q -t user@host <<EOF
echo "hello there "
exit 10
EOF
RET=$?
echo "Out is" $RET
} &
echo "RET is $RET"
################## End
Me sale RET 0 OUT es 10
¿Cómo puedo obtener el código de estado de salida correcto en el bloque exterior? Necesito ver el código de salida 10.
Respuesta1
Necesitas ejecutar el comando en primer plano
$ (exit 10)
$ echo $?
10
O si se ejecuta en segundo plano, explícitamente wait
:
$ (sleep 3; exit 10) &
$ wait %% # %% refers to the current (last) job
$ echo $?
10
O especificando el ID del proceso en lugar del número de trabajo para wait
:
$ (sleep 3; exit 10) & pid=$!
$ wait $pid # $! holds the PID of the last background process
$ echo PID $pid exited with code $?