我正在尝试从bash脚本启动erl。当我从变量erl传递eval表达式内容时无法启动。当eval选项是直接的 在erl命令行中给出,erl启动没有任何问题。 怎么做对吗?
#!/bin/bash
echo "-----------------------------------"
#
# Fails
EVAL_EXP1="\"ok, observer:start(), ok\""
echo $EVAL_EXP1
erl -eval $EVAL_EXP1
#
# Fails
echo "-----------------------------------"
EVAL_EXP2="ok, observer:start(), ok"
echo $EVAL_EXP2
erl -eval $EVAL_EXP2
#
# Works
echo "-----------------------------------"
erl -eval "ok, observer:start(), ok"
脚本输出
13:30 $ ./start.sh
-----------------------------------
"ok, observer:start(), ok"
Crash dump is being written to: erl_crash.dump...done
System process <0.0.0> terminated: {function_clause,
[{init,prepare_run_args,[{eval,[<<"\"ok,">>,<<"observer:start(),">>,
<<"ok\"">>]}],[]},{init,map,2,[]},{init,boot,1,[]}]}
./start.sh: line 7: 26573 Aborted (core dumped) erl -eval $EVAL_EXP1
-----------------------------------
ok, observer:start(), ok
(no error logger present) error: <0.0.0>
Crash dump is being written to: erl_crash.dump...done
System process <0.0.0> terminated: {function_clause,
[{init,prepare_run_args,[{eval,[<<"ok,">>,<<"observer:start(),">>,
<<"ok">>]}],[]},{init,map,2,[]},{init,boot,1,[]}]}
./start.sh: line 13: 26599 Aborted (core dumped) erl -eval
$EVAL_EXP2
-----------------------------------
Erlang/OTP 18 [erts-7.0] [source] [64-bit] [smp:4:4] [async-threads:10]
[kernel-poll:false]
Eshell V7.0 (abort with ^G)
1>
答案 0 :(得分:2)
您需要将变量替换用引号括起来,以便它们作为单个参数发送到erl
:
EVAL_EXP2="ok, observer:start(), ok"
erl -eval "$EVAL_EXP2"