Find the answer to your Linux question:
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 ...
  1. #1
    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

  2. #2
    Linux Guru kkubasik's Avatar
    Join Date
    Mar 2004
    Location
    Lat: 39:03:51N Lon: 77:14:37W
    Posts
    2,396
    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.
    Avoid the Gates of Hell. Use Linux
    A Penny for your Thoughts

    Formerly Known as qub333

  3. #3
    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 ass 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

  4. #4
    Linux User IsaacKuo's Avatar
    Join Date
    Feb 2005
    Location
    Baton Rouge, LA, USA
    Posts
    292

    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

  5. #5
    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.

  6. #6
    Just 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:
    Code:
    echo <filename> | grep -o '\.&#91;^.&#93;*$'
    will output the the extention of the file.

  7. #7
    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

  8. #8
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •