【问题标题】:Writing exit code of shell script into a file将shell脚本的退出代码写入文件
【发布时间】:2013-02-15 12:59:52
【问题描述】:

我有一个运行工具的 shell 脚本。

我想要的是,这个shell脚本的退出代码应该写在.XMLfile中。

到目前为止我做了什么

# command1
# command2    
echo "<status>" > /home/buser/ABC/status.xml   // Writing into XML    
cp2foss -f ABC -q all /srv/foss/$archive_file2 --user foss --password foss; 
echo $? >>  /home/buser/ABC/status.xml   /// Wrting exit code into XML
echo "</status>" >> /home/buser/ABC/status.xml

它工作得很好,但我认为它不是一个好的 cosing 实践。

如何在 XML 文件中编写退出代码而不发生这种违规行为?

【问题讨论】:

标签: xml shell exit-code


【解决方案1】:

怎么样

# command1
# command2    
cp2foss -f ABC -q......
echo "<status>$?</status>" > path/to/status.xml

【讨论】:

    【解决方案2】:

    上面的代码没有违规,但是你可以这样写:

    cp2foss -f ABC -q all /srv/foss/$archive_file2 --user foss --password foss
    printf '<status>%d</status>\n' "$?" > /home/buser/ABC/status.xml
    

    【讨论】:

      【解决方案3】:

      你可能更喜欢这样写,因为它更干净,更容易理解。

      { 
          echo "<status>" 
          cp2foss -f ABC -q all /srv/foss/$archive_file2 --user foss --password foss;
          echo $? 
          echo "</status>" 
      } > /home/buser/ABC/status.xml   # Writing into XML
      

      【讨论】:

        猜你喜欢
        • 2013-01-26
        • 2019-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多