getopts的不会连续两次打电话? [英] getopts won't call twice in a row?
本文介绍了getopts的不会连续两次打电话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于某些原因,选择做工精细的第一个电话lib_progress_bar -c@-u_0 100
,但在第二个电话并超越一切默认情况下,因为它看起来像 getopts的C:ü:D:p:S:%:M:标志
是不正确的,第二次左右,或永远不会执行ATLEAST的情况下当我用设置-x
#!/斌/庆典lib_progress_bar(){
局部电流= 0
当地最大= 100
本地completed_char =#
本地uncompleted_char =。
当地小数= 1
当地preFIX =[
当地后缀=]
当地percent_sign =%
当地MAX_WIDTH = $(tput的COLS) 当地完整的保持减法宽度ATLEAST%的字符
当地填充= 3 而getopts的C:ü:D:P:S:%:M:标志;做
案$旗在
C)completed_char =$ OPTARG;;
U)uncompleted_char =$ OPTARG;;
D)十进制=$ OPTARG;;
P)preFIX =$ OPTARG;;
S)后缀=$ OPTARG;;
%)percent_sign =$ OPTARG;;
M)MAX_WIDTH =$ OPTARG;;
ESAC
DONE
移位$((OPTIND-1))
电流= $ {1: - $当前}
最大= $ {2: - $最大}
如果((十进制大于0));然后
((填充=填充+小数+ 1))
科幻
让减法= $ {#completed_char} + $ {#preFIX} + $ {#后缀} +填充+ $ {#percent_sign}
让宽度= MAX_WIDTH减法
如果((宽度小于5));然后
((ATLEAST = 5 +减法))
回声>和2($ MAX_WIDTH)的MAX_WIDTH太小,至少要包含$ ATLEAST
返回1
科幻
如果((电流>最大));然后
回声>和2的电流值必须小于最高值。
返回1
科幻 百分比= $(AWK -vF =%$ {}填充$ {}十进制F-vC = $当前-vM = $最大'BEGIN {printf的('F',C / M * 100)}') ((字符=电流*宽/最大)) #sprintf的N个零,到命名为ARG于-v的VAR
printf的-v完整的%0 *。* D'''$字符''
printf的-v保持%* 0 * D'''$((宽 - 字符))'' #与所需的字符替换零
完整= $ {//完成0 /$ completed_char}
依然= $ {//保持0 /$ uncompleted_char} printf的%s%S%s%S%s%S \\ r'$preFIX$完整,$留,后缀$$%的$ percent_sign 如果((电流> = MAX));然后
回声
科幻
}
lib_progress_bar -c@-u_0 100
回声
lib_progress_bar -c@-u_25 100
回声
lib_progress_bar -c@-u_50 100
回声出口;
解决方案
只需添加:
本地OPTIND
在您的函数的顶部
For some reason the options work fine the first call of lib_progress_bar -c "@" -u "_" 0 100
, but on the second call and beyond everything is default because it seems like getopts c:u:d:p:s:%:m: flag
isn't true the second time around, or atleast the case is never executed when I used set -x
#!/bin/bash
lib_progress_bar() {
local current=0
local max=100
local completed_char="#"
local uncompleted_char="."
local decimal=1
local prefix=" ["
local suffix="]"
local percent_sign="%"
local max_width=$(tput cols)
local complete remain subtraction width atleast percent chars
local padding=3
while getopts c:u:d:p:s:%:m: flag; do
case "$flag" in
c) completed_char="$OPTARG";;
u) uncompleted_char="$OPTARG";;
d) decimal="$OPTARG";;
p) prefix="$OPTARG";;
s) suffix="$OPTARG";;
%) percent_sign="$OPTARG";;
m) max_width="$OPTARG";;
esac
done
shift $((OPTIND-1))
current=${1:-$current}
max=${2:-$max}
if (( decimal > 0 )); then
(( padding = padding + decimal + 1 ))
fi
let subtraction=${#completed_char}+${#prefix}+${#suffix}+padding+${#percent_sign}
let width=max_width-subtraction
if (( width < 5 )); then
(( atleast = 5 + subtraction ))
echo >&2 "the max_width of ($max_width) is too small, must be atleast $atleast"
return 1
fi
if (( current > max ));then
echo >&2 "current value must be smaller than max. value"
return 1
fi
percent=$(awk -v "f=%${padding}.${decimal}f" -v "c=$current" -v "m=$max" 'BEGIN{printf('f', c / m * 100)}')
(( chars = current * width / max))
# sprintf n zeros into the var named as the arg to -v
printf -v complete '%0*.*d' '' "$chars" ''
printf -v remain '%0*.*d' '' "$((width - chars))" ''
# replace the zeros with the desired char
complete=${complete//0/"$completed_char"}
remain=${remain//0/"$uncompleted_char"}
printf '%s%s%s%s %s%s\r' "$prefix" "$complete" "$remain" "$suffix" "$percent" "$percent_sign"
if (( current >= max )); then
echo ""
fi
}
lib_progress_bar -c "@" -u "_" 0 100
echo
lib_progress_bar -c "@" -u "_" 25 100
echo
lib_progress_bar -c "@" -u "_" 50 100
echo
exit;
解决方案
Just add:
local OPTIND
at the top of your function.
这篇关于getopts的不会连续两次打电话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文