cygwin - Shell script works in the console but not when saved as a text file -
cygwin - Shell script works in the console but not when saved as a text file -
consider simple shell script:
#!/bin/sh fruitlist="apple pear tomato peach grape" fruit in $fruitlist if [ "$fruit" = "tomato" ] || [ "$fruit" = "peach" ] echo "i ${fruit}es" else echo "i ${fruit}s" fi done
when paste cygwin window works fine when save text file test.sh , run cygwin terminal i'm getting this:
$ ./test.sh ./test.sh: line 4: syntax error near unexpected token `$'do\r'' '/test.sh: line 4: `do
however, if remove newlines works:
#!/bin/sh fruitlist="apple pear tomato peach grape" fruit in $fruitlist if [ "$fruit" = "tomato" ] || [ "$fruit" = "peach" ] echo "i ${fruit}es" else echo "i ${fruit}s" fi done
how can create script more readable maintaining new lines in file, \n
doesn't seem work.
your \r
characters come windows file, new lines defined \r\n
. in unix new line defined \n
, hence \r
stays "orphan" , causes these problems.
what can convert file unix mode command dos2unix
.
more info in: does windows carriage homecoming \r\n consist of 2 characters or 1 character?:
two characters combined represent new line on windows. whereas on linux, \n
represents new line. moves cursor start of new line on linux. on windows, cursor remain @ same column in console on next line.
\r
carriage return; \n
line feed. shell cygwin newline
Comments
Post a Comment