我想打電話給myscript
文件以這種方式使用getopts的一個例子:如何在bash
$ ./myscript -s 45 -p any_string
或
$ ./myscript -h >>> should display help
$ ./myscript >>> should display help
我的要求是:
getopt
這裏得到輸入參數- 檢查
-s
存在,如果不返回錯誤 - 檢查
-s
後的值爲45或90 - 檢查
-p
存在,並且如果用戶輸入./myscript -h
或只是./myscript
然後顯示幫助有後 - 輸入字符串
我試過到目前爲止這樣的代碼:
#!/bin/bash
while getopts "h:s:" arg; do
case $arg in
h)
echo "usage"
;;
s)
strength=$OPTARG
echo $strength
;;
esac
done
但與代碼我得到錯誤。如何用Bash和getopt
做到這一點?
[本教程](HTTP://維基。 bash-hackers.org/howto/getopts_tutorial)可能是一個很好的開始。 – jam 2013-05-10 13:18:50
選項應該是可選的。如果你需要由'-s'指定的值,則使它成爲一個位置參數:'./myscript 45 anystring'。 – chepner 2013-05-10 13:19:04
@chepner'$。/ myscript -s 45 -p any_string' – MOHAMED 2013-05-10 13:21:10