3

I have 2 R scripts in the sub-folders of folder1, and ran the following script. "No such file or directory" error was returened for the R scripts. Does anyone know how to solve this problem? Thanks

Bash script:

#! /usr/bin/env Rscript
for dir in folder1/* ; do
Rscript script1.R
Rscript script2.R
done

3 Answers 3

3

Are script1.R and script2.R inside the subdirectories of folder1? If so, try this:

#! /bin/bash
for dir in folder1/* ; do
    Rscript "$dir"/script1.R
    Rscript "$dir"/script2.R
done

Another way to run all R scripts inside folder1 is to use find:

find folder1 -type f -name "*.R" -exec Rscript {} \;
Sign up to request clarification or add additional context in comments.

Comments

3

You #! specifies R, not bash. And your loop is wrong.

Maybe try this:

#!/bin/bash Rscript
for dir in folder1/script*.R ; do
    echo "Now running $dir"
    Rscript folder1/$dir
done

Comments

0

You never changed the directory, so you should either switch to the new subfolder before executing the scripts or execute them using the full path.

Thus, try to replace

Rscript script1.R
Rscript script2.R

either by

cd folder1/$dir; Rscript script1.R; cd -
cd folder1/$dir; Rscript script2.R; cd -

or

Rscript folder1/$dir/script1.R
Rscript folder1/$dir/script2.R

Also, your shebang is wrong, it should be

#!/bin/bash

or something similar if you want your script to be a shell script. Right now it is executed as an R script.

1 Comment

It is possible to make this in windows

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.