Wednesday, July 31, 2013

bash while loop, function and ssh

Yesterday I struggled an entire morning with a while loop inside a bash script.
The loop ended even if there was a lot of work to do.
And the while loop ended only when I was calling a function inside another function.

The function contained an ssh call to a remote host to get a result.
Such ssh connection, opening a shell on the remote host, maybe killed my while environment?

The solution:

remote() {
  echo -e "Checking if it is the first time we zfs send to remote $1"
 
  ssh $sshparam zfs list $1 </dev/null &>/dev/null 
 
  EL=$?
  if [ $EL -eq 0 ]
  then
    return 1
  else
    return 0
  fi
} 

</dev/null &>/dev/null