LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Opening file with default application from shell script? (https://www.linuxquestions.org/questions/programming-9/opening-file-with-default-application-from-shell-script-169383/)

bourbon_beast 04-12-2004 10:11 PM

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.
or
*.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.

slakmagik 04-13-2004 12:46 AM

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. :D

bourbon_beast 04-13-2004 12:59 AM

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.

Thanks

MarkBurke 04-13-2004 01:45 AM

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

#!/bin/ksh

# $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 [ "${PROGNAME%.java}" != "${1}" ]; then
javasourceme $1
else
echo "You picked to compile a program with a .class (already compiled) extension, aborting ..."
exit 1
fi

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

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

MarkBurke 04-13-2004 01:52 AM

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.


All times are GMT -5. The time now is 04:20 AM.