6

I'm trying to run a script I made in Rstudio on a unix server (command entered bellow). When I run the command, it returns the following error message:

sbatch -N 1 --mem=10000 -p all ./myscript.R

sbatch: error: Batch script contains DOS line breaks (\r\n) instead of expected UNIX line breaks (\n)

How can I resolve this error? I have already had to enter "#!/usr/bin/env Rscript #SBATCH --get-user-env" to get the ssh server to realize it is an R script.

Thank you!

Kenster
  • 23,465
  • 21
  • 80
  • 106
Jautis
  • 459
  • 6
  • 13
  • 2
    If your OS has the `dos2unix` command line tool, run it by doing `cp ./myscript.R ./myscript.bkp; dos2unix ./myscript.R`. It will convert all line breaks to `\n`. – flodel Sep 10 '14 at 22:57
  • 1
    Take a look on answer on [Line endings change editor/app for the whole project](http://stackoverflow.com/questions/19859491/) how to convert the line endings for 1 or more files quickly using a text editor. And nearly all editors on Windows and Linux support a conversion of the line termination type before save or on using **Save As**. There is usually a list box option in the **Save As** dialog for selecting DOS/Windows, UNIX or MAC format. – Mofi Sep 11 '14 at 06:56
  • @flodel: it doesn't have a dox2unix command function. – Jautis Sep 11 '14 at 13:57
  • @Jautis what is your Unix flavor as an answer may depend on it? Either way [this answer](https://stackoverflow.com/a/2613834/7598113) or one of the surrounding might help you as well. – kuza Nov 29 '17 at 18:38
  • This nice short article on wikipedia [dos2unix](https://en.wikipedia.org/wiki/Unix2dos) could be helpful. Otherwise, Notepad ++ is very handy - see [this](https://stackoverflow.com/a/8195860/5193830) – Valentin_Ștefan Apr 12 '19 at 15:24
  • `dos2unix script_name.R` should convert. If this is not preinstalled then `$ sed -i 's/^M//' script_name.R` – banikr Nov 15 '22 at 19:04

1 Answers1

1

The solutions proposed in the comments used external programs (dos2unix or editors). Here is a way to make the changes in R. You can read the file containing the script and the write it back out using Unix-style newlines. The help page ?file specifies that opening the connection using open="wb" specifies writing in 'binary mode'. That will make \n write out as \n, not \r\n.

Text = paste(readLines("Script.R"), collapse="\n")
conx = file("Script.UnixNL.R", open="wb")
write(Text, conx)
close(conx)
G5W
  • 36,531
  • 10
  • 47
  • 80