gpt4 book ai didi

linux - bash 脚本在失败时创建环回(使用对话框)

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:56:05 25 4
gpt4 key购买 nike

我正在编写一个脚本,使用对话框来提示用户提出问题等。其中一个步骤是运行一个命令来生成一些信息。我有一个 if 语句以防它失败,我想做的是,以防它无法让用户重新运行它。对如何去做有点迷茫。

脚本本身

#!/bin/bash

#Generating UID

UID_GREP=${UID_GREP=dialog}
$UID_GREP --title "Appliance Imaging Script" --clear \
--yesno "Begin Generate UID for Appliance:" 10 30
case $? in
0)
#uid generate script
/usr/share/bin/create >/tmp/appliance_run.log 2>&1
;;
1)
clear
echo "exiting"
exit;;
255)
clear
echo "ESC Pressed... Exiting"
exit;;
esac

#if Generation of UID fails
if [ $? != 0 ] ; then
UID_FAIL=${UID_FAIL=dialog}
$UID_FAIL --title "UID Genenration failed" --clear \
--yesno "UID failed to genenerate, would you like to try again" 10 30
case $? in
0)
#loopback to $UID_GREP

;;
1)
clear
echo "exiting"
exit;;
255)
clear
echo "ESC Pressed... Exiting"
exit;;
esac

fi

所以基本上,它说“#loopback to $UID_GREP”我想要它,如果选择是则循环回到 UID_GREP 对话框屏幕。

谢谢。

最佳答案

基本上,您应该做的是为您的应用程序创建一个“主要功能”,基本上是一个循环,它将控制应用程序流程。然后将所有对话框分离到单独的功能。并考虑在适当的情况下将实际操作分离到一个单独的函数中。

#!/bin/bash

# Variable that holds information whether we have generated the UID.
uid_generated=0
loop_count=0

generate_uid() {
loop_count=$(( $loop_count + 1 )) #This can be used to check how many times we have failed
echo "generating UI, execution $loop_count" >> log.txt #For testing purposes
/usr/share/bin/create >/tmp/appliance_run.log 2>&1

# Check our result
if [[ $? -eq 0 ]]; then
uid_generated=1
fi

}

initial_dialog() {
UID_GREP=${UID_GREP=dialog}
$UID_GREP --title "Appliance Imaging Script" --clear \
--yesno "Begin Generate UID for Appliance:" 10 30
case $? in
0)
#uid generate script
generate_uid
;;
1)
clear
echo "exiting"
exit
;;
255)
clear
echo "ESC Pressed... Exiting"
exit
;;
esac
}

check_result() {
#if Generation of UID fails
if [ $? != 0 ] ; then
UID_FAIL=${UID_FAIL=dialog}
$UID_FAIL --title "UID Genenration failed" --clear \
--yesno "UID failed to genenerate, would you like to try again" 10 30

case $? in
0)
#loopback to $UID_GREP
generate_uid
;;
1)
clear
echo "exiting"
exit
;;
255)
clear
echo "ESC Pressed... Exiting"
exit
;;
esac
fi
}

# First, show the initial dialog
initial_dialog

# Then we enter our application loop, this might not be exactly what you want, but idea is
# to have variables that control the execution states.
# So you could have eternal while here as well, and other variables define which screen
# should be shown at what time, but to show that this is correct, I do this
while [ $uid_generated -eq 0 ]
do
generate_uid

# Consider showing some other screen, or other information in this dialog
# For example informing user that "we have failed once, and re-generating etc"
check_result
done

echo "UID generation done" #This will end the program

这只是一个示例,但它确实有效。您可以从 log.txt 中查看您执行了多少次 uid 生成。我保留了与您的格式相似的格式,以便您看起来很熟悉并且可以看到不同之处,但您可能希望有更多的外部变量来控制应用程序流程。

请注意,您不再需要在检查结果中检查失败代码,因为错误检查是在 generate_uid 函数中完成的。该功能纯粹是为了通知用户它失败了。但同样,我没有修改它以保持您的原始内容不变。

但我建议将应用程序逻辑与 UI 逻辑分开。

关于linux - bash 脚本在失败时创建环回(使用对话框),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18708597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com