ソースbashスクリプトでデバッグモードを実行するには?

ソースbashスクリプトでデバッグモードを実行するには?

現在、2つのファイルがあります。 1つは基本機能で、もう1つはコアロジックです。メイン関数は、関数のようにコードロジックとアクセス権を取得します。しかし、私の質問はコードロジックに問題があることです。デバッグモードでどのように表示できますか?以下は例です。

コードロジック

function logic() {
    #!/bin/bash
     if [[ -f /tmp/sample.txt ]]; then
        echo "hello world"
     fi
}

主な機能ファイル

#!/bin/bash
if [[ -f /tmp/test.txt ]] ; then
      logic
      echo "Done"
fi

出力を実行するとき:

sh -x myscript.sh

++ [[ -f /tmp/test.txt ]]
hello world      ## I need debug output here itself.
++ echo "Done"

ベストアンサー1

関数を呼び出す前にファイルをインポートする必要があります。

#!/bin/bash
source /path/to/codeLogic.sh
if [[ -f /tmp/test.txt ]] ; then
      logic
      echo "Done"
fi

その後、デバッグモードで実行します。

sh -x myscript.sh
+ source /path/to/codeLogic.sh
+ [[ -f /tmp/test.txt ]]
+ logic
+ [[ -f /tmp/sample.txt ]]    ---> this is the execution part of logic function
+ echo 'hello world'
hello world
+ echo Done
Done

おすすめ記事