0

I am working on a batch script that produces the deployment shell script according to my input, and I found my generated shell script has EOL \r\n, so I need to add a procedure to convert this file to use \n. I was looking for a solution in native batch but failed, so now I am trying to do it in other approaches.

The input is a typical shell script of commands with \r\n as EOL.

The output should be the same shell script with \n as EOL.

Any method that can be done in Windows environment will be appreciated.

devildelta
  • 119
  • 1
  • 4
  • Possible duplicate of [Anything like DOS2Unix for Windows?](https://stackoverflow.com/questions/20368781/anything-like-dos2unix-for-windows) – Pratham Dec 05 '18 at 07:06
  • DOS2Unix seems to be a good answer of it but I would not consider it duplicated, as I am also looking for the solution that can have least extra dependency added to the script. That is, I am trying to use everything available in my environment to achieve the target, and I believe the way to achieve is not unique. – devildelta Dec 05 '18 at 10:01
  • 1
    So something like here : https://stackoverflow.com/questions/40803502/echo-text-with-unix-line-endings-from-a-windows-batch-bat-script – Pratham Dec 05 '18 at 10:11

1 Answers1

0

nodejs approach:

const fs = require('fs');
fs.readFile('apply_patchx.sh','utf8',(err,data)=>{
    fs.writeFile('apply_patch.sh',data.replace(/\r?\n/g,"\n"),()=>{
        console.log('converted');
    });
});

Call by node rn2n.js, input embedded in script.

Java approach:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class RN2N {
    public static void main(String[] args){
        try {
            BufferedReader br = new BufferedReader(new FileReader("apply_patchx.sh"));
            BufferedWriter bw = new BufferedWriter(new FileWriter("apply_patch.sh"));
            for(String line; (line = br.readLine()) != null;){
                bw.write(line.replaceAll("\\\\r\\\\\n", "\\\\n"));
            }
            br.close();
            bw.flush();
            bw.close();
            System.out.println("converted");
        } catch (IOException e) {
            System.exit(1);
        }
    }
}

Call by java RN2N, input embedded in script.

devildelta
  • 119
  • 1
  • 4