* Setting Integer Variables in Bash @ 2009-01-30 0:40 whitewall 2009-01-30 0:51 ` Tim McDaniel 2009-01-30 9:02 ` Mark J. Reed 0 siblings, 2 replies; 4+ messages in thread From: whitewall @ 2009-01-30 0:40 UTC (permalink / raw) To: cygwin The text below is from a text file. If I type the commands line-by-line in the bash then the commands work as expected. If I save the commands in a text file and call the script I get the error message: ': not a valid identifier2: declare: 'Red ': not a valid identifier3: declare: 'Green #! /cygdrive/c/cygwin/bin/bash declare -i Red declare -i Green Red=10 Green=$Red+1 echo $Green exit 0 -- View this message in context: http://www.nabble.com/Setting-Integer-Variables-in-Bash-tp21737864p21737864.html Sent from the Cygwin list mailing list archive at Nabble.com. -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Problem reports: http://cygwin.com/problems.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/ ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Setting Integer Variables in Bash 2009-01-30 0:40 Setting Integer Variables in Bash whitewall @ 2009-01-30 0:51 ` Tim McDaniel 2009-01-30 9:02 ` Mark J. Reed 1 sibling, 0 replies; 4+ messages in thread From: Tim McDaniel @ 2009-01-30 0:51 UTC (permalink / raw) Cc: cygwin On Thu, 29 Jan 2009, whitewall <thewhitewall@live.co.uk> wrote: > The text below is from a text file. If I type the commands > line-by-line in the bash then the commands work as expected. If I > save the commands in a text file and call the script I get the error > message: > ': not a valid identifier2: declare: 'Red > ': not a valid identifier3: declare: 'Green Two error messages. > #! /cygdrive/c/cygwin/bin/bash > declare -i Red > declare -i Green > Red=10 > Green=$Red+1 > echo $Green > exit 0 You have a carriage return at the end of each line. bash does NOT consider carriage return to be whitespace, dammit, so it is considered normal characters. So it things, for example, that you're declaring a variable named "Red\r", a four-character name, and it just doesn't allow carriage return in the variable name. The key to recognizing the situation is to see ': not a valid identifier2: declare: 'Red and recognize that there's a carriage return in the middle of the message. The opening ' is just before Red. Its matching closing ' is shown as the start of the line -- because carriage return causes the output display to return to the start of line. So - by default, created files in UNIX file format, not native Windows. - strip out the carriage returns from your existing script -- Tim McDaniel, tmcd@panix.com -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Problem reports: http://cygwin.com/problems.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/ ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Setting Integer Variables in Bash 2009-01-30 0:40 Setting Integer Variables in Bash whitewall 2009-01-30 0:51 ` Tim McDaniel @ 2009-01-30 9:02 ` Mark J. Reed 2009-01-30 9:38 ` Tim McDaniel 1 sibling, 1 reply; 4+ messages in thread From: Mark J. Reed @ 2009-01-30 9:02 UTC (permalink / raw) To: cygwin On Thu, Jan 29, 2009 at 5:54 PM, whitewall wrote: > > The text below is from a text file. If I type the commands line-by-line in > the bash then the commands work as expected. If I save the commands in a > text file and call the script I get the error message: > ': not a valid identifier2: declare: 'Red > ': not a valid identifier3: declare: 'Green If you run the script's stderr through od or similar, you will see that what bash is really saying is /path/to/your/file: line 2: declare: 'Red\r': not a valid identifier /path/to/your/file: line 3: declare: 'Green\r': not a valid identifier where the '\r's are carriage returns which cause the cursor to go back and overwrite the first part of the message. Run d2u on your script. > #! /cygdrive/c/cygwin/bin/bash > declare -i Red > declare -i Green > Red=10 > Green=$Red+1 Since you've declared both Green and Red as integer, you should just do Green=Red+1, without the dollar sign. Doing Green=$Red+1 first takes Red's value, which is stored as an integer, expands it back into its decimal string representation, and then reparses it to yield its integer value. I know that in a real script, any efficiency gains will be swamped by I/O, but there's no sense making the shell do extra work. :) -- Mark J. Reed <markjreed@gmail.com> -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Problem reports: http://cygwin.com/problems.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/ ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Setting Integer Variables in Bash 2009-01-30 9:02 ` Mark J. Reed @ 2009-01-30 9:38 ` Tim McDaniel 0 siblings, 0 replies; 4+ messages in thread From: Tim McDaniel @ 2009-01-30 9:38 UTC (permalink / raw) Cc: cygwin On Thu, 29 Jan 2009, Mark J. Reed wrote: > On Thu, Jan 29, 2009 at 5:54 PM, whitewall wrote: >> #! /cygdrive/c/cygwin/bin/bash >> declare -i Red >> declare -i Green >> Red=10 >> Green=$Red+1 > > Since you've declared both Green and Red as integer, you should just > do Green=Red+1, without the dollar sign. Doing Green=$Red+1 first > takes Red's value, which is stored as an integer, expands it back > into its decimal string representation, and then reparses it to > yield its integer value. There IS one subtle difference. If you're running with "set -x", Green=$Red+1 will echo + Green=10+1 But Green=Red+1 will echo + Green=Red+1 (assuming that you've not changed PS4, IFS, &c &c). You can decide which "set -x" output you like. I found that I preferred the substituted forms, the ones with $this and $that. -- Tim McDaniel, tmcd@panix.com -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Problem reports: http://cygwin.com/problems.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/ ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2009-01-30 0:51 UTC | newest] Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2009-01-30 0:40 Setting Integer Variables in Bash whitewall 2009-01-30 0:51 ` Tim McDaniel 2009-01-30 9:02 ` Mark J. Reed 2009-01-30 9:38 ` Tim McDaniel
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).