views:

603

answers:

6

i have a bash script I edit on windows using UltraEdit and save as UTF-8 No BOM, using Unix Line Terminator style, as specified in the "save as" dialog box in UE.

I then upload it to a centos 5 x86 machine running parallels virtuozzo, and everytime, no matter how i upload it (FTP as binary file, or via parallels management console), the script wont work. When i view the script via parallels management console i see that the LAST, and ONLY the last line, has a weird character in it. If i remove the line terminator from the last line (let the script end at the last printable character) everything works fine.

My questions:

A) why is only the LAST line terminator causing problems?

B) how do i avoid this in the future?

thanks!

+1  A: 

firstly i would recommend you transfer as binary.. since you seem to know exactly the format you want..

ASCII/text FTP is known for doing line-ending conversion when you dont want it too.

ShoeLace
if that was the problem it wouldnt happen when i transfer via PMC. unless PMC uses some FTP based transfer and which I dont know about ...
Gaia
transferring as binary doesnt fix, as expected. see comments to sinans answer.
Gaia
+1  A: 

The next step is to do a hexdump on the file and see what is in there. This sounds very much like there is a bare CR in there. Remember that carriage return means "write the next character in position 0 of the current row". Try running hexdump -C file or even od -t x1 file and look for 0A and 0D (0D=CR, 0A=LF).

D.Shawley
it is a bare CR thats in there (^M)the problem is how does it get there? i am killing all the trailing stuff from the last line, then i insert one more newline and save the file, as per the specs above.
Gaia
od -t x1 file returns 0000000 23 21 2f 62 69 6e 2f 62 61 73 68 0d 0a 0000015
Gaia
+1  A: 

you can just run dos2unix to get rid of them..

ghostdog74
i ran the perl script because dos2unix doesnt seem to be available on my distro?
Gaia
-bash: dos2unix: command not found
Gaia
First of all, you can usually say "file filename" to identify when the file has DOS style CRLF line terminators. Then, if you do not have `dos2unix` you can get it in many forms on line for your platform.
nik
+1  A: 

There are two issues here: First, assuming the editor saves with Unix line endings, you should use binary FTP, not ASCII. Second, and this is puzzling, the editor seems to be inserting a CTRL-Z at the end of the file (which has not been necessary since the late eighties AFAIK).

Do save a file consisting of a single line, transfer it to the Unix system via usual means and use:

xxd myscript.sh

to see if the last byte in the file is 0x1a to verify if my theory is correct.

Sinan Ünür
one line script transferred as binary reported`0000000: 2321 2f62 696e 2f62 6173 68 #!/bin/bash`
Gaia
two line returns'0000000: 2321 2f62 696e 2f62 6173 680d 0a #!/bin/bash..'
Gaia
trying to execute the two line shows the extra CR. this is my problem. -bash: ./hello.sh: /bin/bash^M: bad interpreter: No such file or directory (sorry i can't get the backtick right on an internation kbd)
Gaia
A: 

you could just edit the script remotely.

emacs/vim/nano etc. works well over putty. if you like emacs, you can also use tramp to edit remote files with the emacs running on whatever machine you happen to be sitting in front of, though I haven't used tramp with windows. komodo-edit as remote editing as well, and while I highly recommend komodo-edit, i haven't used the remote feature.

ryan_m
sure, but i am trying to find out what is wrong with my current method. it *should* work. thanks
Gaia
+1  A: 

I used CTRL-H on Ultraedit, which displays the same output as unix's XXD command, and I see the 0D 0A in there, though the file is set to use UNIX line terminators. this is an Ultraedit problem. Im using v 13.20a.

I am new to stackoverflow. should i open a new question or re-tag this one?

thanks to all

Gaia
No need to create a new question.. just amend this one with your updates.
Wadih M.