while循环带有意外的bash退出

时间:2016-06-13 19:04:49

标签: bash

问题陈述:

当我使用 After you load the document in XmlDocument available in using System.Xml namespace, you can create mutliple XmlNodeList(s) and then fetch the values. public static void foobar() { string strFile = @"C:\SourceFolder\SampleXML\document-test.xml"; XmlDocument doc = XmlDocument.Load(strFile); if (doc.SelectSingleNode("w:body") != null) { XmlNodeList nodes = doc.SelectNodes(".//w:pPr"); foreach (XmlNode xn in nodes) { XmlNodeList rPr = xn.SelectNodes("w:rPr"); foreach (XmlNode xnrPr in rPr) { if (xnrPr.SelectSingleNode("w:rFonts") != null) { Console.WriteLine(xnrPr.SelectSingleNode("w:rFonts").InnerText.ToString()); } } } } 时,我遇到while循环问题:

set -e

尝试解决方案:

我最初认为这可能是因为测试的退出代码所以我为临时故障排除临时硬连接的真实语句(这仍然不起作用):

 1  #!/bin/bash
 2  # This is a script to perform initial configurations of CentOS 7.2
 3  # Written by Robert J.
 4  
 5  # Exit if unexpected condition
 6  set -e;
 7  
 8  # Simple die function
 9  dieaquickdeath(){
10      code=$1;
11      message=$2;
12      printf "$message\n";
13      exit "$code";
14  }
15  
16  #
17  ## There is an initial prompt here that was removed for simplification.
18  #
19  InputPassword1=1
20  InputPassword2=2
21  
22  # Sanity check for password
23  unset n;
24  while [[ "$InputPassword1" != "$InputPassword2" ]];
25  do
26      # Counter
27      ((n++));
28  
29      # Prompt for new password
30      printf "\nPasswords do not match\n";
31      read -sp "Enter password: " InputPassword1;
32      echo;
33      read -sp "Re-enter password: " InputPassword2;
34  
35      # End loop after passwords match.
36      [[ "$InputPassword1" == "$InputPassword2" ]] &&
37          break;
38  
39      # End loop after 3 attempts
40      [[ "$n" == 3 ]] &&
41          dieaquickdeath 1 "Third failed password attempt";
42  
43  done;

在没有36 [[ "$InputPassword1" == "$InputPassword2" ]] && 37 break || 38 true; 39 40 # End loop after 3 attempts 41 [[ "$n" == 3 ]] && 42 dieaquickdeath 1 "Third failed password attempt" || 43 true; ;

的情况下运行时似乎没有任何问题
set -e

问题:

  • 有谁看到我意外退出的地方?
  • 这是我不知道的一些bash警告的原因吗?

我觉得我长时间一直盯着这个。

提前感谢所有人都很棒。

1 个答案:

答案 0 :(得分:4)

要查看停止位置,请运行bash -x testing

$ bash -x testing
+ set -e
+ InputPassword1=1
+ InputPassword1=2
+ unset n
+ [[ 2 != '' ]]
+ (( n++ ))

它停止,因为n(有效)为零。观察:

$ unset n; ((n++)); echo code=$?
code=1

退出代码1表示失败。因此,set -e终止了执行。

以下两种可能会返回零(成功)退出代码的解决方案:

$ unset n; ((++n)); echo code=$?
code=0
$ unset n; ((n++)) || true; echo code=$?
code=0

一般来说,正如所讨论的hereset -e做了一些奇怪的事情。它的使用是有争议的。