0

I have the following function that is supposed to write strings in new lines. There is a warning option, so that when -w is set by the user, some lines get coloured in red. -w allows an optional numeric argument -wNUM. If NUM is not provided, warn is set to 1 so that only the first line is coloured red.

I am encountering a problem with calling the command

printfm -w -- "First line" "second line").

I have included local warn="1" before the case statement.

  ("-w"|"--warning")
     local warn="1"
     case "$2" in

The function is listed here

printfm ()
{
  # Process command line options
  shortopts="hVw::"
  longopts="help,version,warning::"

  opts=$(getopt -o "$shortopts" -l "$longopts" -n "${0##*/}" -- "$@")

  if [ $? -ne 0 ]; then
    local shorthelp=1  # getopt returned (and reported) an error.
    return
  fi

  local f=0
  
  if [ $? -eq 0 ]; then
    eval "set -- ${opts}"
    while [ $# -gt 0 ]; do
      case "$1" in
        ("-V"|"--version")
          printf "%s\n" "V01 Jul 2021 Wk27"
          declare -i f=0
          shift
          break
          ;;
        ("-h"|-\?|"--help")
          printf "%s\n" "Print strings."
          declare -i f=0
          shift
          break
          ;;
        # ------------------------------------
        ("-w"|"--warning")
          case "$2" in
            (+([[:digit:]])) 
              local -r warn="$2"
              shift 2
              ;;
            (*) 
              local -r warn="1"
              shift 2
              ;;
            esac
            declare -i local f=1
            ;;
          # ----------------------------------------
          (--)
            declare -i local f=1
            shift
            break
            ;;
        esac
      done
    fi

    red=$(tput setaf 9)  
    rgl=$(tput sgr0)
  
    # Print normal text or warnings
  
    if [[ -v $f ]] && (( f != 0 )); then

      # print normal multi-line text
      [[ ! -v $warn ]] && printf '%s\n' "$@"

      # print multi-line warnings

      rge='^[0-9]+$'
      if [[ -v $warn && "$warn" == "1" ]]; then
        printf '%s\n' ${red}"$1"${rgl}  # first line red
        printf '%s\n' "${@:2}"          # remaining, uncoloured
      elif [[ -v $warn && "$warn" =~ $rge ]]; then
        printf '%s\n' ${red}"$@"${rgl}  # all lines red
      fi

    fi
    return 0
  }
15
  • 1
    It looks like you forgot to actually ask a question. What is it you need here? Are you trying to understand why the strings are not printed? If so, we would need to know how you call the script. All we have is the function, not the rest of the script and we don't know how you are running it. By the way, -n warn is testing the string warn, not the variable $warn so that will always be true. Commented Jul 23, 2021 at 12:49
  • The function itself has a syntax error. Have you tried running it? Do you not get line 36: syntax error near unexpected token '(' and line 36: ' (+([[:digit:]])) '? Commented Jul 23, 2021 at 13:00
  • I have been running printfm -w -- "Line 1" "Line 2" and printfm -w2 -- "Line 1" "Line 2" Commented Jul 23, 2021 at 13:05
  • Please edit your question and i) ask something; ii) include what shell you are running this in. In bash, you should get a syntax error with what you have posted. Finally, iii) when asking, it is really helpful to first debug and isolate the problem. We don't need to understand ~80 lines of code to help you, all we need is a few lines that can reproduce the issue. This is also the first step in any debugging process: start removing things until you can find the minimal code that reproduces the problem so we don't waste time looking at irrelevant parts. Commented Jul 23, 2021 at 13:08
  • What is this supposed to do: (+([[:digit:]]))? Specifically the +? Commented Jul 23, 2021 at 13:11

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.