The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
md5 hash a string or char array in SCO jcarter2333 High Level Programming 5 02-08-2007 04:09 PM
replacing char with string phani_sree High Level Programming 1 11-20-2006 08:57 AM
string of 7 char length always... thanuman UNIX for Dummies Questions & Answers 3 04-12-2005 12:51 PM
Compare Char to String Phobos High Level Programming 3 04-09-2005 11:01 AM
Number of specific char in a string. gio123bg Shell Programming and Scripting 7 12-19-2003 02:27 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 12-07-2007
broli's Avatar
broli broli is offline
Registered User
  
 

Join Date: Dec 2007
Location: Argentina
Posts: 215
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
Code:
/usr/*
or
Code:
/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 ?
  #2 (permalink)  
Old 12-07-2007
gus2000 gus2000 is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 157
Pattern-matching for fun and profit!

Code:
# x="/unix/*"
# y="/unix/"
# z="/unix"
# [[ $x == *@(/|\*) ]] && echo TRUE || echo FALSE
TRUE
# [[ $y == *@(/|\*) ]] && echo TRUE || echo FALSE
TRUE
# [[ $z == *@(/|\*) ]] && echo TRUE || echo FALSE
FALSE
  #3 (permalink)  
Old 12-07-2007
broli's Avatar
broli broli is offline
Registered User
  
 

Join Date: Dec 2007
Location: Argentina
Posts: 215
ok. i never saw something like that

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 )

thanks for the reply
  #4 (permalink)  
Old 12-07-2007
prowla prowla is offline
Read Only
  
 

Join Date: Nov 2007
Posts: 165
This should do it:
Code:
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).
  #5 (permalink)  
Old 12-07-2007
broli's Avatar
broli broli is offline
Registered User
  
 

Join Date: Dec 2007
Location: Argentina
Posts: 215
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 ^.*/\*$,
  #6 (permalink)  
Old 12-07-2007
prowla prowla is offline
Read Only
  
 

Join Date: Nov 2007
Posts: 165
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.
Closed Thread

Bookmarks

Tags
linux, regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:07 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0