^M not found

hi:

I had an existing script in production and I commented some lines and implemented the same. The scripts fails with a cc of 127 and I get the msg:

^M: not found

I checked the file format and it is a PC file format. Does this cause the script to fail with cc= 127.

Can somebody please revert.

Thanks

Try:

dos2unix script_file
1 Like

Yes; don't edit UNIX script files in Notepad. UNIX considers the carriage return, ^M, as part of the filename and fails to find the script interpreter.

Lots of ways to do it. Everyone suggests dos2unix but almost nobody has this utility. What usually works instead is

# remove carriage returns, store new file in outputfile
tr -d '\r' < myfile > outputfile
# check if output file is still okay
less outputfile
# if it is okay, overwrite myfile with it.  cat lets us do it without changing permissions
cat outputfile > myfile
1 Like

For many reasons the "tr" command suggested above is the best method.
Some versions of the "dos2unix" command do more than just change the line terminator (e.g. strip the high-order bit from characters).

For anybody reading this who can't find the "dos2unix" command it is often there under the alternative name "dos2ux". It is not always there at all.

1 Like

I know this thread is a few weeks old, but here is another way to do it just in case anyone else runs into this issue.

From inside of vi:

:%s /[ctl v][ctl m]//g

Dont type out [ctl v] or [ctl m] you actually just use the keyboard combination. so it will look like this in vi.

:%s /^M//g