為 while 迴圈提供輸入

為 while 迴圈提供輸入
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

相關內容