linux - 请教一个 shell 语法的问题

查看:113
本文介绍了linux - 请教一个 shell 语法的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

#! /bin/sh

COUNTER=1
while [ "$COUNTER" -lt 10 ]; do
  echo "Here we go again"
  COUNTER=$(($COUNTER+1))
done

请问 $COUNTER+1 外面套两个 (),最后还加上一个 $

我理解是套一个就可以返回数值了,然后用$把值变为变量。但是执行时报错.

请教诸位,这个是怎么回事?

解决方案

根据 bash 手册:

Arithmetic Expansion

Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. The format for arithmetic expansion is:

         $((expression))

所以, 当你要获取 arithmetic evaluation 的值的时候, 要加 $. 不获取它的值时, 不用加$.
Arithmetic ExpansionArithmetic Evaluation是不同的.

ARITHMETIC EVALUATION

The shell allows arithmetic expressions to be evaluated, under certain circumstances.

Arithmetic Evaluation 可以作为单独的命令使用, 而 Arithmetic Expansion 是获取 arithmetic evaluation 命令的结果. 如:

(( 2 > 3 )) # valid
$((2 > 3))  # 0: command not found
(( 2 > 3 )) || echo yes  # yes

这篇关于linux - 请教一个 shell 语法的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆