Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > How to check file extensions ?

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 03-08-2005   #1 (permalink)
Just Joined!
 
Join Date: Mar 2005
Posts: 9
How to check file extensions ?

Hi guys, i'm sure the question been asked before, but i'm a Linux script newbie, so i don't really know how to get things started. i have a task of reading in particular filename and finding out the particular extensions, so that i could do some processing on that file with that extension. like for a MyProgram.java file, i would open an new vi editor to allow edit, a MyWebPage.html, i would open an new browswer to view the page.

How do i check the file extensions ? I thought of several algorithms using the string expressions provided. Could anyone give any pointers ??

regards
tanweeyang is offline  


Reply With Quote
Old 03-08-2005   #2 (permalink)
Linux Guru
 
kkubasik's Avatar
 
Join Date: Mar 2004
Location: Lat: 39:03:51N Lon: 77:14:37W
Posts: 2,397
Send a message via AIM to kkubasik
That would requrire basicaly a massive parsing engin, the the specifics are elusive, and language specific, for example, the newer Java API's have some nice string tokenizers that make this process extrodinarly simple. But also, note that linux stores the information needed to open a file with the proper application in the file, not the extension, the extension is just a human readable component that windows decided to rely on, and became the windows standard, but if you ever write a script, you learn that the header is

#!/bin/bash

telling linux to open the program in /bin/bash.. to create a parsing engine to handle something of this magnetude is really a waste of time, but thats all it would be, something like perl (with its large library support) will make the mechanics of the task simpler, but in the end, its gonna be one massive *** switch/ifelse chain to get it done.
__________________
Avoid the Gates of Hell. Use Linux
A Penny for your Thoughts

Formerly Known as qub333
kkubasik is offline   Reply With Quote
Old 03-08-2005   #3 (permalink)
Just Joined!
 
Join Date: Mar 2005
Posts: 9
Well no choice though..

Quote:
Originally Posted by qub333
That would requrire basicaly a massive parsing engin, the the specifics are elusive, and language specific, for example, the newer Java API's have some nice string tokenizers that make this process extrodinarly simple. But also, note that linux stores the information needed to open a file with the proper application in the file, not the extension, the extension is just a human readable component that windows decided to rely on, and became the windows standard, but if you ever write a script, you learn that the header is

#!/bin/bash

telling linux to open the program in /bin/bash.. to create a parsing engine to handle something of this magnetude is really a waste of time, but thats all it would be, something like perl (with its large library support) will make the mechanics of the task simpler, but in the end, its gonna be one massive *** switch/ifelse chain to get it done.
yeah how i wish i could have used java,but problem this is part of my assignment specifications and i got to adhere to the specs. I tried using string expression like $ expr index program.java .java" it returns the index of the dot and not whether its a java file extension.

It will be great if anyone out there could redirect me to any website or existing threads with similar questions ?

regards
tanweeyang is offline   Reply With Quote
Old 03-08-2005   #4 (permalink)
Linux User
 
IsaacKuo's Avatar
 
Join Date: Feb 2005
Location: Baton Rouge, LA, USA
Posts: 287
Send a message via ICQ to IsaacKuo Send a message via Yahoo to IsaacKuo
Re: Well no choice though..

Quote:
Originally Posted by tanweeyang
yeah how i wish i could have used java,but problem this is part of my assignment specifications and i got to adhere to the specs.
Umm...what are your assignment specifications? You haven't said anything about what sort of tools you are allowed to use.
__________________
Isaac Kuo, ICQ 29055726 or Yahoo mechdan
IsaacKuo is offline   Reply With Quote
Old 03-08-2005   #5 (permalink)
Just Joined!
 
Join Date: Mar 2005
Posts: 9
Tools i am allowed ot use

I been provided with several predefined java classes, and i am supposed to use this java programs to help me. The java programs provided each have a seperate, one being able to display the contents of a piped result into a list box, a dialog box that returns 1 or 0 to standard in and a simple JTextArea for simple editing.

I need to check the file extension, in order to prompt the apporiate action, like .java files to either compile or edit, class files to run etc..

I have to use the Bourn shell to write a script to do just that.
tanweeyang is offline   Reply With Quote
Old 03-08-2005   #6 (permalink)
Just Joined!
 
Join Date: Feb 2005
Posts: 9
Send a message via Yahoo to ninja972
I am not sure I understood you want but if you just need a shell script to get file extention:
Code:
echo <filename> | grep -o '\.[^.]*$'
will output the the extention of the file.
ninja972 is offline   Reply With Quote
Old 03-10-2005   #7 (permalink)
Just Joined!
 
Join Date: Mar 2005
Posts: 5
I think you can use case st. of bash like this

#!/bin/bash

filenames=`ls`
for eachFile in $filenames
do
case $eachFile in
.c) echo $eachFile is a C file
;;
.java) echo $eachFile is a Java source.
;;
esac
done
asvija is offline   Reply With Quote
Old 03-10-2005   #8 (permalink)
Just Joined!
 
Join Date: Mar 2005
Posts: 5
Sorry, I missed out * in the cases. It should be like this

case $eachFile in

*.c) #some actions
;;
*.java) #some thing else
;;

esac
asvija is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 08:40 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2