Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

shell case statement patterns...[SOLVED]

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
4 posts • Page 1 of 1
Author
Message
Sadako
Advocate
Advocate
User avatar
Posts: 3792
Joined: Thu Aug 05, 2004 5:50 pm
Location: sleeping in the bathtub
Contact:
Contact Sadako
Website

shell case statement patterns...[SOLVED]

  • Quote

Post by Sadako » Tue Jun 06, 2017 8:59 pm

Hi, I'm trying to use a case statement in a shell script to match a hexadecimal string.

Code: Select all

case "${FOO}" in
    [0-9a-eA-E]* ) echo BAR ;;
esac
This pretty much works, but it'll match anything that simply begins with a hex digit.

Supposedly, prefixing a pattern with '+' should make it match "one or more" occurances, which is exactly what I'm looking for, but I can't figure out how it should be used...

Code: Select all

case "${FOO}" in
    ( +([0-9a-eA-E]) ) echo FOO ;;
    +([0-9a-eA-E]) ) echo FOO ;;
    (+([0-9a-eA-E])) echo FOO ;;
    +([0-9a-eA-E]) echo EEE ;;
esac
Each of the above (tested separately) results in "syntax error near unexpected token `('".
Various quoting and putting the pattern in a variable hasn't worked, either...

I'm testing with both bash and dash atm, but would rather avoid any bash specific alternatives with '[[', and I am aware of potential locale issues with the [0-9a-eA-E] pattern.

Anyone know the correct syntax for this?

Thanks.
"You have to invite me in"
Top
Sadako
Advocate
Advocate
User avatar
Posts: 3792
Joined: Thu Aug 05, 2004 5:50 pm
Location: sleeping in the bathtub
Contact:
Contact Sadako
Website

  • Quote

Post by Sadako » Wed Jun 07, 2017 12:09 am

Upon further reading, seems that '+(pattern)' for matching one or more relies on shopt -s extglob, meaning it's bash-specific, or at the very least not posix.

D'oh...
"You have to invite me in"
Top
mv
Watchman
Watchman
User avatar
Posts: 6795
Joined: Wed Apr 20, 2005 12:12 pm

  • Quote

Post by mv » Wed Jun 07, 2017 7:30 am

POSIX shell patterns are not nearly as powerful as regular expressions.
In your case you can come through with negating:

Code: Select all

case ${FOO:-x} in
*[!0123456789abcdefABCDEF]*)
  echo "FOO not hex";;
esac
The :-x is there to make an undefined or empty string to be not considered as hex. Note that [!0-9a-fA-F] would be locale dependent which is probably not what you want. (BTW: "F" still is hex...)
Top
Sadako
Advocate
Advocate
User avatar
Posts: 3792
Joined: Thu Aug 05, 2004 5:50 pm
Location: sleeping in the bathtub
Contact:
Contact Sadako
Website

  • Quote

Post by Sadako » Wed Jun 07, 2017 3:40 pm

mv wrote:POSIX shell patterns are not nearly as powerful as regular expressions.
In your case you can come through with negating:

Code: Select all

case ${FOO:-x} in
*[!0123456789abcdefABCDEF]*)
  echo "FOO not hex";;
esac
The :-x is there to make an undefined or empty string to be not considered as hex. Note that [!0-9a-fA-F] would be locale dependent which is probably not what you want.
I had return this

Code: Select all

PATTERN='[0-9a-fA-F]'

ishex() {
    [ -s "${1}" ] || return 1

    FOO="${1}"

    while [ ! "${FOO}" = "${FOO#${PATTERN}}" ]; do
    	FOO="${FOO#${PATTERN}}"
    done

    [ -z "${FOO}" ]
}
which got the job done, but that negation looks like a rather elegant solution I don't think I would have thought of myself, so tyvm for that.

However, I found BAR="$((0x${FOO}))" returns an error if the value isnt hex, and gives me the decimal value of it at the same time.
(BTW: "F" still is hex...)
Ahhhhhh! Brainfart! <.< >.>
"You have to invite me in"
Top
Post Reply

4 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy