-1

I would like get ops to default to the -8 flag if someone runs the -9 and does not put anything for the OPTARG after the flag OR can i check to see if OPTARG is null and replace the empty value with a base value.

ft1 and ftx are just format functions.

When i run my goat -8 it prints out that file in question

When i run goat -9 SEARCH STRING it prints the file with only results of that search string.

If i run goat -9 and do not put a search string it does nothing because OPTARG is blank so the grep has nothing to search.

I would like it to be able to change the -9 to the -8 in the event a user does not enter a string after goat -9.

    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
}
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
Calvin
  • 29
  • 4
  • Please post: expected input, expected output. Please create an MCVE. How is `ft1` and `ftx` and `for file in ..` and all the rest of the script related to the question? And what is the question? – KamilCuk Feb 25 '19 at 17:14
  • simply put i would like to check if OPTARG is NULL and then place a default value in if it is. – Calvin Feb 25 '19 at 18:11
  • I don't think `getopts` supports that. I suggest you write your own option parser. And I suggest it's easier to do `-9 ''` - ie. pass empty argument for the option,and just do `grep_exp=${OPTARG:-default}`. Maybe duplicate [optional argument for getopt](https://stackoverflow.com/questions/11517139/optional-option-argument-with-getopts). Or move to [getopt](https://linux.die.net/man/1/getopt) which is easier to use, less portable and supports optional arguments with `::`. – KamilCuk Feb 25 '19 at 20:02

1 Answers1

0

I would take the business logic out of the option parsing code:

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
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • i may be missing it so sorry, kinda new so im just learning things as i google them. But the -8 and -9 options are not what i need to checking . – Calvin Feb 25 '19 at 22:39
  • i may be missing it, kinda new so im just learning things as i google them. But the -8 and -9 options are not what i need to check. The -9 options is using a user defined variable. It can be anything they type. I need that variable checked... AKA if they use " goat -9 dog " it would grep the file for dog and display those results for 2018 or 2019 events in this case. and then color all the 2019 dates green. ..... But if they just try and use " goat -9" it does nothing as the variable is not defined. . – Calvin Feb 25 '19 at 22:47
  • I understand your question. Take some more time reviewing my answer. – glenn jackman Feb 25 '19 at 23:18
  • i will spend more time with it tomorrow. Thank you for you time. – Calvin Feb 25 '19 at 23:43