これが私のスクリプトの内容です
#!/bin/bash
# test.sh
# Note: Set permissions on this script to 555 or 755,
# then call it with ./test.sh or sh ./test.sh.
echo
echo "This line appears ONCE in the script, yet it keeps echoing."
echo "The PID of this instance of the script is still $$."
# Demonstrates that a subshell is not forked off.
echo "==================== Hit Ctl-C to exit ===================="
sleep 1
exec $0 # Spawns another instance of this same script
#+ that replaces the previous one.
echo "This line will never echo!" # Why not?
exit 99 # Will not exit here!
# Exit code will not be 99!
これは、/ bin/bashを使用してスクリプトを実行したときの出力です。
[user@localhost ~]$ /bin/bash test.sh
This line appears ONCE in the script, yet it keeps echoing.
The PID of this instance of the script is still 4872.
==================== Hit Ctl-C to exit ====================
test.sh: line 11: exec: test.sh: not found
これは、/ bin/shを使用してスクリプトを実行したときの出力です。
[user@localhost ~]$ /bin/sh ./test.sh
This line appears ONCE in the script, yet it keeps echoing.
The PID of this instance of the script is still 4934.
==================== Hit Ctl-C to exit ====================
This line appears ONCE in the script, yet it keeps echoing.
The PID of this instance of the script is still 4934.
==================== Hit Ctl-C to exit ====================
これを止めるにはCtl-Cを使わなければなりませんでした。
同じスクリプトが異なる実行モードに基づいて異なる動作をするのはなぜですか。参考:次のようなスクリプトを実行したCtl-Cを使用する必要がありました。./test.sh