由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Unix版 - One shell question
相关主题
bash script question: read in text怎么从script中把环境变量传回到当前shell中? (转载)
how to get the backgroup run state?how to change login shell?
[转载] Re: shell script questionBash 里一个简单诡异的问题 (转载)
Shell script求教Shell Question
新手求助shell scriptmy homework for UNIX shell高手, thanks!!!
which shell is best?[转载] 问个shell怎么写?
help shell script (waiting on line)question
shell script for "for" loop[转载] About the history
相关话题的讨论汇总
话题: 10话题: ans话题: n2话题: n1话题: cal
进入Unix版参与讨论
1 (共1页)
yy
发帖数: 45
1
ne short script:
ns=0
if [ $# -eq 3 ]; then
ans=$(( $n1 $op $n2 ))
echo "$n1 $op $n2 = $ans"
return $ans
else
echo "Function cal requires atleast three args"
fi
return
}
cal 5 + 10
cal 10 - 2
cal 10 / 2
echo $?
Run result:
$ ./pass1
5 + 10 = 15
10 - 2 = 8
10 / 2 = 5
5
My question is:
why it is: ans=$(( $n1 $op $n2 ))
not: ans=( $n1 $op $n2 ) ***
I tried the *** one: the running result is:
5 + 10 = 5
10 - 2 = 10
10 / 2 = 10
10
can any exlain a little bit?
Thank you very much
s
w*****e
发帖数: 23
2
(( ... )) is special characters in Bash, means expand and evaluate integer
expression between (( )).
( one two three ) is just array, if you use A=(B C), just means A is assigned
as array, so when you echo, it just dump first element which is B out.
You may want to look at "Advanced Bash scripting"

【在 yy 的大作中提到】
: ne short script:
: ns=0
: if [ $# -eq 3 ]; then
: ans=$(( $n1 $op $n2 ))
: echo "$n1 $op $n2 = $ans"
: return $ans
: else
: echo "Function cal requires atleast three args"
: fi
: return

yy
发帖数: 45
3
Thanks a lot!

【在 w*****e 的大作中提到】
: (( ... )) is special characters in Bash, means expand and evaluate integer
: expression between (( )).
: ( one two three ) is just array, if you use A=(B C), just means A is assigned
: as array, so when you echo, it just dump first element which is B out.
: You may want to look at "Advanced Bash scripting"

1 (共1页)
进入Unix版参与讨论
相关主题
[转载] About the history新手求助shell script
where can find source code of Bash or csh or any shellwhich shell is best?
Re: 想登录后的shell是bashhelp shell script (waiting on line)
How to program? Shell Questionshell script for "for" loop
bash script question: read in text怎么从script中把环境变量传回到当前shell中? (转载)
how to get the backgroup run state?how to change login shell?
[转载] Re: shell script questionBash 里一个简单诡异的问题 (转载)
Shell script求教Shell Question
相关话题的讨论汇总
话题: 10话题: ans话题: n2话题: n1话题: cal