2

I defined a function in my .bashrc that allows me to open e.g. pdf files from bash by running copen myfile.pdf:

function copen {
    if [[ $# -eq 1 ]] ; then
        xdg-open "$1" > /dev/null 2>&1 & disown
        return 1
    fi
}

By running this command, I execute e.g. Okular in the background, redirect STDOUT and STDERR to /dev/null and disown it from bash. However, after running copen myfile.pdf I still get output printed to bash (QSqlQuery::exec: database not open multiple times) and I don't understand why. How do I suppress that kind of output?

Note that the problem persists when changing the code to nohup xdg-open "$1" > /dev/null 2>&1 &.

closed as off-topic by ilkkachu, Eric Renouf, Totor, Anthony Geoghegan, HalosGhost Oct 21 '16 at 14:10

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – ilkkachu, Eric Renouf, Totor, Anthony Geoghegan, HalosGhost
If this question can be reworded to fit the rules in the help center, please edit the question.

1

Try this approach:

#!/usr/bin/bash

copen() {
    (($# == 1)) && xdg-open "$1" &>/dev/null & disown
}
copen "$1"

Run the script as follows: ./myscript SomePDFfile.pdf

The &>/dev/null is a shorthand for >/dev/null 2>&1 added in bash >4.

The copen function needs a parameter.

  • Some explenation? – MatthewRock Oct 21 '16 at 10:05
  • I keep getting the same output to my shell, QSqlQuery::exec: database not open. – cbaumhardt Oct 21 '16 at 10:08
  • @cbaumhardt Does xdm-open somefile.pdf open correctly from the terminal? – Valentin Bajrami Oct 21 '16 at 10:13
  • @val0x00ff: Bash doesn't know the command xdm-open. If I just enter xdg-open myfile.pdf in bash, the pdf opens correctly and a lot of overhead is printed in bash. – cbaumhardt Oct 21 '16 at 10:17
  • 3
    Your code as well as my original code was right from the very start, it just didn't work because made a very stupid mistake: I didn't source the code changes correctly, because I am new to using a recently installed bash framework (github.com/Bash-it/bash-it). I am so sorry for this and happily will accept your answer (which works, just like my initial code). – cbaumhardt Oct 21 '16 at 11:58

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