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

时间:2013-09-09 23:28:41

标签: linux bash

我正在使用对话框编写脚本以提示用户提问等。其中一个步骤是运行命令以生成一些信息。我有一个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

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

谢谢。

3 个答案:

答案 0 :(得分:1)

将所有内容放在while循环中。如果您成功运行create,请使用 break命令离开循环。第二个case语句可以嵌套在第一个语句的默认子句中,如果用户选择再次尝试,则可以简单地通过循环重复循环。

while true; do
    #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
          break
          ;;  
        1) clear; echo "exiting"; exit
           ;;
        255) clear; echo "ESC Pressed... Exiting"; exit
           ;; 

        *)  # All other non-zero exit statuses
            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
              1) clear; echo "exiting"; exit
                 ;;
              255) clear; echo "ESC Pressed... Exiting"; exit
                 ;;
            esac
            ;;
    esac
done

答案 1 :(得分:1)

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

#!/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逻辑分开。

答案 2 :(得分:1)

也许这是您需要的格式:

#!/bin/bash

# Generating UID

while :; do
    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
        # 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)
                continue 2
                ;;
            1)
                clear
                echo "exiting"
                exit
                ;;
            255)
                clear
                echo "ESC Pressed... Exiting"
                exit
                ;;
            *)
                # What do we do with other codes?
                ;;
            esac
        fi
        break
        ;;
    1)
        clear
        echo "exiting"
        exit
        ;;
    255)
        clear
        echo "ESC Pressed... Exiting"
        exit
        ;;
    *)
        # What do we do with other codes?
        ;;
    esac
done

# Continues here after success. All other failure or cancellations make the code exit.
相关问题