這是代碼,我想運行這個命令
獲取test.log輸入,然后將其轉換為文本型別,然后將其存盤在output.txt檔案中
./small.sh test.log -t text -o output.txt
#!/usr/bin/env bash
usage() { echo "$0 usage:" && grep " .)\ #" $0; exit 0; }
[ $# -eq 0 ] && usage
parse()
{
local file=$1
local type=$2
local output=$3
echo "file: ${file}, type: ${type}, output: ${output}"
}
while getopts ":ht:o:" arg; do
case $arg in
t) # specify type.
type=${OPTARG} ;;
o) # specify directory.
output=${OPTARG} ;;
h | *) # Display help.
usage
exit 0
;;
esac
done
shift $(( OPTIND - 1))
parse "${file}" "${type}" "${output}"
使用此代碼,我只能在像這樣將其按未排序順序放置檔案名時獲取檔案名
./small.sh -t text -o output.txt test.log
uj5u.com熱心網友回復:
如何使用 getopts 獲取檔案名和引數
./small.sh test.log -t text -o output.txt
這不可能。getopts僅支持選項引數之后的位置引數。
你可以:
- 接受限制
- 使用別的東西,比如 GNU
getopt。 - 撰寫自己的選項決議代碼
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/420376.html
標籤:
