我撰寫了一個 bash 腳本,允許用戶通過輸入索引從陣列中選擇檔案名。這很好用,只是我現在需要一個函式來驗證用戶輸入的數字。我使用 shellcheck 測驗了該功能,這讓它變得一目了然。
#! /bin/bash
validateRange () {
if (( "$PICK" > "0" )) && (( "$PICK" <= "$LIMIT" )) then
echo "valid number"
fi
}
(( LIMIT=4 ))
(( PICK=2 ))
validateRange "$PICK" "$LIMIT"
shellcheck 讓這一切都清楚
$ shellcheck myscript
No issues detected!
$
但是,相同的代碼不會在我的.bash_functions script.
.bash_functions: line 201: syntax error near unexpected token `then'
.bash_functions: line 201: ` if (( "$PICK" > "0" )) && (( "$PICK" <= "$LIMIT" )) then '
我不確定我的代碼有什么問題。有沒有另一種方法可以在 bash 中實作這一點?
uj5u.com熱心網友回復:
此腳本的正確版本可能是:
#!/bin/bash
validateRange () {
if (( "$1" > 0 )) && (( "$1" <= "$2" ))
then
echo "valid number"
fi
}
declare -i LIMIT
LIMIT=4
declare -i PICK
PICK=2
validateRange "$PICK" "$LIMIT"
uj5u.com熱心網友回復:
試試[ "$PICK" -gt 0 ]。還有很多其他問題。我建議您坐下來閱讀 Bash 教程,這些問題非常基本且顯而易見。
shellcheck 只是一個短絨。如果您的語法已經非常錯誤,則它不起作用。您可以將它與https://github.com/bash-lsp/bash-language-server/一起使用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/314573.html
標籤:猛击
上一篇:在ubuntu終端或bash腳本中使用系統引數運行Matlab腳本
下一篇:在否定匹配上列印單行
