2

Suppose I want to associate PPT files to be opened with a bash script that converts it to PDF then opens it with the approperiate PDF editor. How do I do that?

Conversion will be done with uniconv as I learnt from another question

uniconv -f pdf presentation.odt

I believe I have to change "presentation.odt" with the filename that triggered this script

4

Do you mean that the script will be called with (for example) presentation.odt as an argument? The argument is available as "$1" in the script (the double quotes are required if the file name contains characters like spaces which the shell would otherwise expand). You can construct the name of the PDF file by stripping away the .odt suffix: ${1%.odt}. Note that it's unoconv, not the unrelated uniconv.

#!/bin/sh
unoconv -f pdf "$1"
appropriate-pdf-editor "${1%.odt}.pdf"
  • 1
    appropriate-pdf-editor could be xdg-open to use whichever pdf viewer is currently set as default. – geirha Aug 21 '11 at 13:43
1

In a shell script, you can access command line parameters through the variable 1, 2, 3, ..., as in

#!/bin/bash

echo "$1"
echo "$2"
echo "$3"
# ...

If you need it, "$0" represents the script name itself.

You can also reassign these variable, and shift them, to access more than 9 parameters.

0

If you want to find all .odt files and feed them into unoconv, you can use the find tool to find all of them, then pipe it into xargs, which can do parallel processing.

find * -type f -iname "*.odt" | xargs -i -P12 "uniconv -f pdf {}"

This will find all files in the running directory, recurssively, piping files that it finds into xargs, which will run up to 12 processes silmultaneously (use -P0 for unlimited processes).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.