これを試してみて、探しているものが実行されるかどうかを確認してください。
#!/bin/sh
if [ $_ != $0 ]
then
echo interactive;
else
echo noninteractive;
fi
アンダースコア($_
)は、スクリプトの呼び出しに使用される絶対パス名に展開されます。ゼロ($0
)は、スクリプトの名前に展開されます。それらが異なる場合、スクリプトはインタラクティブシェルから呼び出されました。Bashでは、の後続の展開により$_
、前のコマンドに展開された引数が与えられます(値$_
を保持するために、の値を別の変数に保存することをお勧めします)。
差出人man bash
:
0 Expands to the name of the shell or shell script. This is set
at shell initialization. If bash is invoked with a file of com‐
mands, $0 is set to the name of that file. If bash is started
with the -c option, then $0 is set to the first argument after
the string to be executed, if one is present. Otherwise, it is
set to the file name used to invoke bash, as given by argument
zero.
_ At shell startup, set to the absolute pathname used to invoke
the shell or shell script being executed as passed in the envi‐
ronment or argument list. Subsequently, expands to the last
argument to the previous command, after expansion. Also set to
the full pathname used to invoke each command executed and
placed in the environment exported to that command. When check‐
ing mail, this parameter holds the name of the mail file cur‐
rently being checked.