簡體   English   中英

Linux shell 負值打印問題

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

為什么會這樣?

在 - 周圍使用空間:

exp=' 2 - 3 '

或者

操作 = expr a - b

或者

COUNT="expr $FIRSTV - $SECONDV"

也看到這個[ 鏈接] 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'
  1. 我刪除此行operation='expr ab' # => ' 1'
  2. 在這一行operation=$((a+b)) # => ' 1'你必須使用 - 而不是 +:
  3. 對於 output在變量周圍使用“”

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM