Search code examples
rcarriage-return

Running R file using sh/bash


I have created below .sh file to run R code saved in separate .R file.

cat EE.sh
#!/bin/bash

VARIABLES=( 20190719 20190718 )

for i in ${VARIABLES[@]}; do
        VARIABLENAME=$i
        /usr/lib/R/bin/Rscript -e 'source("/home/EER.R")'

Basically what it is expected to do is, take the dates from VARIABLE and pass to the /home/EER.R file, and R will do execution based on passed date (after correct formatting)

Then I ran below code

sudo chmod a+rx EE.sh

and

sudo bash EE.sh

But I then get below error message.

sudo bash EE.sh
EE.sh: line 2: $'\r': command not found
EE.sh: line 3: $'\r': command not found
EE.sh: line 4: $'\r': command not found

Can anyone help me to resolve this issue.

I am using Ubuntu 18 with R version 3.4.4 (2018-03-15)


Solution

  • This problem looks to be related to carriage returns related(which come when we copy text from windows machine to unix machine), so to identify them use:

    cat -v Input_file
    

    If you see carriage returns in your file then try:

    tr -d '\r' < Input_file > temp && mv temp Input_file
    

    Once they are removed then try to run your program.