【问题标题】:Default Flag Option Get Ops默认标志选项获取操作
【发布时间】:2019-07-19 03:33:27
【问题描述】:

如果有人运行 -9 并且在标志之后没有为 OPTARG 放置任何内容,我希望操作默认为 -8 标志,或者我可以检查 OPTARG 是否为空并用基数替换空值价值。

ft1 和 ftx 只是格式函数。

当我运行 goat -8 时,它会打印出有问题的文件

当我运行goat -9 SEARCH STRING 时,它会打印仅包含该搜索字符串结果的文件。

如果我运行 goat -9 并且不输入搜索字符串,它什么也不做,因为 OPTARG 是空白的,所以 grep 没有什么可搜索的。

如果用户在goat -9 之后没有输入字符串,我希望它能够将-9 更改为-8

    function goat() {
        local OPTIND
        local OPTARG
            if [ "$1" = "" ] ;
            then ft1 ;
            printf "

        This command requires an OPTION. Refer to the help menu (-h) for assistance.

    " | layoutcc
    ft1 ;
    fi

    while getopts ":h89:" opt;
    do case $opt in 

     8) for file in analysis/connection_history.txt ; do ftx $file | layoutcc ; 
                printf "%s" "$(<$file)"                                                    
                echo ;
                ft1 ;
                done 
    ;;

    9) grep_expr=$OPTARG

       for file in analysis/connection_history.txt ; do
            ftx "$file" | layoutcc
            grep "$grep_expr" $file | perl -ne 'print if /-201[8-9]/' | 
            perl -pe 's/......-2019/\e[1;32m$&\e[0m/g' 
                echo
                ft1
                done 
                ;;

function ft1
{
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' = 
}   

function ft2
{
ft1 ;
ft1
}

function ftx
{ 
ft1 ;
echo "                         File Name: $1    $(grep -h "Created on" *-Report.txt | head -n1)" 
ft1
}       

function fsx
{
ft1 ;
echo "                         File Name: $1"
ft1
}

【问题讨论】:

  • 请发布:预期输入,预期输出。请创建一个 MCVE。 ft1ftxfor file in .. 以及与该问题相关的所有其余脚本如何?问题是什么?
  • 简单地说,我想检查 OPTARG 是否为 NULL,如果是,则在其中放置一个默认值。
  • 我不认为getopts 支持这一点。我建议您编写自己的选项解析器。我建议更容易做到-9 '' - 即。为选项传递空参数,然后执行grep_exp=${OPTARG:-default}。可能重复optional argument for getopt。或者移至getopt,它更易于使用、便携性较差,并且支持:: 的可选参数。

标签: bash getopts


【解决方案1】:

我会从选项解析代码中取出业务逻辑:

grep_expr=""
has_8=false
has_9=false

while getopts ":h89:" opt; do
    case $opt in 
        h) do_some_h_thing ;;
        8) has_8=true ;;
        9) has_9=true; grep_expr=$OPTARG ;;
        :) echo "Error: missing argument for option -$OPTARG" >&2; exit 1 ;;
        *) echo "Error: unknown option -$OPTARG" >&2; exit 1 ;;
     esac
done
shift $((OPTIND - 1))

if $has_8 || ($has_9 && [[ -z $grep_expr ]]); then
    for file in analysis/connection_history.txt ; do
        ftx $file | layoutcc
        printf "%s" "$(<$file)"   # why not `cat "$file"` ?                                                    
        echo ;                    #
        ft1 ;
    done 
elif $has_9; then
   for file in analysis/connection_history.txt ; do
        ftx "$file" | layoutcc
        # following pipeline can be greatly simplified
        grep "$grep_expr" $file |
          perl -ne 'print if /-201[8-9]/' | 
          perl -pe 's/......-2019/\e[1;32m$&\e[0m/g' 
        echo
        ft1
    done 
 else
    echo "Error: you must use one of '-8' or '-9 basic_regex'" >&2
    exit 1
fi

【讨论】:

  • 我可能错过了,很抱歉,有点新,所以我只是在谷歌上学习东西。但是 -8 和 -9 选项不是我需要检查的。
  • 我可能会错过它,有点新,所以我只是在谷歌上学习东西。但 -8 和 -9 选项不是我需要检查的。 -9 选项使用用户定义的变量。它可以是他们键入的任何内容。我需要检查该变量...AKA,如果他们使用“ goat -9 dog ”,它将为狗搜索文件并在这种情况下显示 2018 年或 2019 年事件的结果。然后将所有 2019 年的日期涂成绿色。 ..... 但是,如果他们只是尝试使用“goat -9”,则它不会执行任何操作,因为未定义变量。 .
  • 我理解你的问题。花点时间查看我的答案。
  • 我明天会花更多的时间在它上面。谢谢你的时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-06
  • 1970-01-01
相关资源
最近更新 更多