相关疑难解决方法(0)

如何编写一个带有可选输入参数的bash脚本?

我希望我的脚本能够获取可选输入,

例如,目前我的剧本是

#!/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)

bash arguments parameter-passing

425
推荐指数
8
解决办法
22万
查看次数

getopts不会连续两次调用?

由于某种原因,选项在第一次调用时运行良好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)

bash shell getopts

12
推荐指数
2
解决办法
4446
查看次数

标签 统计

bash ×2

arguments ×1

getopts ×1

parameter-passing ×1

shell ×1