LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   help on shell script as a wrapper to launch default programs according to input file (http://www.linuxquestions.org/questions/programming-9/help-on-shell-script-as-a-wrapper-to-launch-default-programs-according-to-input-file-386182/)

daveiro 11-24-2005 01:29 PM

help on shell script as a wrapper to launch default programs according to input file
 
Hello!

I'm using marvellous program Freemind, see freemind.sourceforge.net/wiki/index.php/Main_Page, everyone should find this program useful for lots of daily tasks!

It's a Java program and it allows us to launch an external program where the first parameter is a path of a file. In Linux it defaults to konqueror which is not good to open files like OpenOffice.org documents or Adobe PDFs in a user-friendly way, since I'm using Gnome as default desktop.

So I thought on using a shell script to parse the file received, detect the file extension and then launch the corresponding program.

I have very little experience on shell scripting so I ask for help on how to do this.

The script receives a path with this format:

file:/home/myusername/path/to/file.that.can.have.dots.before.extension.pdf

So the script will have to extract what's on the right of : (this I already think it's working)

Then it will have some kind of WHILE command to go trough the string to find the extension, save it in some variable, then have something like a CASE that for predefined extension types will then launch the corresponding program.

Here's what I got until now:

Code:

#!/bin/bash
PDFPROG=/usr/bin/acroread
ODTPROG=/usr/bin/oowriter

PATH=`echo $1 | awk -F'file:' '{print $2;}'`
$PDFPROG $PATH

But it's not working :(

I try this

Code:

fmlaunch file:/home/david/actual/academico/Phd/bibliografia/function/bickhard/ErrorDynamics.pdf
and I get this:

Code:

/usr/bin/acroread: line 8: ls: command not found
/usr/bin/acroread: line 8: sed: command not found
/usr/bin/acroread: line 9: dirname: command not found
/usr/bin/acroread: line 17: dirname: command not found
/usr/bin/acroread: line 102: dirname: command not found
/usr/bin/acroread: line 109: dirname: command not found
/usr/bin/acroread: line 117: ls: command not found
/usr/bin/acroread: line 117: sed: command not found
/usr/bin/acroread: line 118: expr: command not found
ERROR: Cannot find installation directory.

I wonder if it's because acroread is also a script?

tlowk 11-24-2005 04:08 PM

#!/bin/bash
PDFPROG=/usr/bin/acroread
ODTPROG=/usr/bin/oowriter

FILEPATH=$(echo $1 | sed 's/^file://')
EXTENSION=$(echo 1 | sed 's/.*\.//g')

case "$EXTENSION" in
'pdf')
$PDFPROG "$FILEPATH"
;;
'odt')
$ODTPROG "$FILEPATH"
;;
*)
xmessage -buttons 'ok:1' "unknown type $FILEPATH"
;;
esac

#end
use double quotes for variables that can contain spaces (even better don't use spaces
in a file or directory name at all)

and don't use PATH since it can block other scripts if you use it with export, also it makes the script less readable

regards,

tlowk

daveiro 11-24-2005 04:20 PM

Thank you for your help on the script! :)
On the second echo you forgot a dollar sign right?

Meanwhile I found another alternative, one just needs to use the command

gnome-open

in Freemind prefs and Gnome automagically opens the default app for the file tha I clicked in Freemind.


All times are GMT -5. The time now is 04:54 PM.