Results 1 to 8 of 8
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 ...
- 03-08-2005 #1Just 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
- 03-08-2005 #2
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 ass switch/ifelse chain to get it done.
- 03-08-2005 #3Just Joined!
- Join Date
- Mar 2005
- Posts
- 9
Well no choice though..
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.
Originally Posted by qub333
It will be great if anyone out there could redirect me to any website or existing threads with similar questions ?
regards
- 03-08-2005 #4
Re: Well no choice though..
Umm...what are your assignment specifications? You haven't said anything about what sort of tools you are allowed to use.
Originally Posted by tanweeyang Isaac Kuo, ICQ 29055726 or Yahoo mechdan
- 03-08-2005 #5Just 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.
- 03-08-2005 #6Just Joined!
- Join Date
- Feb 2005
- Posts
- 9
I am not sure I understood you want but if you just need a shell script to get file extention:
will output the the extention of the file.Code:echo <filename> | grep -o '\.[^.]*$'
- 03-10-2005 #7Just 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
- 03-10-2005 #8Just 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


Reply With Quote
