Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-12-2004, 11:11 PM   #1
LQ Newbie
Registered: Apr 2004
Distribution: Fedora Core 3, Trustix 3.0
Posts: 8

Rep: Reputation: 0
Question Opening file with default application from shell script?

Hi all,

I am currently writing a simple file manager in a Bourne shell script for my studies.

I have the file selection sorted but need some help with the running or opening of the selected file based on its extension.

I want to be able to have the file open with the system default application

e.g *.gif - will open with system default associated editor.
*.html - will open with default browser.

I can make the call manually i.e 'monzilla $file', but I want to use current default application for file extension instead of me picking it.

Can you search for the default application and then make the call with results or is there a simple command that will find the application and run the file for you?

Are the associations kept in a config file or similar somewhere that I could 'grep' the application from using extensions of file as the search argument?

Thanks in advance.
Old 04-13-2004, 01:46 AM   #2
Senior Member
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
If I'm understanding what you're saying...

The only real 'default apps' are $EDITOR and $PAGER and $SHELL, itself. And that's as easy as '$EDITOR $1'. But there's nothing guaranteeing they're set. And as far as GUIs go, they all keep configs in files but most of them don't even have default apps - mostly just IDEs set that kind of stuff, like Gnome and KDE. But you'd have to test for what they were using as well. There's no '$ICEWMEDITOR' variable. Most file manager's do their own setting. So why not let the user define the default app themselves? Then use those internal variables. Create your own configs, in essence, without deciding on a default app (they may not be there) or trying to find out some default from somewhere else.

But, yeah, to find out my defaults (or some of them sometimes) you could try to parse ~/.emelfm/filetypes. Wouldn't work for many other people, though.

How's a file manager in shell work? A shell *is* a file manager.
Old 04-13-2004, 01:59 AM   #3
LQ Newbie
Registered: Apr 2004
Distribution: Fedora Core 3, Trustix 3.0
Posts: 8

Original Poster
Rep: Reputation: 0
It's is just little script to make us use some commands and send and receive variables to and from java. University subject stuff, not very useful just getting us started.

It allows you to pass a directory as argument when starting script or if no argument entered uses current directory, after checking for validity of passed directory, lits contents of directory and allows selection of file or directory. If directory selected, lists contents and allows selection again and so.

If it's a file checks type and offers user options based on type:

e.g if *.java - 'Do you want to edit or compile ?'

But I think the idea of getting user to enter application they wish to use or the action they wish to take is the way to tackle it.

Old 04-13-2004, 02:45 AM   #4
LQ Newbie
Registered: Nov 2003
Location: Orange County, CA, USA
Distribution: aix, rhel, ubuntu
Posts: 24

Rep: Reputation: 0
a quickie script, idea only. Hopefully that would be quick to try, or as an exercise to perfect by the student..


# $1 could be used to identify a command line argument, not used in the below ex.

alias javasourceme="javac $COMPILEOPTS"
alias javarunme="java $RUNOPTS"

menu() { echo "menu:"; echo ""; echo "1) compile program ?"; echo "2) run program ?"; echo ""; echo -e "Choice-> "\\c; read p; clear; }
# the above is all on one line, but can be seperate lines wherever the ; appears.

echo -e "enter program name: "\\c ; read PROGNAME

if [ "${}" != "${1}" ]; then
javasourceme $1
echo "You picked to compile a program with a .class (already compiled) extension, aborting ..."
exit 1

if [ "${PROGNAME%.class}" != "${1}" ]; then # % strips off .class from the right
# side (ext) of the
# file.class becomes file at this point.
javarunme $1
echo "You picked to run a program with a .java extension, aborting ..."
exit 1

# Single malt only, or is blended fine sometime ?
just kidding on the above.

Actually, I should have read my assignment more thoroughly. The above is written in korn shell, which I find more usefull than Bourne in many instances, certain particular differences may lie in the usage of the ${VARIABLE%.class}, in the using of read p for a menu, certainly in #!/bin/ksh (#!/bin/bsh instead). Sorry, but I do not have bsh currently loaded to check the above. Hope you find another suggestion useful if the the above does not work completely. I have used scripting before, and would see that the above has a good chance to work.

Last edited by MarkBurke; 04-13-2004 at 02:49 AM.
Old 04-13-2004, 02:52 AM   #5
LQ Newbie
Registered: Nov 2003
Location: Orange County, CA, USA
Distribution: aix, rhel, ubuntu
Posts: 24

Rep: Reputation: 0
In general, the desktop environment is in charge of deciding which application will open which file. The command line arugment comment above is generally correct I think, each command interpreter program will have a set of variables which it will look for.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
help on shell script as a wrapper to launch default programs according to input file daveiro Programming 2 11-24-2005 05:20 PM
changing default for pdf file opening moonmoth Linux - Software 7 07-27-2005 04:05 PM
opening file with .xls on linux shell prompt suchi_s Programming 1 07-28-2004 08:40 AM
Default mode for opening text file qcoder Programming 4 05-24-2004 07:08 PM
How to bring up application window to front from shell script. aleksas_m Linux - General 2 08-21-2003 02:22 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:21 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration