简体   繁体   中英

Generate and execute R, Python, etc.., script from within bash script

I have been trying to find a solution for this for a while but haven't found anything satisfactory yet. I write a lot of bash scripts, but sometimes I want to use R or Python as part of the script. Right now, I end up having to write two scripts; the original bash script to perform the first half of the task, and the R or Python script to perform the second half of the task. I call the R/Python script from within the bash script.

I am not satisfied with this solution because it splits my program over two files, which increases the chances of things getting out of sync, more files to track, etc.. Is there a way to write a block of text that contains the entirety of my R/Python script, and then have bash spit it out into a file and pass arguments to it & execute it? Is there an easier solution? This is more complicated than passing simple one-liners to R/Python because it usually involves creating and manipulating objects over several steps.

There are probably lots of solutions, but this one works:

#!/bin/bash
## do stuff
R --slave <<EOF
  ## R code
  set.seed(101)
  rnorm($1)
EOF

If you want the flexibility to pass additional bash arguments to R, I suggest:

#!/bin/bash
## do stuff
R --slave --args $@ <<EOF
  ## R code
  set.seed(101)
  args <- as.numeric(commandArgs(trailingOnly=TRUE))
  do.call(rnorm,as.list(args))
EOF
  • this allows a flexible number of arguments, but assumes they will all be numeric
  • it also assumes that all parameters will be passed through from the bash script to the R sub-script

obviously you could relax these, eg refer to parameters positionally

Here's an example for python, similar to Ben Bolker 's answer

#!/bin/bash
python << EOF
print 'Hello $1'
EOF

output:

> sh test.sh "world"

Hello world

-----------------------------------------------

You can pass variables from bash to python too:

for ((i=1;i<=$1;i++)) do
python << EOF
print 'Hello world $i times'
EOF
done

output:

> sh test.sh 5

Hello world 1 times
Hello world 2 times
Hello world 3 times
Hello world 4 times
Hello world 5 times

Importing pandas, numpy, matplotlib, and others for normal jobs works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM