我希望我的脚本能够获取可选输入,
例如,目前我的剧本是
#!/bin/bash
somecommand foo
Run Code Online (Sandbox Code Playgroud)
但我想说:
#!/bin/bash
somecommand [ if $1 exists, $1, else, foo ]
Run Code Online (Sandbox Code Playgroud) 由于某种原因,选项在第一次调用时运行良好lib_progress_bar -c "@" -u "_" 0 100,但在第二次调用时超出一切都是默认值,因为getopts c:u:d:p:s:%:m: flag第二次看似不是真的,或者至少在我使用时从未执行过的情况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 > …Run Code Online (Sandbox Code Playgroud)