delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2009/01/29/18:40:44

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=-1.8 required=5.0 tests=AWL,BAYES_00,SARE_MSGID_LONG40,SPF_PASS
X-Spam-Check-By: sourceware.org
MIME-Version: 1.0
In-Reply-To: <21737864.post@talk.nabble.com>
References: <21737864 DOT post AT talk DOT nabble DOT com>
Date: Thu, 29 Jan 2009 18:40:32 -0500
Message-ID: <f60fe000901291540o62d155c5sa47004f8cfe8c750@mail.gmail.com>
Subject: Re: Setting Integer Variables in Bash
From: "Mark J. Reed" <markjreed AT gmail DOT com>
To: cygwin AT cygwin DOT com
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

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 AT gmail DOT 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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019