Wikipedia:Reference desk/Archives/Computing/2017 April 25

From Wikipedia, the free encyclopedia
Computing desk
< April 24 << Mar | April | May >> April 26 >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


April 25[edit]

Copying a Bash script from Windows into WSL (CMD) adds carriage returns[edit]

I copy a Bash script from a .sh file stored in Windows10 into nano in WSL (CMD). Yet, the script is copied with carriage returns and these break script execution in WSL. Before copying, I access the script file with Notepad++.

The more the CMD window (tty) is narrower, the more carriage returns are created when the script is created.

I start nano with the following syntax (.sh give Bash highlighting) and the CR characters are seen as little Green boxes:

nano ~/ses.sh && chmod +x ~/ses.sh && sh ~/ses.sh && rm ~/ses.sh

Any ideas? Ben-Yeudith (talk) 12:30, 25 April 2017 (UTC)[reply]

Windows always prefers 0D 0Ah for a new line in text files. Use Notepad++. NPP also allows to replace the \r\n by \n when setting the checkboxes in the find and replace dialog box, using CTRL+H. && is a new command in the same line, accepted by Linux and Windows. --Hans Haase (有问题吗) 12:41, 25 April 2017 (UTC)[reply]
Too bad I didn't mention that (edited to mention): I copy the script TO Nano FROM Notepad++, yet Carriage returns are still created as per the length of the CMD TTY window. This seems unrelated to the particular text editor I copy from, user:Hans Haase. Ben-Yeudith (talk) 12:55, 25 April 2017 (UTC)[reply]
Once using a Windows editor, the CRs are inserted. Remove them manually by STRG+H and replace \r\n by \n, using the checkboxes insode the replace dialog box. How did You transfer the file from Linux to Windows? --Hans Haase (有问题吗) 13:06, 25 April 2017 (UTC)[reply]
I have no problem to remove them in Notepad++, the problem is that they are inserted when the script conent is pasted in nano (to be later saved in a file and executed inside WSL), hence, they are included inside the nano script. When I do search and replace in nano on either \r\n or \r I find nothing but that doesn't really matter because I am looking for a way to prevent Windows to add these in the pasting anyway. Ben-Yeudith (talk) 13:15, 25 April 2017 (UTC)[reply]
The transfer is with WSL. You can copy and paste anything you want in the WSL window and there shouldn't be any problem with that naturally, the point is working cross platform easily with WSL. Ben-Yeudith (talk) 13:17, 25 April 2017 (UTC)[reply]
Adjust the checkboxes inside the find and replace dialog! --Hans Haase (有问题吗) 14:42, 25 April 2017 (UTC)[reply]
Again, my problem is not with Notepad++. This has nothing to do to any GUI editor in Windows. Ben-Yeudith (talk) 16:59, 25 April 2017 (UTC)[reply]
when you say copy, do you mean you copy and paste text from the Windows clipboard into nano which is running in a console window? if you do cat > file.txt (paste, then Ctrl-D) do you still get extra line breaks? Asmrulz (talk) 10:36, 26 April 2017 (UTC)[reply]
HI sSer:Asmrulz: Yes, that's what I mean, and the last timre I tried that in the method of (cat > script.sh AND_THEN paste AND_THEN CTRL+D it did work without problems). Ben-Yeudith (talk) 11:12, 27 April 2017 (UTC)[reply]
Then I guess it's nano. Asmrulz (talk) 11:45, 27 April 2017 (UTC)[reply]
From (very cursory) googling nano seems to be inserting hard line breaks by default [1][2] Normally, editors should just break lines for display (so you don't have to scroll horizontally) but not insert linebreaks into the file when saving. Try to alias nano to nano -w. Is it some remote thing where the editor is running on a remote machine and the TTY is on yours? If so, find a way to copy files to the remote machine without having to paste text into an editor, such as with scp, netcat or something Asmrulz (talk) 11:57, 27 April 2017 (UTC)[reply]
Sadly, User:Asmrulz, it happens when I paste the same code in heredocuments as well, not only with regular scripts in Nano... If I use cat > script.sh AND_THEN paste AND_THEN CTRL+D, it works with heredocuments as well, but if I don't, they too fail. — Preceding unsigned comment added by 79.178.144.67 (talk) 13:02, 27 April 2017 (UTC)[reply]
I'm not sure how to fix it in nano, but I do have some notes on how to fix this using vi:

To remove the ^M characters at the end of all lines in vi, use:

 :%s/^V^M//g

The ^V is a CONTROL-V character and ^M is a CONTROL-M. When you type this, it will look like this:

 :%s/^M//g

Hope this helps. Random character sequence (talk) 17:19, 27 April 2017 (UTC)[reply]

For solution see here. — Preceding unsigned comment added by Ben-Yeudith (talkcontribs) 16:41, 28 April 2017 (UTC)[reply]