红联Linux门户
Linux帮助

shell的问题

发布时间:2010-10-25 09:51:14来源:红联作者:wucongdonglai
[i=s] 本帖最后由 wucongdonglai 于 2010-10-25 10:29 编辑 [/i]

今天看一bash的脚本。里面有这么一段:
d=1
c=”“
while [ "$c" != "q" ]
do
echo "read tmp"
read tmp
#这里是不是删除tmp中第一个,后的所有的内容,并赋给c?
c=${tmp%%,*}
#这里的if判断和前面语句结合,不就是判断tmp中有没有逗号?
if [ "$tmp" != "$c" ]; then
echo "Sorry, no commas allowed"
continue
fi
if [ -n "$c" ] ; then
if [ "$c" != "q" ]; then
#这下面是函数调用,不必看
insert_track $cdcatnum,$cdtrack,$cdttitle
fi
else
#函数算数计算的格式不是应该是 d=$(($d-1))吗?这个是什么意思
d=$((d-1))
fi
d=$((d+1))
done
麻烦各位大大帮忙看看啊
文章评论

共有 2 条评论

  1. wucongdonglai 于 2010-10-25 14:18:08发表:

    2# deepwhite
    恩,谢white兄

  2. deepwhite 于 2010-10-25 11:30:52发表:

    [i=s] 本帖最后由 deepwhite 于 2010-10-25 11:34 编辑 [/i]

    前面两个都对,最后一个, d=$((d-1)) 这么写也是可以的。可以看看 bash reference 或者自己实验一下。

    引用:

    3.5.5 Arithmetic Expansion

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

    $(( expression ))
    The expression is treated as if it were within double quotes, but a double quote inside the parentheses is not treated specially. All tokens in the expression undergo parameter expansion, command substitution, and quote removal. Arithmetic expansions may be nested.

    The evaluation is performed according to the rules listed below (see Shell Arithmetic). If the expression is invalid, Bash prints a message indicating failure to the standard error and no substitution occurs.

    [code]
    deepwhite@local ~ $ a=1 && echo $((a+3))
    4
    [/code]