bash_profile이 로그인을 통해 얻어지는지 또는 스크립트를 통해 얻어지는지 결정합니까?

bash_profile이 로그인을 통해 얻어지는지 또는 스크립트를 통해 얻어지는지 결정합니까?

파일에 설정된 환경 변수에 의존하는 많은 스크립트가 있습니다 .bash_profile.

또한 서버에 로그인할 때 특정 메시지를 표시하고 싶었는데, 그것도 추가했습니다 .bash_profile. 하지만 스크립트가 호출될 때 이 메시지가 표시되는 것을 원하지 않습니다.

을 사용할 수도 있지만 source ~/.bash_profile > /dev/null 2>&1그렇게 하려면 모든 스크립트를 확인해야 하므로 지루합니다. 따라서 스크립트의 출처를 확인할 수 있는 방법이 있는지 궁금합니다.

내 목표는 다음과 같습니다(의사 코드).

#FROM_TERMINAL should be set to 1 if ran by the user (logging into the server) - otherwise it stays empty.
if [[ $FROM_TERMINAL = 1 ]]; then
    echo "some message"
else
    :
fi

이를 수행하는 우아한 방법이 있습니까?

감사합니다!

답변1

에서는 man bash다음과 같이 말합니다.

An interactive shell is one started without non-option arguments (unless -s is specified) and without the -c option whose standard input and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option.
PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state.

$-플래그에는 쉘 옵션 세트가 포함되어 있으며 이에 대한 설명은 다음과 같습니다. https://tldp.org/LDP/abs/html/internalvariables.html.

$-다음과 같이 변수의 내용을 확인합니다 .

$ echo "$-"
himBHs

다음과 같이 설정되어 있는지 확인합니다 i.

if [[ $- == *i* ]]
then
    # I was called interactively, do the echoing etc.
fi

$PS1var가 설정되어 있는지 확인할 수도 있는데 , 이 방법은 신뢰성이 떨어집니다. 바라보다https://tldp.org/LDP/abs/html/intandnonint.html#IITEST.

관련 정보