This is basically Analogue of NotebookDirectory[] for scripts (to get the directory path of my script.m)? or Write a mathematica script that returns the absolute path to its directory or How to set the relative directory so that they work both in script and in notebook and portable?, except that none of the answers given there work when my file starts with
#!/usr/bin/env wolframscript
and I run it as ./file.wls
.
For example, this file:
$ cat file.wls
#!/usr/bin/env wolframscript
Print["$InputFileName: '"<>$InputFileName<>"'"];
Print["$Input: '"<>$Input<>"'"];
Print["NotebookDirectory[]: '"<>NotebookDirectory[]<>"'"];
$ ./file.wls
$InputFileName: ''
$Input: ''
FrontEndObject::notavail:
A front end is not available; certain operations require a front end.
StringJoin::string:
String expected at position 2 in NotebookDirectory[]: '<>$Failed<>'.
StringJoin[NotebookDirectory[]: ', $Failed, ']
$ echo |wolframscript
Wolfram Language 11.2.0 Engine for Linux x86 (64-bit)
Copyright 1988-2017 Wolfram Research, Inc.
In[1]:= In[1]:=
How do I make this work?