2

I would like to pass the output of my R file to a bash script. The output of the R script being the title of a video: "Title of Video"

And my bash script being simply:

youtube-dl title_of_video.avi  https://www.youtube.com/watch?v=w82a1FTere5o88

Ideally I would like the output of the video being "Title of Video".avi

I know I can use Rscript to launch an R script with a bash command but I don't think Rscript can help me here.

ML_Enthousiast
  • 1,147
  • 1
  • 15
  • 39

2 Answers2

5

In bash you can call a command and use its output further via the $(my_command) syntax.

Make sure your script only outputs the title.

E.g.

# in getTitle.R
cat('This is my title')   # note, print would have resulted in an extra "[1]"

# in your bash script:
youtube-dl $(Rscript getTitle.R) http://blablabla

If you want to pass arguments to your R script as well, do so inside the $() syntax; if you want to pass those arguments to your bash script, and delegate them to the R script to handle them, you can use the special bash variables $1 (denote the 1st argument, etc), or $* to denote all arguments passed to the bash script, e.g.

#!/bin/bash
youtube-dl $(Rscript getTitle.R $1) http://blablablabla

(this assumes that getTitle.R does something to those arguments internally via commandArgs etc to produce the wanted title output)

Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57
0

You can call Rscript in bash script and you might assign the output of the R script to a variable in bash script. Check this question. After that you can execute

youtube-dl $outputFromRScript  https://www.youtube.com/watch?v=w82a1FTere5o88
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
aekiratli
  • 518
  • 7
  • 18