所以我不明白为什么我的取消按钮在确认删除 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
答案1
请记住,这$?
意味着返回代码最后命令运行。
$?
因此,如果您在运行后引用Sfile=$(basename "$Spath")
,则$?
表示命令的状态basename
。下一个参考$?
引用下一个命令。
如果你想测试zenity
命令的返回码,那么你应该设置ret=$?
就在之后调用 to zenity
(与您在文件开头所做的类似),然后检查$ret
and not的值$?
。