シェルスクリプト出力の違い

シェルスクリプト出力の違い

私はforループがあり、バックグラウンドで16個のスレッドを起動し、次のコマンドに進む前に各スレッドが完了するのを待つ単純なシェルスクリプトを実行しています。

これはシェルです:

total_threads=16

    echo "" 
    echo "Running Web Exclusive Item updates ... "  
    echo "Total Number of threads = " $total_threads
     for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
     do
        echo "Running Thread $i ..  "
        ./run_upd_web_excl_items.sh $i $total_threads &
     done
     wait
     echo "Done."


    echo ""  
    echo "Committing data ... "
    output_val=`$ORACLE_HOME/bin/sqlplus -S $UP <<endofsql
       whenever sqlerror exit 2
       whenever oserror exit 3
       SET HEADING  OFF
       SET FEEDBACK OFF
       SET ECHO     OFF
       set serverout on
      commit;
      /
    exit
    endofsql`
    echo "Done."

実行すると、出力は次のようになります。

~> . ./run_upd_web_excl_items_mitctrl.sh

Running Web Exclusive Item updates ...
Total Number of threads =  16
Running Thread 1 ..
Running Thread 2 ..
Running Thread 3 ..
Running Thread 4 ..
Running Thread 5 ..
Running Thread 6 ..
Running Thread 7 ..
Running Thread 8 ..
Running Thread 9 ..
Running Thread 10 ..
Running Thread 11 ..
Running Thread 12 ..
Running Thread 13 ..
Running Thread 14 ..
Running Thread 15 ..
Running Thread 16 ..
Total time for Thread 3 : 0 minute(s), 22.5 second(s).
Total time for Thread 2 : 0 minute(s), 23.3 second(s).
Total time for Thread 12 : 0 minute(s), 24.3 second(s).
Total time for Thread 8 : 0 minute(s), 24.8 second(s).
Total time for Thread 7 : 0 minute(s), 29.9 second(s).
Total time for Thread 1 : 0 minute(s), 30 second(s).
[1]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
[2]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
[3]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
[7]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
[8]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
[12]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
Total time for Thread 16 : 0 minute(s), 32.1 second(s).
Total time for Thread 4 : 0 minute(s), 32.8 second(s).
[4]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
[16]+  Done                    ./run_upd_web_excl_items.sh $i $total_threads
Total time for Thread 10 : 0 minute(s), 33.2 second(s).
Total time for Thread 13 : 0 minute(s), 33.7 second(s).
Total time for Thread 5 : 0 minute(s), 33.8 second(s).
[5]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
[10]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
[13]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
Total time for Thread 14 : 0 minute(s), 35.6 second(s).
Total time for Thread 6 : 0 minute(s), 36.8 second(s).
[6]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
[14]-  Done                    ./run_upd_web_excl_items.sh $i $total_threads
Total time for Thread 11 : 0 minute(s), 37.7 second(s).
Total time for Thread 9 : 0 minute(s), 37.8 second(s).
[9]   Done                    ./run_upd_web_excl_items.sh $i $total_threads
[11]-  Done                    ./run_upd_web_excl_items.sh $i $total_threads
Total time for Thread 15 : 0 minute(s), 38.8 second(s).
[15]+  Done                    ./run_upd_web_excl_items.sh $i $total_threads
Done.

Committing data ...
Done.

通常、私が期待する出力は次のとおりです。

~> ./run_upd_web_excl_items_mitctrl.sh

Running Web Exclusive Item updates ...
Total Number of threads =  16
Running Thread 1 ..
Running Thread 2 ..
Running Thread 3 ..
Running Thread 4 ..
Running Thread 5 ..
Running Thread 6 ..
Running Thread 7 ..
Running Thread 8 ..
Running Thread 9 ..
Running Thread 10 ..
Running Thread 11 ..
Running Thread 12 ..
Running Thread 13 ..
Running Thread 14 ..
Running Thread 15 ..
Running Thread 16 ..
Total time for Thread 1 : 0 minute(s), 26.5 second(s).
Total time for Thread 10 : 0 minute(s), 27.1 second(s).
Total time for Thread 2 : 0 minute(s), 27.5 second(s).
Total time for Thread 6 : 0 minute(s), 27.9 second(s).
Total time for Thread 3 : 0 minute(s), 27.9 second(s).
Total time for Thread 15 : 0 minute(s), 27.9 second(s).
Total time for Thread 9 : 0 minute(s), 28 second(s).
Total time for Thread 5 : 0 minute(s), 28 second(s).
Total time for Thread 16 : 0 minute(s), 28.1 second(s).
Total time for Thread 8 : 0 minute(s), 30.5 second(s).
Total time for Thread 12 : 0 minute(s), 31 second(s).
Total time for Thread 11 : 0 minute(s), 31.5 second(s).
Total time for Thread 7 : 0 minute(s), 31.9 second(s).
Total time for Thread 14 : 0 minute(s), 32 second(s).
Total time for Thread 13 : 0 minute(s), 32.7 second(s).
Total time for Thread 4 : 0 minute(s), 34.8 second(s).
Done.

Committing data ...
Done.

シェルが次の追加行を印刷する理由はわかりません。

[13]   Done                    ./run_upd_web_excl_items.sh $i  $total_threads

どんなアイデアがありますか?

ベストアンサー1

スクリプトを実行しているからです。シェル(デフォルトのインタラクティブシェル)は、まるでシェルプロンプトに手動で入力したかのように、このファイルのコマンドを処理します。表示されるメッセージは、デフォルトのバックグラウンドプロセス(つまり、デフォルトのシェルから直接開始されたプロセス)が終了したときに通常シェルに表示されるメッセージです。. script_name

特別な理由がない限り、以下を使用してスクリプトを実行することをお勧めします。

sh run_upd_web_excl_items_mitctrl.sh

(または必要に応じて使用bash)または実行可能にしてください。

chmod +x run_upd_web_excl_items_mitctrl.sh

その後実行

./run_upd_web_excl_items_mitctrl.sh

おすすめ記事