0

I am trying to run a batch script (set of R commands saved in a file), from a bash script. For some reason, the statement in the bash script that runs the R script works when I type it manually on the command line, however when the statement is executed in the batch script it fails with the error:

munge_data.sh: line 17: --file=stats.R: command not found

Here is a snippet of the bash script:

#!/bin/bash
SCRIPTDIR=/path/to/some/directory
RBIN=`which R`

cd $SCRIPTDIR

$RBIN --file=stats.R > my.stats.output.txt # <- this is line 17 in my script

Can anyone spot what is causing this problem?

3 Answers 3

4

Apprently, which R returned nothing, probably because R is not in your PATH, so that $RBIN is empty, and the shell tries to run an inexistent command --file=stats.R.

(If R is supposed to be in the PATH, you can just call it R, without using a variable.)

Sign up to request clarification or add additional context in comments.

3 Comments

Hmmm, thats interesting, I am running the bash script from a cron task.; I tested the statements on the command line whilst writing the (bash) script and WHICH R does return the bath to the R binary. As a matter of fact, R is on my path but when running as a cron task, I noticed R was not being found, and thats why I explicitly stated where to find R - and that still appears not to be working - what to do?!
The PATH used by crontab is defined in the crontab file itself and is usually very conservative (i.e., almost empty). You can either add the directory containing R to this PATH, or change the PATH at the begining of your script (preferred), or hardcode the path to the R executable in your script (e.g., by explicitly setting the value of your RBIN variable).
Yup, the explicit path fixed it. Thanks!
1

You don't put the right R call function in Bash.

Running R in command line following below command format (R CMD BATCH file output.file)

#!/bin/bash

R CMD BATCH your_file.R my.stats.output.txt

will get you 99% what you want(Make sure you have execute permission of this bash file)

The reason you didn't get it right is because when execute which R ,the control was transfer from bash file to R program , then the bash script pause the execution .

Comments

1

Nobody mentioned Rscript (comes with R) or littler which predates it:

#!/usr/bin/Rscript

z <- rnorm(5)
# ... other R commands ...

or

#!/usr/bin/r  

z <- rnorm(5)
# ... other R commands ...

both of which you can run directly once you do the usual chmod 755 filename.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.