![為 while 迴圈提供輸入](https://rvso.com/image/1047577/%E7%82%BA%20while%20%E8%BF%B4%E5%9C%88%E6%8F%90%E4%BE%9B%E8%BC%B8%E5%85%A5.png)
while getopts "f:t:d:g:o:p:b:q:r:" opt; do
case "$opt" in
(f)fan=${OPTARG}
(t)..
esac
done
shift $(( OPTIND - 1 ));
如何提供輸入?誰能告訴我如何為上述程式碼片段提供輸入?
答案1
對於特定的 while 迴圈(使用getopts
),您通常會將其放在外殼腳本然後使用您的選項/參數呼叫腳本,例如
#!/bin/bash
while getopts "f:t:d:g:o:p:b:q:r:" opt; do
case "$opt" in
f) fan=${OPTARG}
;;
t) echo "doing somthing with option t = $OPTARG"
;;
esac
done
shift $(( OPTIND - 1 ));
然後使其可執行
chmod +x yourscript.sh
然後運行它
$ ./yourscript.sh -t 3
doing somthing with option t = 3