HattrickNZ
HattrickNZ

Reputation: 4643

diff command + cygwin + how to read the output

I am tring to understand how to use the diff command to undersand the difference between these files. Can someone please explain what this means? cases.sh was written in notepad++, cases2.sh was written in nano from within cygwin on windows7. but I cannot see the difference as I don't think it is visible? what is the advice for getting cases.sh to work, but I want to understand what is wrong with it.

$ diff cases.sh cases2.sh
1,13c1,13
< #!/bin/sh
< # Prompt user to enter a character
< echo "Please enter a letter:"
< read charac
< case $charac in
<       "a"|"A") echo "You have typed a vowel!" ;;
<       "e"|"E") echo "You have typed a vowel!" ;;
<       "i"|"I") echo "You have typed a vowel!" ;;
<       "o"|"O") echo "You have typed a vowel!" ;;
<       "u"|"U") echo "You have typed a vowel!" ;;
<       *)         echo "You have typed a consonant!" ;;
< esac
< exit 0
\ No newline at end of file
---
> #!/bin/sh
> # Prompt user to enter a character
> echo "Please enter a letter:"
> read charac
> case $charac in
>       "a"|"A") echo "You have typed a vowel!" ;;
>       "e"|"E") echo "You have typed a vowel!" ;;
>       "i"|"I") echo "You have typed a vowel!" ;;
>       "o"|"O") echo "You have typed a vowel!" ;;
>       "u"|"U") echo "You have typed a vowel!" ;;
>       *)         echo "You have typed a consonant!" ;;
> esac
> exit 0

cases.sh does not work, I wrote this in notepad++ and I get the error below

$ ./cases.sh
Please enter a letter:
t
': not a valid identifier `charac
./cases.sh: line 5: syntax error near unexpected token `$'in\r''
'/cases.sh: line 5: `case $charac in

cases2.sh does work but I wrote this in nano from within cygwin on windows7

$ ./cases2.sh
Please enter a letter:
t
You have typed a consonant!

Upvotes: 1

Views: 1363

Answers (2)

yossarian
yossarian

Reputation: 1667

You're encountering the classic linefeed problem.

Linux and the Unices (including compatibility layers like Cygwin) use a single \n (newline) to terminate each line, while the Windows family uses \r\n (carriage return, newline) for each line.

As a result, many (although not all) Unix utilities will choke on scripts written on Windows, as they contain additional, invisible, carriage returns. In your case, diff knows that cases1.sh differs from cases2.sh on those lines, but doesn't display the \r characters that actually cause the difference. Similarly, bash gets caught up on the carriage returns.

The easiest solution is enable Unix line endings in your editor of choice, and to run a tool like dos2unix on the scripts you've already written. It should be available in Cygwin's repositories.

Upvotes: 0

peter karasev
peter karasev

Reputation: 2614

try turning on "show all characters" in notepad++ ... sounds like you have windows-style newlines set for your default editor settings there. Convert the file to unix-style newlines and try running it.

Upvotes: 1

Related Questions