diff --git a/src/kscript b/src/kscript index 97bc5158..1fbede4a 100755 --- a/src/kscript +++ b/src/kscript @@ -27,5 +27,8 @@ if [[ $(uname) == CYGWIN* ]]; then jarPath=$(cygpath -w ${jarPath}); fi ## prefer KOTLIN_HOME instead of PATH to resolve `kotlin` location (see #145) if [[ -z "$KOTLIN_HOME" ]]; then KOTLIN_EXEC="kotlin"; else KOTLIN_EXEC="$KOTLIN_HOME/bin/kotlin"; fi +## expose the name of the script being run to the script itself +export KSCRIPT_FILE="$1" + ## run it using command substitution to have just the user process once kscript is done eval "exec $(${KOTLIN_EXEC} -classpath ${jarPath} kscript.app.KscriptKt "$@")" diff --git a/test/resources/uses_self_file_name.kts b/test/resources/uses_self_file_name.kts new file mode 100755 index 00000000..bd8668d0 --- /dev/null +++ b/test/resources/uses_self_file_name.kts @@ -0,0 +1,2 @@ +#!/usr/bin/env kscript +println("Usage: ${System.getenv("KSCRIPT_FILE")} [-ae] [--foo] file+") \ No newline at end of file diff --git a/test/test_suite.sh b/test/test_suite.sh index c72a4d2b..a948d784 100755 --- a/test/test_suite.sh +++ b/test/test_suite.sh @@ -115,6 +115,10 @@ assert "unset KOTLIN_HOME; echo 'println(99)' | kscript -" "99" ## todo test what happens if kotlin/kotlinc/java/maven is not in PATH +## run script that tries to find out its own filename via environment variable +f="${KSCRIPT_HOME}/test/resources/uses_self_file_name.kts" +assert "$f" "Usage: $f [-ae] [--foo] file+" + assert_end environment_tests