Now I get a script.sh, previously it was executed using PuTTY provided it was written in VMWare, but now I want to execute in Windows using Cygwin, I already copy the script.sh out to the corresponding directory, but some commands Cygwin can not recognise.

generate(){
 date +%T
}

TIME = generate()
echo " Current Time: $TIME"

After execute in Cygwin

script.sh: line 3: syntax errot neat unexpected token '$'<\r''
script.sh: line 3:'generate<><
link|improve this question
feedback

1 Answer

You have a couple of errors in your script. You can't have spaces around the equal sign in an assignment. In order to assign the output of a function or program to a variable, you have to use command substitution which means the command name is surrounded by $() (which is preferable) or backticks (which is less desirable). Also, when you call a function, you don't use parentheses after the function name like you would in other languages.

generate () {
    date +%T
}

TIME=$(generate)

echo " Current Time: $TIME"

The $'\r' error comes from having Windows line endings. You can use dos2unix to convert the file or use an editor that you can choose which type of endings to save a file with.

dos2unix script.sh
link|improve this answer
I still new in shell script, this troubles me when i follow a linux shell script book, it always execute in Linux environment but i try it in Windows, so there are problems appear. Thank you very much for your help – Lily Mar 18 '11 at 6:33
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.