Bash

呼叫函式時出現語法錯誤

  • May 1, 2022

所以我嘗試在腳本中創建一個函式,在執行腳本時為每個參數創建一個新變數。這是我的程式碼:

#!/bin/bash

# Creating function log
#ARG1=${1}

log() {
 echo "You called DA LOG FUNCTION!!!1!!11one111!"
}

log

#echo "${1}"
#echo "$ARG1"

fcta() {
 for ((x=0;x<1000;++x)); do
  "a$x"=${1}
  if [[ ${#} -gt 1 ]]; then
    shift
  else
    x=1001
  fi
 echo "${a$x}"
#  echo "${1}"
}

fcta $@

我明白了:

vagrant@localhost vagrant]$./luser-demo10.sh 12 12 12
You called DA LOG FUNCTION!!!1!!11one111!
./luser-demo10.sh: line 25: syntax error near unexpected token `}'
./luser-demo10.sh: line 25: `}'
[04:11----------------------------------------
vagrant@localhost vagrant]$

這是第 25 行

#  echo "${1}"
} <----- LINE 25

fcta $@

編輯:感謝您告訴我失去的“完成”。人們問我想做什麼,好吧,我問了另一個問題,因為這個問題已經得到解答(問題是,為什麼我會收到語法錯誤)。再次感謝。

在您的函式中,有一個do但沒有匹配項done來關閉命令列表。

嘗試shellcheck來驗證您的腳本。這是腳本中檢測到的錯誤和可疑點的報告:

Line 16:
 for ((x=0;x<1000;++x)); do
 ^-- SC1009: The mentioned syntax error was in this for loop.
     ^-- SC1073: Couldn't parse this arithmetic for condition. Fix to allow more checks.
                         ^-- SC1061: Couldn't find 'done' for this 'do'.

Line 25:
}
^-- SC1062: Expected 'done' matching previously mentioned 'do'.
^-- SC1072: Unexpected keyword/token. Fix any mentioned problems and try again.

引用自:https://unix.stackexchange.com/questions/464071