delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2005/12/10/16:37:28

X-Spam-Check-By: sourceware.org
Message-ID: <439B4A77.5050609@byu.net>
Date: Sat, 10 Dec 2005 14:36:55 -0700
From: Eric Blake <ebb9 AT byu DOT net>
Reply-To: cygwin AT cygwin DOT com
User-Agent: Mozilla Thunderbird 1.0.2 (Windows/20050317)
MIME-Version: 1.0
To: Robert Body <rbody99 AT hotmail DOT com>, cygwin AT cygwin DOT com
Subject: Re: sftp progress showing ... 66% complete
References: <BAY104-F18D8486CB6C9EB7925EBE0AD440 AT phx DOT gbl>
In-Reply-To: <BAY104-F18D8486CB6C9EB7925EBE0AD440@phx.gbl>
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
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

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

http://cygwin.com/acronyms/#PPIOSPE

Ugh - top-posted.  Reformatted.  http://cygwin.com/acronyms/#TOFU

>> From: Eric Blake <ebb9 AT byu DOT net>
                         ^^^^   ^^^^^

Ugh - http://cygwin.com/acronyms/#PCYMTNQREAIYR

>
> If you use bash, $(( x - y )) does just that.
>
>> (nor how to get
>> clock-ticks since 1970/1/1)
>
> date +%s
>

According to Robert Body on 12/10/2005 2:23 PM:
> Thanks Eric, that does the trick, it's short and interesting syntax with
> ((x-y)), i tried (x-y) before myself, i didn't think of trying double
> brackets:
> /home/Owner/r> start=$(date +%s)
> /home/Owner/r> end=$(date +%s)
> /home/Owner/r> (($end-$start))
> /home/Owner/r> alpha=$(($end-$start)); echo $alpha
> 10
> 
> how come straight out (($end-$start)) doesn't work, i have to assign it
> to a variable?

The syntax for arithmetic substitution is $(( expression )).  It is
required by POSIX shells, and all it does is replace everything from the
starting $(( to the ending )) with the string result, provided the
expression could be evaluated.  (($end-$start)) is a bash extension which
is only used to test the result for 0/non-zero, rather than actually
providing the result in the text of the rest of the line.

> with some playing:
> /home/Owner/r> echo (($end-$start))
> bash: syntax error near unexpected token `('

Because (( )) only makes sense as an entire command, not as an argument to
some other command.

> /home/Owner/r> echo `(($end-$start))`
>

Because (()) was used as an entire command, but produced no output, so the
`` was invoked with no input, and produced no output, so echo just printed
a newline.

> /home/Owner/r> echo `($end-$start)`
> bash: 1134249426-1134249416: command not found
>

Because () invokes a subshell, rather than arithmetic substitution, and as
bash told you, there really isn't a command by that name.

> /home/Owner/r> echo $(($end-$start))
> 10

Bingo.

I would suggest reading up on 'man bash'.

> 
> too bad it's not a standard way that would work in anything, sh, ksh,
> how would ksh syntax look like?

If your shell is POSIX-compliant (cygwin's /bin/sh, bash, ksh, and zsh all
have POSIX-compliant behavior, but ash does not), then $(( )) will work.

If you want to be portable to older hosts or shells, consider reading up
on 'man expr' (shell arithmetic substitution was designed to be more
efficient than what expr could do).

- --
Life is short - so eat dessert first!

Eric Blake             ebb9 AT byu DOT net
volunteer cygwin bash maintainer
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDm0p284KuGfSFAYARAsG9AKDQG/we1dmLKGYqUqT9JC7wz03WHwCgrhve
M/YdH20NtyJdJzXegck1FrU=
=ycN6
-----END PGP SIGNATURE-----

--
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