I am trying to run a command similar to
> system("cat <(echo $PATH)")
which fails when run from within R or Rstudio with the following error message:
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `cat <(echo $PATH)'
However, if I run this on the command line it works fine:
$ cat <(echo $PATH)
[...]/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
I checked that the shell I am using is bash using system("echo $SHELL"). Can anyone help me solving this?
This syntax works in bash but not sh. The $SHELL environment variable doesn't necessarily mean that is the shell being used. echo $0 will show your shell.
system("echo $0")
#> sh
You could force bash to be used like this
system("bash -c 'cat <(echo $PATH)'")
#> /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
Related
Regard this distilled example of the problem.
% echo 'echo "$((1.5*2))"' | zsh
3.
% echo 'echo "$((1.5*2))"' >x
% <x zsh
3.
% echo $SHELL
/bin/zsh
% chmod +x x
% ./x
./x: line 1: 1.5*2: syntax error: invalid arithmetic operator (error token is ".5*2")
I understand this error in bash.
If I change the script to just
echo "$SHELL"
it emits /bin/zsh as expected when executed. So I have no reason to expect bash behaviour here.
Someone explain why zsh is acting like sh/bash but identifying as zsh? I'm not interested in a work around (I have several), I want to understand this. Thanks.
Short answer: zsh is not executing the script; bash is.
$SHELL is the name of your login shell, not the currently executing shell. Unlike bash, which will use itself to execute a script with no shebang, zsh uses /bin/sh; from man zshmisc:
If execution fails because the file is not in executable format, and
the file is not a directory, it is assumed to be a shell script.
/bin/sh is spawned to execute it.
In your case, /bin/sh is a shell (either bash or dash, most likely) that does not support floating-point arithmetic.
I have created below .sh file to run R code saved in separate .R file.
cat EE.sh
#!/bin/bash
VARIABLES=( 20190719 20190718 )
for i in ${VARIABLES[#]}; do
VARIABLENAME=$i
/usr/lib/R/bin/Rscript -e 'source("/home/EER.R")'
Basically what it is expected to do is, take the dates from VARIABLE and pass to the /home/EER.R file, and R will do execution based on passed date (after correct formatting)
Then I ran below code
sudo chmod a+rx EE.sh
and
sudo bash EE.sh
But I then get below error message.
sudo bash EE.sh
EE.sh: line 2: $'\r': command not found
EE.sh: line 3: $'\r': command not found
EE.sh: line 4: $'\r': command not found
Can anyone help me to resolve this issue.
I am using Ubuntu 18 with R version 3.4.4 (2018-03-15)
This problem looks to be related to carriage returns related(which come when we copy text from windows machine to unix machine), so to identify them use:
cat -v Input_file
If you see carriage returns in your file then try:
tr -d '\r' < Input_file > temp && mv temp Input_file
Once they are removed then try to run your program.
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
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
How to knit an Rmd document in a job on an LSF cluster?
I tried this:
bsub -q normal -E 'test -e /nfs/users/nfs_c/username' -R "select[mem>20000] rusage[mem=20000]" -M20000 -o DOWNLOAD.o -e DOWNLOAD.e -J DOWNLOAD Rscript -e "rmarkdown::render('code/12downloadSeq.Rmd')"
The above command does not work. I get the following error:
Error in strsplit(version_info, "\n")[[1]] : subscript out of bounds
Calls: <Anonymous> ... pandoc_available -> find_pandoc -> lapply -> FUN -> get_pandoc_version
Execution halted
On the other hand, a script like the following to run a normal R script works:
bsub -q normal -E 'test -e /nfs/users/nfs_c/cr7' -R "select[mem>20000] rusage[mem=20000]" -M20000 -o RSCRIPT.o -e RSCRIPT.e -J RSCRIPT Rscript code/setup.R
I imagine there is a problem with the quotation marks but I do not know how to solve it.
I am trying to call a perl script in R using the system command
This is what I tried:
system("perl R/BEMSER.pl -f R/fasta.txt -m R/matrix.txt -o R/result.txt -tu 100 -tl -100 -c", intern=F)
I get the following error message:
Warning message:
In system("perl R/BEMSER.pl -f R/fasta.txt -m R/matrix.txt -o R/result.txt -tu 100 -tl -100 -c", :
perl not found
The command inside the system call works in the command line (giving parameters to the perl script, the files perl should use are in a special folder called "R"), perl is of course installed.
What can I do to call the perl script from R?