LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-27-2023, 02:34 PM   #1
Faki
Member
 
Registered: Oct 2021
Posts: 574

Rep: Reputation: Disabled
Detecting invalid options


I process positional parameters in the following way. I use the pattern `(-*)` for detecting invalid options - is this good or can one be more precise?

Code:
  local parg=""
  while (( $# > 0 )); do
    parg="$1"
    case $parg in
      ##------------------------------------------------
      (-p+([[:digit:]]))
          val="${parg:2}"    
          shift 1
          ;;
      ##------------------------------------------------
      ("--")
          break ;;
      (-*)
          printf '%s\n' "$parg Unrecognised Predicate"
          break ;;
      (*)
          break ;;
    esac
  done

Last edited by Faki; 04-27-2023 at 03:54 PM.
 
Old 04-28-2023, 02:42 AM   #2
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
It's okay.
I would place the shift before the done so it's immediately visible that the loop won't run forever.
And shift out all -* args.
Code:
  while (( $# > 0 )); do
    case $1 in
    ##------------------------------------------------
    (-p+([[:digit:]]))
      val="${1:2}" ;;
    (-p*)
      printf '%s\n' "${1:2} malformed number" ;;
    (--)
      shift; break ;;
    (-*)
      printf '%s\n' "$1 Unrecognised Predicate"
      shift; break ;;
    (*)
      break ;;
    esac
    shift
  done
 
Old 04-28-2023, 09:00 AM   #3
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Interesting. You do this on a regular basis? In many instances others shift the required amount within the case. I wonder if - on its own should behave the same as --. Is it correct to say that (-*) should be described by "Unrecognised Predicate" ?

Last edited by Faki; 04-28-2023 at 09:25 AM.
 
Old 04-28-2023, 10:29 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Some old Unix commands have a single - as a "last option" indicator.
But Posix says it must be --
 
1 members found this post helpful.
Old 04-29-2023, 04:35 PM   #5
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by MadeInGermany View Post
It's okay.
I would place the shift before the done so it's immediately visible that the loop won't run forever.
And shift out all -* args.
Code:
  while (( $# > 0 )); do
    case $1 in
    ##------------------------------------------------
    (-p+([[:digit:]]))
      val="${1:2}" ;;
    (-p*)
      printf '%s\n' "${1:2} malformed number" ;;
    (--)
      shift; break ;;
    (-*)
      printf '%s\n' "$1 Unrecognised Predicate"
      shift; break ;;
    (*)
      break ;;
    esac
    shift
  done
I do not see that it shifts out all -* args. Because on the first one, you exit the loop.
 
  


Reply

Tags
bash



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Detecting invalid options occurring between known option arguments Faki Linux - Newbie 4 12-13-2021 07:14 AM
USB devices stop detecting (or un-detecting). Mleahy Linux - Desktop 1 07-31-2011 02:08 AM
RedHat 9 is not detecting the LAN card, but Win'98 is detecting the LAN card SKYNAT Linux - Newbie 4 12-28-2007 10:25 PM
squirremail ...it says invalid user or invalid password. rnj Fedora 9 10-25-2004 09:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration