Reputation: 3429
Im having trouble with cygwin not running my script file as expected
here is my code
#!/bin/sh
echo "Ja eller nei:"
read svar
if ["$svar" = "ja"]; then
echo "Du er Positiv"
exit 0
elif ["$svar" = "nei"]; then
echo "Du er Negativ"
exit 0
else
echo "Du er Nøytral"
fi
and here is my outputin cygwin
$ /cygdrive/c/MyScripts/Bash.sh
Ja eller nei:
ja
': not a valid identifierh.sh: line 6: read: `svar
/cygdrive/c/MyScripts/Bash.sh: line 10: syntax error near unexpected token `elif'
'cygdrive/c/MyScripts/Bash.sh: line 10: `elif ["$svar" = "nei"]; then
its the 3 last error lines i dont understand per say. i understand where the error's are but not why they are there.
BTW the reason im saying some is
for i in {1..100}; do echo $i; done
works properly
Upvotes: 0
Views: 2460
Reputation: 263617
read svar
': not a valid identifierh.sh: line 6: read: `svar
I'll bet the actual error message is something along the lines of:
`svar\r': not a valid identifer ...
Your script has Windows-style "\r\n"
line endings. Cygwin's /bin/sh
requires Unix-style "\n"
line endings. The shell sees the "read svar"
line, not as "read svar"
followed by end-of-line, but as "read svar"
followed by a CR ('\r'
) character followed by end-of-line. It tries to interpret "svar\r"
as an identifier, and complains that it's invalid.
EDIT : I see that another answer already mentioned this. I'm going to leave my answer here because I suggest some ways to verify the problem and to avoid it.
Try running /cygdrive/c/MyScripts/Bash.sh | cat -A
to see what the error messages really look like.
Use something like dos2unix
(I think that's the right command name) to fix the script's line endings. (Read the man page first; unlike most Unixish filter commands, it replaces the named file rather than writing to stdout.)
One way to avoid this problem in the future is to compose your scripts using one of the text editors provided by Cygwin (vim, emacs, nano, whichever one you prefer). If you want to use a Windows editor (notepad, Notepad++, ...), there might be a way to configure it to write Unix-style line endings.
Upvotes: 0
Reputation: 185680
Correct me if I'm wrong, but cygwin is bash. So try this instead :
#!/bin/bash
echo "Ja eller nei:"
read svar
if [[ "$svar" == "ja" ]]; then
echo "Du er Positiv"
exit 0
elif [[ "$svar" == "nei" ]]; then
echo "Du er Negativ"
exit 0
else
echo "Du er Nøytral"
fi
And to be sure you have a proper version, do this :
wget http://pastie.org/pastes/2880899/text
chmod +x text
./text
Upvotes: 0
Reputation: 22552
Try forming your test statements with correct spacing:
#!/bin/sh
echo "Ja eller nei:"
read svar
if [ "$svar" = "ja" ]; then
echo "Du er Positiv"
exit 0
elif [ "$svar" = "nei" ]; then
echo "Du er Negativ"
exit 0
else
echo "Du er Nøytral"
fi
This is the output I get:
lnet@bowser ~ $:) sh test.sh
Ja eller nei:
ja
Du er Positiv
lnet@bowser ~ $:) sh test.sh
Ja eller nei:
nei
Du er Negativ
lnet@bowser ~ $:) sh test.sh
Ja eller nei:
fish
Du er N[G[Gøytral
lnet@bowser ~ $:(
Update (thanks Sorpigal):
Also, make sure your file has proper unix line endings:
dos2unix -f /cygdrive/c/MyScripts/Bash.sh
Upvotes: 3