Testing a command's stored exit status with var=$?; [[ $var ]] is always true, whether 0 or 1

前端 未结 2 734
再見小時候
再見小時候 2021-01-16 20:55

Consider:

true; run_backup=$?
if [[ $run_backup ]]; then
  echo \"The user wants to run the backup\"
fi

...and...

false; ru         


        
相关标签:
2条回答
  • 2021-01-16 21:26

    The answer is yes, because neither 0 nor 1 is NULL.

    0 讨论(0)
  • 2021-01-16 21:36

    [[ $run_backup ]] is not a Boolean check; it fails only if its argument is an empty string (which neither 0 nor 1 is).

    Since zenity returns 0 if you click OK, you want something like

    [[ $run_backup -eq 0 ]] && echo "The user wants to run the backup"
    

    or

    (( run_backup == 0 )) && echo "The user wants to run the backup"
    

    or

    # You need to negate the value because success(0)/failure(!=0) use
    # the opposite convention of true(1)/false(0)
    (( ! run_backup )) && echo "The user wants to run the backup"
    

    Based on the fact that run_backup was the exit status of a zenity command in the original question, the simplest thing would be to simply use && to combine zenity and your function into one command.

    zenity --question --width=300 --text "..." && echo "The user wants to run the backup"
    
    0 讨论(0)
提交回复
热议问题