1

I am trying to start erl from bash script. When I pass the eval expression content from a variable erl fails to start. When eval option is directly given in erl command line, erl starts without any issues. How to do correct ?

#!/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"

The script output is

   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> 
DevMinz
  • 23
  • 3
  • Solved for my purpose using multiple -eval arguments passed to erl. Got solution from similar post http://stackoverflow.com/questions/16871832/erlang-invoking-erl-eval-from-command-line-never-exits . Still not sure why above **failed** methods didn't work – DevMinz Jul 23 '16 at 08:20

1 Answers1

2

You need to wrap the variable substitution in quotes so that they're sent as a single argument to erl:

EVAL_EXP2="ok, observer:start(), ok"
erl -eval "$EVAL_EXP2"
Dogbert
  • 212,659
  • 41
  • 396
  • 397