Sunday, May 5, 2024
 Popular · Latest · Hot · Upcoming
16
rated 0 times [  16] [ 0]  / answers: 1 / hits: 158130  / 2 Years ago, wed, december 1, 2021, 12:44:14

Ok, so running gedit myfile.txt works well. But what about opening a file from inside a bash script, using the default desktop app linked to the filetype?



I've tried below, which works great when run manually in terminal, but when I put it in a bash file, nothing happens:



#!/bin/bash
xdg-open "myfile.txt"&


What should I do instead?



Please note that I need the file to stay open after the terminal is closed as well.


More From » bash

 Answers
6

I think your script should work. But you might add something to it to get a little more information:



#!/bin/bash
T=`xdg-mime query filetype $1`
echo "opening file " $1 " of type " $T "with " `xdg-mime query default $T`
xdg-open $1
echo "finished script"


when running this script (named my_open.sh) in a terminal like this:



my_open.sh path/to/somefile.txt


I get the following output:



opening file  path/to/somefile.txt  of type  text/plain with  gedit.desktop
finished script


which tells me that the path to the file is ok, the mimetype is recognized and the desktopfile which is used to open the file is ok as well. And gedit opens with the file in question.



Now when run on another file:



my_open.sh path/to/README


I get the following output:



opening file  path/to/README  of type  text/x-readme with
finished script


Note the different mimetype and the missing desktop file.
Nevertheless, xdg-open opens the default for all text files (gedit).



So, you might want to add something like this to your script and see if you get unexpected output (which you can then add to your question...).


[#40917] Wednesday, December 1, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ainsbeave

Total Points: 280
Total Questions: 98
Total Answers: 105

Location: Faroe Islands
Member since Sat, Aug 20, 2022
2 Years ago
;