
source
(.)またはexecutable
(shebangなど)で始まるかどうかをスクリプト内で確認できますか?
答え1
$0
スクリプトがあるかどうかをテストします。
#!/bin/bash
echo $0
実行可能にして(chmod 755 test.sh
)、次の操作を行います。
source test.sh
bash
あなたは(またはログイン方法とシェルが何であるかによって異なるもの)を得るでしょう。
そうすれば
./test.sh
./test.sh
スクリプトがディスクにどのように保存されるかを知っていると仮定すると、次のことを行う必要があります。
if [ $(basename "$0") == "test.sh" ]
then
..... your code here for non-sourced
else
..... your code here for sourced
fi