![shell 脚本输出的差异](https://linux22.com/image/55088/shell%20%E8%84%9A%E6%9C%AC%E8%BE%93%E5%87%BA%E7%9A%84%E5%B7%AE%E5%BC%82.png)
我正在运行一个简单的 shell 脚本,它有一个 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.
我无法弄清楚为什么 shell 会打印额外的行,例如:
[13] Done ./run_upd_web_excl_items.sh $i $total_threads
有什么想法吗?
答案1
这是因为您正在运行脚本。 shell(您的主交互式 shell)处理此文件中的命令,就像您在 shell 提示符下手动键入它们一样。您看到的消息是主后台进程(即直接从主 shell 启动的进程)终止时 shell 通常显示的消息。. 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
答案2
原因是因为它们是在您的后台运行的作业当前的壳。
当您使用.
或source
运行脚本时,它不会在子 shell 中运行,就像您直接在终端中键入命令一样。
添加#!/bin/bash
为脚本的第一行并直接运行它,而不是获取它。