last char from a string

i have a script that reads a plain text file. (its a ksh, and i can use bash also)
each line of the file is a fullpath of a file. that makes the list huge.
i need to add a functionalitie to that script, i have to be able to add

/usr/*

or

/usr/

and with that reference all the files and folders inside.
so i need to get the last char from the line and do a case to match / or *, and use the default for the nomal fullpath and file
how do i get that last character ?

Pattern-matching for fun and profit!

# x="/unix/*"
# y="/unix/"
# z="/unix"
# [[ $x == *@(/|\*) ]] && echo TRUE || echo FALSE
TRUE
# [[ $y == *@(/|\*) ]] && echo TRUE || echo FALSE
TRUE
# [[ $z == *@(/|\*) ]] && echo TRUE || echo FALSE
FALSE

ok. i never saw something like that :smiley:

after some time trying to understand that, some googling, and some more thinking, i think i got what it does
thats a nice feature i never read about !!
i am sure alot of people will scream at me cause is really hard to read, but it does the trick (i will have to make a 10 lines comment for one if condition :o )

thanks for the reply

This should do it:

case "$line" in
  *\*) echo "It ends in asterisk";;
  */) echo "It ends in slash";;
  *) echo "It ends in neither slash nor asterisk";;
esac

If you have issues with asterisks being expanded into filename lists, then you can precede the above with "set -f" (disable filename expansion), and add a "set +f" after (to reinstate filename expansion).

i dont know about that case, i will have to run some tests, but i have a feeling it can give me some false positives, can i use a regular expr ? so i can make it ^./$ or ^.*/\$,

Yep - you can use regular expressions like "^" to anchor it to the start of a line, "^[a-z]" to anchor it to a line starting with a letter, or any other variation you choose.
(It's Korn and POSIX shell syntax, but I think it's good for Bash too. I'm not sure how much of it works with basic Bourne shell though.)
Basically have a play; it's like a stuck vending machine - you just have to find the right place to give it a thump.

Hi, prowla

On my GNU/Linux (Debian), bash and pdksh case expressions are filename expressions, not regular expressions:

and

Is this different on your system? ... cheers, drl


  1. a-z ↩︎