Register
It is currently Wed Oct 22, 2014 12:24 am

Case statement matching end of a string


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Mon Dec 06, 2010 5:36 pm   

Joined: Mon Dec 06, 2010 5:27 pm
Posts: 9
Hello,

I'm trying to match in a case statement based on file extension. Essentially, I want to do something different for .mp3 files and .ogg files.

I have this case statement:

Code:
   case musicfile in
   
   *.ogg\"$)
      echo "Processing .ogg file"
   ;;

   *.mp3\"$)
      echo "Processing .mp3 file"
   ;;

   *)
      echo "Warning: Unexpected file type: " $musicfile
   ;;

   esac


But this element always comes up in the default case, instead of the .ogg case:
"/home/testuser/gpodder-downloads/GNU World Order Linux OggCast/gnuWorldOrder_6x02.ogg"

The trailing double quote is necessary because this case statement is in a loop that processes on a list of files, which could contain spaces in the path, so I used awk to wrap each line in double quotes.

Essentially, I just want to see if the string ends in '.mp3"' or '.ogg"'. I am clearly missing something but every example I see uses trivial regular expression matches. Any suggestions?


Top
 Profile  
 PostPosted: Mon Dec 06, 2010 11:38 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
well for starters.....

it would be

case $musicfile in

you forgot the $

:)


Top
 Profile WWW  
 PostPosted: Tue Dec 07, 2010 2:21 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Do your file names really contain "$ at the end? For me, just
Code:
case $file in
  *.txt) echo Text $file ;;
esac

works.


Top
 Profile  
 PostPosted: Tue Dec 07, 2010 11:36 am   

Joined: Mon Dec 06, 2010 5:27 pm
Posts: 9
@crouse: thanks :) I knew it was something silly like that. I haven't written shell script in years and I'm just getting back into it. It's amazing what you forget when you don't use it.

@choroba: The intent of that was to match specifically the end of a string. This is because some of the folder names that the podcasts are put into contain '.mp3' or '.ogg', and I want to be sure I match specifically against the file extention. I'm a bit rusty, maybe the $ isn't necessary? The " is necessary though, because each filename is wrapped in double quotes since the folder names sometimes contain spaces.

Thanks for the help!


Top
 Profile  
 PostPosted: Tue Dec 07, 2010 5:59 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
gunit888 wrote:
I'm a bit rusty, maybe the $ isn't necessary?

Case uses wildcard patterns, not regular expressions. $ has no special meaning there.


Top
 Profile  
 PostPosted: Tue Dec 07, 2010 7:43 pm   

Joined: Mon Dec 06, 2010 5:27 pm
Posts: 9
Ah, ok, then I can throw out the $.

Thanks for the clarification!


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP