スクリプトのデバッグ、-xと-euxo Pipelinefail設定の違いは何ですか?

スクリプトのデバッグ、-xと-euxo Pipelinefail設定の違いは何ですか?

私が知っているスクリプトのデバッグの主な方法は、-xshabang(#!/bin/bash -x)に追加することです。

set -euxo pipefail私は最近、次のようにshabangの下に追加された新しい方法を見つけました。

#!/bin/bash
set -euxo pipefail

これら2つのデバッグ方法の主な違いは何ですか?時には他のものより1つを好むのですか?

新入生として、これを読んだ後、そのような結論を下すことはできません。

ベストアンサー1

まず、-oオプションについて説明します。http://explainshell.com完全に真実ではありません。

これがset組み込みコマンドであることを考慮すると、help以下を実行してその文書を表示できますhelp set

  -o option-name
      Set the variable corresponding to option-name:
          allexport    same as -a
          braceexpand  same as -B
          emacs        use an emacs-style line editing interface
          errexit      same as -e
          errtrace     same as -E
          functrace    same as -T
          hashall      same as -h
          histexpand   same as -H
          history      enable command history
          ignoreeof    the shell will not exit upon reading EOF
          interactive-comments
                       allow comments to appear in interactive commands
          keyword      same as -k
          monitor      same as -m
          noclobber    same as -C
          noexec       same as -n
          noglob       same as -f
          nolog        currently accepted but ignored
          notify       same as -b
          nounset      same as -u
          onecmd       same as -t
          physical     same as -P
          pipefail     the return value of a pipeline is the status of
                       the last command to exit with a non-zero status,
                       or zero if no command exited with a non-zero status
          posix        change the behavior of bash where the default
                       operation differs from the Posix standard to
                       match the standard
          privileged   same as -p
          verbose      same as -v
          vi           use a vi-style line editing interface
          xtrace       same as -x

ご覧のとおり、次のことを-o pipefail意味します。

パイプの戻り値は、ゼロ以外の状態で終了した最後のコマンドの状態、またはゼロ以外の状態で終了したコマンドがない場合はゼロです。

しかしそれは言いません: Write the current settings of the options to standard output in an unspecified format.

すでに-x知っているように、-eデバッグのためにスクリプトの最初のエラーが発生した後、実行は停止します。次のスクリプトを考えてみましょう。

#!/usr/bin/env bash

set -euxo pipefail
echo hi
non-existent-command
echo bye

この行は、 echo byeゼロが返されないため、使用されると実行されません。-enon-existent-command

+ echo hi
hi
+ non-existent-command
./setx.sh: line 5: non-existent-command: command not found

最後の行がない場合は、エラーが発生しても自動的に終了するように指示しなかった-eために印刷されます。Bash

+ echo hi
hi
+ non-existent-command
./setx.sh: line 5: non-existent-command: command not found
+ echo bye
bye

set -e通常、最初のエラーが発生したときにスクリプトが停止するようにスクリプトの上部に配置されます。たとえば、ファイルのダウンロードに失敗した場合、ファイルを抽出する必要はありません。

おすすめ記事