[英]Linux shell negative values print problem
operation () {
operator=${exp:1:1} # 2nd character / 1st is ${words:0:1}
# Read into an array as tokens separated by IFS
IFS="$operator"
read -ra array <<< "$exp" # -ra = raw input array
a=array[0]
b=array[1]
# https://www.computerhope.com/unix/bash/read.htm
if [ "$operator" == "+" ]; then
operation=$((a+b))
fi
if [ "$operator" == "-" ]; then
operation=$((a+b)) # => ' 1'
operation=`expr a-b` # => ' 1'
fi
if [ "$operator" == "*" ]; then
operation=$((a*b))
fi
if [ "$operator" == "/" ]; then
operation=$((a/b))
fi
# https://www.tutorialsandyou.com/bash-shell-scripting/bash-arithmetic-operations-11.html
echo $((operation))
}
exp='2-3'
operation $exp
一切(几乎)工作正常,只是在进行减法operation=$((a+b))
或operation=`expr ab`
,我无论如何都无法打印减号('-')而不是空格(''),( -) 标志它已被空格替换。 在这个例子中,我得到' 1'
而不是'-1'
。
为什么会这样?
我猜您打算使用-
if 运算符是减号:
if [ "$operator" == "-" ]; then
operation=$((a - b)) # => ' 1'
fi
这个脚本有两个问题。 首先,不引用 arrays 元素,如数组 [0] 在 C 中,但${name[subscript]}
就像在 man bash 中所说:
可以使用 ${name[subscript]} 引用数组的任何元素。
所以应该是:
a=${array[0]}
b=${array[1]}
其次, $((var))
只能用于算术运算。 如果要打印变量的内容,只需执行以下操作:
echo "$operation"
总而言之,您的脚本应该是:
#!/usr/bin/env bash
operation () {
operator=${exp:1:1} # 2nd character / 1st is ${words:0:1}
# Read into an array as tokens separated by IFS
IFS="$operator"
read -ra array <<< "$exp" # -ra = raw input array
a=${array[0]}
b=${array[1]}
# https://www.computerhope.com/unix/bash/read.htm
if [ "$operator" == "+" ]; then
operation=$((a+b))
fi
if [ "$operator" == "-" ]; then
operation=$((a - b))
fi
if [ "$operator" == "*" ]; then
operation=$((a*b))
fi
if [ "$operator" == "/" ]; then
operation=$((a/b))
fi
# https://www.tutorialsandyou.com/bash-shell-scripting/bash-arithmetic-operations-11.html
echo "$operation"
}
exp='2-3'
operation $exp
用法:
$ ./script.sh
-1
我们在这里缺少一些上下文:
a=array[0]
只有在declare -ia
时才是正确的。exp
?但问题是缺乏引用的简单问题:
$ IFS="-"
$ operation="-6"
$ echo $((operation))
6
$ echo "$((operation))"
-6
这完全是由于 IFS 值。 bash 获取echo -6
并将其转换为echo "" 6
,其中-
分隔符左侧的空字符串。
在引号内,不执行分词。
手册中3.5.7 分词中记录的行为。
您只能在读取命令期间设置 IFS,这样它就不会影响脚本的 rest:
$ exp="4-5"
$ IFS=- read -ra array <<<"$exp"
$ declare -p array
declare -a array=([0]="4" [1]="5")
$ printf "%q\n" "$IFS"
$' \t\n'
operation='expr ab' # => ' 1'
operation=$((a+b)) # => ' 1'
你必须使用 - 而不是 +: echo "$((operation))"
此代码正常工作:
operation () {
operator=${exp:1:1} # 2nd character / 1st is ${words:0:1}
# Read into an array as tokens separated by IFS
IFS="$operator"
read -ra array <<< "$exp" # -ra = raw input array
a=array[0]
b=array[1]
# https://www.computerhope.com/unix/bash/read.htm
if [ "$operator" == "+" ]; then
operation=$((a+b))
fi
if [ "$operator" == "-" ]; then
operation="$((a-b))"
fi
if [ "$operator" == "*" ]; then
operation=$((a*b))
fi
if [ "$operator" == "/" ]; then
operation=$((a/b))
fi
# https://www.tutorialsandyou.com/bash-shell-scripting/bash-arithmetic-operations-11.html
echo "$((operation))"
}
exp='2-3'
operation $exp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.