Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux bash脚本在失败时创建环回(使用对话框)_Linux_Bash - Fatal编程技术网

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

Linux bash脚本在失败时创建环回(使用对话框),linux,bash,Linux,Bash,我正在编写一个脚本,使用对话框向用户提示问题等。其中一个步骤是运行命令生成一些信息。我有一个if语句,如果它失败了,我想做的是,如果它不能给用户重新运行它的能力。对怎么做有点迷茫 脚本本身 #!/bin/bash #Generating UID UID_GREP=${UID_GREP=dialog} $UID_GREP --title "Appliance Imaging Script" --clear \ --yesno "Begin Generate UID for Appli

我正在编写一个脚本,使用对话框向用户提示问题等。其中一个步骤是运行命令生成一些信息。我有一个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”我想要它,如果选择了yes,则返回到UID_GREP对话框屏幕


谢谢。

只要把所有东西都放到
循环中即可。如果成功运行
create
,请使用 中断命令以离开循环。第二个
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

基本上,您应该为应用程序创建一个“主函数”,基本上是一个循环,用于控制应用程序流。 然后将所有对话框分隔开以分隔函数。并考虑将实际操作分离为一个单独的函数,适用于.< /P>
#!/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函数中完成的,因此不再需要在check result中检查故障代码。该功能纯粹是通知用户它失败了。。但是,我并没有修改它来保持你原来的内容不变


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

也许这就是您需要的格式:

#!/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.

是否可以通过将每行缩进4个空格而不是使用
引号格式字符来重新格式化代码?读起来有点难。现在正在写,呵呵,对不起,没想到会这么难看。。。很高兴了解4个空格…
x=${x=foo}
可以工作,但看起来有点笨拙。我更喜欢
x=${x-foo}
:${x=foo}
。还考虑替换空字符串,例如,<>代码:${x:= fo} 。BTW,我使用了很多这一点,唯一的问题是,循环计数器对每个循环计数两次,因为有两个条目用“GeaType UID”。但除此之外,太棒了,谢谢你给了我正确的方向是的,我并没有检查太多的bug,我只是想展示一些如何为应用程序构建主循环的方法。将UI与实际的操作逻辑分离是一种很好的编程方式,虽然bash有一些限制,但您仍然可以使用变量或临时文件(我的首选)。如果您感兴趣,可以从
模型视图控制器
-模式中了解更多信息。但是,如果有帮助的话,我很高兴