Szymon Błaszczyński
Szymon Błaszczyński

Reputation: 329

bash: return exit code from function and print out from her, but not the exit code

I have a function like this

function backup {
  do_backup
  printf "success"
  do_more_backup
  echo "success"
  if ... ; then
    echo 1
  else
    echo 0
  fi
}

And I have a conditional statement like that

if [[ ! $(backup) ]]; then
...

I can't get the output from function into the shell, but it's evaluating all commands - I've checked in debug mode. I want to evaluate the function, so it does her purpose and in a meanwhile print out some log messages, then to check on exit code of this function if everything succeed. How to approach this problem in bash? And how to avoid printing out last echo, which is a status, or an exit code of a function for evaluation purposes in conditional statement.

Upvotes: 0

Views: 696

Answers (1)

Laurence Geng
Laurence Geng

Reputation: 434

please use return 1 return 0 not echo

Upvotes: 3

Related Questions