1

I have the following script:

rstest

text=$1

cmd="Rscript -e \"a='$1'; print(a)\""
echo $cmd
$cmd

This is the output I get when I run it:

balter@spectre3:~$ bash rstest hello
Rscript -e "a='hello'; print(a)"
Error: unexpected end of input
Execution halted

However, if I run the echoed command directly, it runs fine:

balter@spectre3:~$ Rscript -e "a='hello'; print(a)"

[1] "hello"

I would like to understand why this is. I've tried various combinations of quoting the bash variables and adding eval. But that doesn't seem to be the issue.

EDIT

I tried the answer given below, but get a different result!

balter@spectre3:~$ cat rstest
text=$1

cmd="Rscript -e \"a=$1; print(a)\""
echo $cmd
eval $cmd

balter@spectre3:~$ bash rstest
Rscript -e "a=; print(a)"
Error in cat("pointing to conda env:", env_name, "and lib location", lib,  :
  argument "env_name" is missing, with no default
Calls: startCondaEnv -> cat
Execution halted
abalter
  • 9,663
  • 17
  • 90
  • 145

1 Answers1

1

Below script worked for me.

text=$1

cmd="Rscript -e \"a='$1'; print(a)\""
echo $cmd
eval $cmd

Removing eval gave the same error you posted.

Rscript -e "a='Hello'; print(a)"
Error: unexpected end of input
Execution halted
  • Welcome to Stackoverflow! I added to the original post trying to run what you gave. I get the same error! I'm running this in Ubuntu 18.10. You? – abalter May 15 '19 at 16:04
  • Mint 19.1 and R 3.6.0; i think after you used `eval` you changed `cmd` and forget to add the apostrophe around `$1`. – Hisham Al Kayed May 15 '19 at 20:29