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.

2 Answers 2

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)

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

Comments

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

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.