LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1110|回复: 8

这个循环怎样用bash shell写啊?[基本解决]

[复制链接]
发表于 2004-6-6 23:10:10 | 显示全部楼层 |阅读模式
int num=0;
for ( int i=1; i<1000000; i++ ) num += i;
发表于 2004-6-6 23:15:13 | 显示全部楼层
  1. n=0;num=0
  2. while ((n<=1000000));do
  3.         ((num+=n))
  4.         ((n+=1))
  5. done;echo $num
复制代码

ps:sorry,看错题啦
发表于 2004-6-7 08:51:07 | 显示全部楼层
bash 的 for 语句:

  1. sum=0
  2. for ((i = 0; i < 100; i++))
  3. do
  4.   ((sum += i))
  5. done
复制代码
发表于 2004-6-7 09:16:56 | 显示全部楼层
Why use:
((sum += 1))
Not
sum += 1 or (sum += 1)

Thanks in advance.
发表于 2004-6-7 09:40:34 | 显示全部楼层
sum=$((sum + i)) 也是可以的, 我的写法只是简单些, 这是 bash 的语法
发表于 2004-6-7 09:44:17 | 显示全部楼层

  1. ARITHMETIC EVALUATION
  2.        The shell allows arithmetic expressions to be evaluated, under  certain
  3.        circumstances  (see  the let builtin command and Arithmetic Expansion).
  4.        Evaluation is done in fixed-width integers with no check for  overflow,
  5.        though division by 0 is trapped and flagged as an error.  The operators
  6.        and their precedence and associativity are the same as in  the  C  lan-
  7.        guage.   The  following  list  of  operators  is grouped into levels of
  8.        equal-precedence operators.  The levels are listed in order of decreas-
  9.        ing precedence.

  10.        id++ id--
  11.               variable post-increment and post-decrement
  12.        ++id --id
  13.               variable pre-increment and pre-decrement
  14.        - +    unary minus and plus
  15.        ! ~    logical and bitwise negation
  16.        **     exponentiation
  17.        * / %  multiplication, division, remainder
  18.        + -    addition, subtraction
  19.        << >>  left and right bitwise shifts
  20.        <= >= < >
  21.               comparison
  22.        == !=  equality and inequality
  23.        &      bitwise AND
  24.        ^      bitwise exclusive OR
  25.        |      bitwise OR
  26.        &&     logical AND
  27.        ||     logical OR
  28.        expr?expr:expr
  29.               conditional evaluation
  30.        = *= /= %= += -= <<= >>= &= ^= |=
  31.               assignment
  32.        expr1 , expr2
  33.               comma

  34.        Shell  variables  are  allowed as operands; parameter expansion is per-
  35.        formed before the expression is evaluated.  Within an expression, shell
  36.        variables  may  also  be referenced by name without using the parameter
  37.        expansion syntax.  The value of a variable is evaluated  as  an  arith-
  38.        metic expression when it is referenced.  A shell variable need not have
  39.        its integer attribute turned on to be used in an expression.

  40.        Constants with a leading 0 are interpreted as octal numbers.  A leading
  41.        0x  or  0X  denotes  hexadecimal.   Otherwise,  numbers  take  the form
  42.        [base#]n, where base is a decimal number between 2 and 64  representing
  43.        the arithmetic base, and n is a number in that base.  If base# is omit-
  44.        ted, then base 10 is used.  The digits greater than 9  are  represented
  45.        by  the  lowercase  letters,  the  uppercase letters, @, and _, in that
  46.        order.  If base is less than or equal to 36,  lowercase  and  uppercase
  47.        letters  may be used interchangably to represent numbers between 10 and
  48.        35.

  49.        Operators are evaluated in order  of  precedence.   Sub-expressions  in
  50.        parentheses  are  evaluated first and may override the precedence rules
  51.        above.
复制代码
发表于 2004-6-7 09:45:00 | 显示全部楼层
详见 man bash
发表于 2004-6-7 13:52:57 | 显示全部楼层
thanks for your kind reply.
发表于 2004-6-7 14:17:44 | 显示全部楼层
更详尽的解释:
  1. Compound Commands

  2.     ...

  3.     [color=red]((expression))[/color]
  4.            The expression is evaluated according  to  the  rules  described
  5.            below  under [color=red]ARITHMETIC EVALUATION[/color].  If the value of the expres-
  6.            sion is non-zero, the return status is 0; otherwise  the  return
  7.            status is 1.  This is exactly equivalent to let "expression".

  8.     ...

  9. Arithmetic Expansion
  10.     Arithmetic expansion allows the evaluation of an arithmetic  expression
  11.     and  the  substitution of the result.  The format for arithmetic expan-
  12.     sion is:

  13.            [color=red]$((expression))[/color]

  14.     The expression is treated as if it were within  double  quotes,  but  a
  15.     double  quote  inside  the  parentheses  is not treated specially.  All
  16.     tokens in the expression undergo parameter expansion, string expansion,
  17.     command  substitution, and quote removal.  Arithmetic substitutions may
  18.     be nested.

  19.     The evaluation is performed according to the rules listed  below  under
  20.     [color=red]ARITHMETIC EVALUATION[/color].  If expression is invalid, bash prints a message
  21.     indicating failure and no substitution occurs.

  22. [color=red]ARITHMETIC EVALUATION[/color]
  23.        The shell allows arithmetic expressions to be evaluated, under  certain
  24.        circumstances  (see  the let builtin command and Arithmetic Expansion).
  25.        Evaluation is done in fixed-width integers with no check for  overflow,
  26.        though division by 0 is trapped and flagged as an error.  The operators
  27.        and their precedence and associativity are the same as in  the  C  lan-
  28.        guage.   The  following  list  of  operators  is grouped into levels of
  29.        equal-precedence operators.  The levels are listed in order of decreas-
  30.        ing precedence.

  31.        id++ id--
  32.               variable post-increment and post-decrement
  33.        ++id --id
  34.               variable pre-increment and pre-decrement
  35.        - +    unary minus and plus
  36.        ! ~    logical and bitwise negation
  37.        **     exponentiation
  38.        * / %  multiplication, division, remainder
  39.        + -    addition, subtraction
  40.        << >>  left and right bitwise shifts
  41.        <= >= < >
  42.               comparison
  43.        == !=  equality and inequality
  44.        &      bitwise AND
  45.        ^      bitwise exclusive OR
  46.        |      bitwise OR
  47.        &&     logical AND
  48.        ||     logical OR
  49.        expr?expr:expr
  50.               conditional evaluation
  51.        = *= /= %= += -= <<= >>= &= ^= |=
  52.               assignment
  53.        expr1 , expr2
  54.               comma

  55.        Shell  variables  are  allowed as operands; parameter expansion is per-
  56.        formed before the expression is evaluated.  Within an expression, shell
  57.        variables  may  also  be referenced by name without using the parameter
  58.        expansion syntax.  The value of a variable is evaluated  as  an  arith-
  59.        metic expression when it is referenced.  A shell variable need not have
  60.        its integer attribute turned on to be used in an expression.

  61.        Constants with a leading 0 are interpreted as octal numbers.  A leading
  62.        0x  or  0X  denotes  hexadecimal.   Otherwise,  numbers  take  the form
  63.        [base#]n, where base is a decimal number between 2 and 64  representing
  64.        the arithmetic base, and n is a number in that base.  If base# is omit-
  65.        ted, then base 10 is used.  The digits greater than 9  are  represented
  66.        by  the  lowercase  letters,  the  uppercase letters, @, and _, in that
  67.        order.  If base is less than or equal to 36,  lowercase  and  uppercase
  68.        letters  may be used interchangably to represent numbers between 10 and
  69.        35.

  70.        Operators are evaluated in order  of  precedence.   Sub-expressions  in
  71.        parentheses  are  evaluated first and may override the precedence rules
  72.        above.
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表