如何在 escript 程序中获取程序名称?
How to get the program name in an escript program?
我有一个 Erlang 程序 运行 使用 escript
:
% filename: myscript.erl
-export([main/1]).
main(Args) ->
io:format("Args: ~p~n", [Args]).
当我运行escript myscript.erl 123 456
时,打印出来的是:
Args: ["123","456"]
这很好,但是程序名称在哪里(即 myscript.erl
)?
在 C 中,例如,在 int main(int argc, char *argv[]) { ... }
中,argv[0]
总是包含程序的名称。如何在 Erlang 中获取程序的名称?
来自 erlang escript docs:
To retrieve the pathname of the script, call escript:script_name()
from your script (the pathname is usually, but not always, absolute).
这是实际操作:
myescript.erl:
-export([main/1]).
main(Args) ->
io:format("Args: ~p~n~p~n", [Args, escript:script_name()]).
在 bash shell:
~/erlang_programs$ escript myescript.erl 123 456
Args: ["123","456"]
"myescript.erl"
和:
~/erlang_programs$ cd
~$ escript erlang_programs/myescript.erl 123 456
Args: ["123","456"]
"erlang_programs/myescript.erl"
所以,不管文档怎么说,我得到了一个相对于我发出 escript
命令的目录的路径,或者等同于我提供给 escript
命令的路径。
Why is the pathname surrounded by double quotes? How can it be removed?
在 erlang 中,术语 "a"
是列表 [97]
的 shorthand 符号。同样,术语 "erlang_programs/myescript.erl"
对于列表 [101, 114, ...108]
是 shorthand。每次看到 shell 打印出带双引号的内容时,您都必须对自己重复一遍。 shell 输出双引号字符串而不是它们真正表示的列表这一事实是 shell 的一个可怕特性,并且会导致初学者和有经验的错误者等人难以言喻的困惑。 “嘿,让我们打印出学生在最后三场考试中的原始分数,它们是 [97,98,99]
:
9> io:format("Student scores: ~p~n", [[97,98,99]]).
Student scores: "abc"
ok
Wtf??!
以下是一些删除输出引号的方法:
1)用io:format/2
输出时,可以使用控制序列~s:
s
: Prints the argument with the string syntax. The argument is ...
an iolist(), a binary(), or an atom()... The characters are printed
without quotes.
例如:
1> io:format("~s~n", ["hello"]).
hello
ok
2>
(功劳归功于评论中的操作员!)
2) 您可以将列表(例如"abc")转换为原子:
~$ erl
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V9.3 (abort with ^G)
1> list_to_atom("a").
a
2> list_to_atom([97]).
a
3> "a" =:= [97].
true
我有一个 Erlang 程序 运行 使用 escript
:
% filename: myscript.erl
-export([main/1]).
main(Args) ->
io:format("Args: ~p~n", [Args]).
当我运行escript myscript.erl 123 456
时,打印出来的是:
Args: ["123","456"]
这很好,但是程序名称在哪里(即 myscript.erl
)?
在 C 中,例如,在 int main(int argc, char *argv[]) { ... }
中,argv[0]
总是包含程序的名称。如何在 Erlang 中获取程序的名称?
来自 erlang escript docs:
To retrieve the pathname of the script, call
escript:script_name()
from your script (the pathname is usually, but not always, absolute).
这是实际操作:
myescript.erl:
-export([main/1]).
main(Args) ->
io:format("Args: ~p~n~p~n", [Args, escript:script_name()]).
在 bash shell:
~/erlang_programs$ escript myescript.erl 123 456
Args: ["123","456"]
"myescript.erl"
和:
~/erlang_programs$ cd
~$ escript erlang_programs/myescript.erl 123 456
Args: ["123","456"]
"erlang_programs/myescript.erl"
所以,不管文档怎么说,我得到了一个相对于我发出 escript
命令的目录的路径,或者等同于我提供给 escript
命令的路径。
Why is the pathname surrounded by double quotes? How can it be removed?
在 erlang 中,术语 "a"
是列表 [97]
的 shorthand 符号。同样,术语 "erlang_programs/myescript.erl"
对于列表 [101, 114, ...108]
是 shorthand。每次看到 shell 打印出带双引号的内容时,您都必须对自己重复一遍。 shell 输出双引号字符串而不是它们真正表示的列表这一事实是 shell 的一个可怕特性,并且会导致初学者和有经验的错误者等人难以言喻的困惑。 “嘿,让我们打印出学生在最后三场考试中的原始分数,它们是 [97,98,99]
:
9> io:format("Student scores: ~p~n", [[97,98,99]]).
Student scores: "abc"
ok
Wtf??!
以下是一些删除输出引号的方法:
1)用io:format/2
输出时,可以使用控制序列~s:
s
: Prints the argument with the string syntax. The argument is ... an iolist(), a binary(), or an atom()... The characters are printed without quotes.
例如:
1> io:format("~s~n", ["hello"]).
hello
ok
2>
(功劳归功于评论中的操作员!)
2) 您可以将列表(例如"abc")转换为原子:
~$ erl
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V9.3 (abort with ^G)
1> list_to_atom("a").
a
2> list_to_atom([97]).
a
3> "a" =:= [97].
true