1

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

谢谢你。

4

3 回答 3

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.
于 2013-09-10T00:56:13.087 回答
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
于 2013-09-10T00:43:05.877 回答
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 逻辑分开。

于 2013-09-10T00:51:16.567 回答