Quantcast
Channel: User Kamil Maciorowski - Super User
Viewing all articles
Browse latest Browse all 837

Answer by Kamil Maciorowski for grep with 'or' not working in cygwin bash script

$
0
0

Hypothesis

Your whois-list.sh contains Windows/DOS line endings (denoted CRLF, \r\n) instead of Unix line endings (denoted LF, \n).

The extra character (denoted CR, \r) is not recognized by the shell interpreting the script as a part of the line terminator; it is recognized as a part of input, so it gets "glued" to the last argument passed to egrep.

In effect egrep does not see …|^Domain Name, it sees …|^Domain Name\r where \r denotes the character you did not mean to pass to egrep. The output from whois never matches this pattern.


To confirm

If file whois-list.sh tells you … with CRLF line terminators then this is the case.

Alternatively and without additional tools (like file), you can try to put a line like this into the script:

whois customerdomain.com | egrep -e ^'Name Server|^Domain Name' #

where everything after # (including \r, if any) will be seen as a comment. If this line works and the same line without # misbehaves then it's a strong clue \r is there.

In general not every possible line of shell code can be "fixed" this way. This is not a good fix, just a simple way to diagnose the problem.


What about shebang?

In Linux a trailing \r after #!/bin/bash would make the shebang misbehave in the first place. Either this line alone is fine in your script, or Cygwin does some fixing here.


Fix

A common tool to convert files from Windows/DOS line endings to Unix line endings is dos2unix:

dos2unix whois-list.sh

(Other possibilities: here). Some GUI text editors can convert the file for you, even in Windows (e.g. Notepad++). Fixing the line endings in whois-list.sh is the right thing to do.

Do not use Windows-centric text editors to edit scripts for Cygwin (or Linux/Unix). Make sure the editor you use can and does save your scripts with Unix line endings.


Viewing all articles
Browse latest Browse all 837

Trending Articles