English 中文(简体)
I m 在UNIX进行算术表示
原标题:I m having trouble performing arithmetic expressions in UNIX

我有以下文字:

  #!/bin/sh
  r=3
  r=$((r+5))
  echo r

然而,我发现这一错误:

3. 第3行的协同效应错误:出乎意料。

我不理解我做错的事情。 http://www.unixtutorial.org/2008/06/arithmetic-operations-in-unix-scripts/“rel='nofollow”http://www.unixtutorial.org/2008/06/arithmetic-operations-in-unix-scripts/

最佳回答

如果你再次使用<条码>bash,但<条码>$(r+5),如果你再次使用另一枚炮弹,则可能不予支持。 <代码>/bin/sh? 您是否考虑将其改为/bin/bash。 如果有的话?

问题回答

she子是你的问题。 b 现金没有消失。 将其改为<代码>#! 页: 1

如果您将<代码>echo r改为<代码>echo /code>,则该编码对我有效(<代码>8)。 您安装了什么版本的<代码>sh? 什么分配不统一?

您不妨尝试:

#!/bin/sh  
r=3  
r=$((r + 5))  
echo $r

在进行数学(包括精度/脂肪)时,可使用<代码>awk或<代码>bc/dc。

awk -vr="$r"  BEGIN{r=r+5;print r} 

echo "$r+5" | bc




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签