bash - Why would a correct shell script give a wrapped/truncated/corrupted error message? -


i have shell script command seems should work, instead fails odd wrapped/truncated/corrupted error message. example:

$ ls -l myfile -rw-r----- 1 me me 0 aug  7 12:36 myfile $ cat myscript  ls -l myfile $ bash myscript : no such file or directory 

the file exist, if didn't, kind of error message get:

$ ls -l idontexist ls: cannot access idontexist: no such file or directory 

notice how includes tool name ls, message string , filename while mine not.

here's if try use mysql instead. error message looks it's been wrapped, , starts quote:

command:  mysql -h myhost.example.com expected: error 2005 (hy000): unknown mysql server host 'myhost.example.com' (0) actual:   ' (0) 2005 (hy000): unknown mysql server host 'myhost.example.com 

and here's trivial ssh command should work, or @ least give normal error message, instead wrapped start colon , ends strange clobbering:

command:  ssh myhost expected: ssh: not resolve hostname myhost: name or service not known actual:   : name or service not knownname myhost 

why happen, , how fix it?

tl;dr: script or data has windows style crlf line endings.

convert unix style deleting carriage returns.


how check if script or data has carriage returns?

they're detectable ^m in output of cat -v yourscript:

$ cat -v myscript ls -l myfile^m 

if script doesn't have them, data might -- if reading ini/csv files or curl:

hostname=$(curl https://example.com/loginhost.txt) ssh "$hostname"            # shows strange error echo "$hostname" | cat -v  # shows myhost^m 

how remove them?

set editor save file unix line endings, aka "line terminators" or "end-of-line characters", , resave it.

you can remove them command line dos2unix yourscript or cat yourscript | tr -d '\r' > fixedscript.

if found in data, can pipe source through tr -d '\r':

hostname=$(curl https://example.com/loginhost.txt | tr -d '\r') 

why carriage returns cause strange error messages?

the "carriage return" character, aka cr or \r, causes cursor move start of line, , continue printing there. in other words, starts overwriting line start. why wrap strangely:

intended:     ssh: not resolve hostname myhost\r: name or service not known  written:      ssh: not resolve hostname myhost\r overwritten:  : name or service not known               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                   result:       : name or service not knownname myhost 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -