Why is OPTIND messing my positional params?

后端 未结 2 939
时光取名叫无心
时光取名叫无心 2021-01-28 07:22

I have this function:

    sgrep () 
{ 
    local OPTIND;
    if getopts i o; then
        grep --color=auto -P -in \"$1\" \"$2\";
        shift $((OPTIND-1));
           


        
2条回答
  •  孤独总比滥情好
    2021-01-28 08:10

    I have changed the positional params to one bigger:

    sgrep () 
        { 
            local OPTIND;
            if getopts i o; then
                grep --color=auto -P -in "$2" "$3";
                shift $((OPTIND-1));
            else
                grep --color=auto -P -n "$1" "$2";
            fi | sed -E -n 's/^([0-9]+).*/\1/p' | xargs -I{} vim +"{}" "$2";
            stty sane
        }
    

    Which works, but I don't like it. In one branch I have to use bigger positions, because of the option, but on the other branch without option used, positions do not change. It is messy

    I have tried to put shift $((OPTIND-1)) immediately after then but for no avail.

提交回复
热议问题