문제

How can I tell in an ash script if it is running "sourced" or "normal"? By sourced I mean using the "." or "source" command to launch the script in the current shell.

도움이 되었습니까?

해결책

Not sure if it's the best option (will not work if the script has the same name as the shell), but you can check the first parameter ($0). Example:

$ cat test.sh
#!/bin/ash
echo "Value: $0"


$ ./test.sh
Value: ./test.sh

$ source test.sh
Value: ash

If you want to check if the file was sourced, you can use something like this:

#!/bin/ash
case $0 in
    ash) echo "Sourced" ;;
      *) echo "Not sourced" ;;
esac
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top