Bash

Zenity 按鈕不起作用

  • January 26, 2021

所以我不明白為什麼我的取消按鈕在確認刪除 zenity 框上不起作用……你能解釋一下嗎?

directory=$(zenity --entry \
   --text "Enter a path" \
   --title "Delete"\
   --ok-label "Done" )
   ret=$?

   if ((ret==0));then

       if [ -z "$directory" ];then #Check if user entered a path or not.
           directory=$(pwd)    #If not take the actual path.
       fi
       if [ -d "$directory" ];then #Check if entered path exist.
           Spath=$(zenity --file-selection --filename="$directory/" --title "File Browser")
           #Change the directory otherwise will not delete the wanted              file.           
           cd $directory
       fi
       if (($?==0));then
           #Check if the user really want to delete this file.
           zenity --question --icon-name "edit-delete" --title "Confirmation" --text "Are you sure you want to delete this file?"

           #Getting only the file name from the path
           Sfile=$(basename "$Spath")
           echo $?
           clear $?

問題出在這部分,當按下取消時,它遵循“確定”按鈕路徑。

           if (($?==0));then
               rm -f "$Sfile"  #Delete the file.
           elif (($?==1));then
               echo $?
               zenity --error --title "Info" --text "No file was deleted"
           fi      
       fi
       
   else
       #If not existing show error message.
       zenity --error --title "Error" --text "The path you entered does not exist" 
   fi

請記住,這意味著最後一個命令執行$?的返回碼。

所以如果你指的是$?執行後Sfile=$(basename "$Spath"),則$?代表basename命令的狀態。下一個引用是$?指下一個命令。

如果你想測試zenity命令的返回碼,那麼你應該ret=$? 呼叫後立即設置zenity(類似於你在文件開頭已經做過的),然後檢查$retand not的值$?

引用自:https://unix.stackexchange.com/questions/631111