Issue
I am trying to check that status of a prior command in list of commands sent to a server to change the password. When I just echo $?
it works. when I store it to a variable like STATUS=$?
and then echo $STATUS
I get a blank line. And when I place the echo $?
inside my if loop then returns something weird also. Below is the code of the ssh command for any advise on how to get this fixed.
ssh [email protected] "printf '%s\n%s' "$PASSWORD" "$PASSCONFIRM" | passwd; STATUS=$?; echo $STATUS; if (( $STATUS == 0 )); then printf 'Password Changed Successfully\n'; else printf 'Password Change Unsuccessful\n'; fi; sleep 1;"
Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
Password Change Unsuccessful
bash: ((: == 0 : syntax error: operand expected (error token is "== 0 ")
Solution
You will need to scape the variables you want to use in the remote server.
$PASSWORD
and $PASSWORDCONFIRM
are fine, those are used in the local server.
$STATUS
and $?
you want then to be evaluated in the remote server, so you will need to scape them: STATUS=\$?
, echo \$STATUS
and if (( \$STATUS == 0))
Answered By - Tiago Lopo Answer Checked By - David Marino (WPSolving Volunteer)