檢查傳遞給 Bash 腳本的參數數量 - Check number of arguments passed to a Bash script

問題:

I would like my Bash script to print an error message if the required argument count is not met.如果未滿足所需的參數計數,我希望我的 Bash 腳本打印錯誤消息。

I tried the following code:我嘗試了以下代碼:

#!/bin/bash
echo Script name: $0
echo $# arguments 
if [$# -ne 1]; 
    then echo "illegal number of parameters"
fi

For some unknown reason I've got the following error:由於某些未知原因,我收到以下錯誤:

test: line 4: [2: command not found

What am I doing wrong?我究竟做錯了什麼?


解決方案:

參考一: https://en.stackoom.com/question/1FuZG
參考二: https://stackoom.com/question/1FuZG
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章