[英]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.