First of all, there is no need to cd
into a directory in order to open a file in it. You can just use the full (or relative) path to the file. So instead of cd foo
and then libreoffice bar.doc
, you can just do libreoffice foo/bar.doc
.
Now, the next issue is opening multiple files. When you run a script, each command in that script will be executed sequentially and the script will wait for the first command to finish before launching the second. That's why your script was only opening one file, it was waiting until the first libreoffice
process exited to move on to calling evince
.
If all you need is to open two files, all you need is:
#!/bin/bash
libreoffice '/home/me/Dropbox/00-School/03-TA POLI 397/Assignment'/grading-instructions.odt &
evince '/home/me/Dropbox/00-School/03-TA POLI 397/POLI 397 Assignment.pdf '
You don't even need a script for this. You can paste this directly into a terminal:
libreoffice '/home/me/Dropbox/00-School/03-TA POLI 397/Assignment'/grading-instructions.odt &
evince '/home/me/Dropbox/00-School/03-TA POLI 397/POLI 397 Assignment.pdf'
The trick is to use the &
after the first command to send it to the background and allow the shell to move to the next command.
You can simplify further by using xdg-open
which will find the default program to handle each file and with xdg-open
, you don't even need to send the first to the background since xdg-open
will return as soon as it launches whatever program is used for the relevant file type:
#!/bin/bash
xdg-open '/home/me/Dropbox/00-School/03-TA POLI 397/Assignment'/grading-instructions.odt
xdg-open '/home/me/Dropbox/00-School/03-TA POLI 397/POLI 397 Assignment.pdf'
With this in mind, you can write a general script that takes a list of N file names as input and opens each of them:
#!/bin/bash
Iterate over all file names given on the command line
for file in "$@"; do
## open each file
xdg-open "$file"
done
If you save that as ~/bin/openFiles.sh
and make it executable (chmod a+x ~/bin/openFiles.sh
), you can now run:
openFiles.sh '/home/me/Dropbox/00-School/03-TA POLI 397/Assignment'/grading-instructions.odt'
'/home/me/Dropbox/00-School/03-TA POLI 397/POLI 397 Assignment.pdf'
and it will open each file in the appropriate tool and will work with as many files as you want to give it.
libreoffice '/home/me/Dropbox/00-School/03-TA POLI 397/Assignment'/grading-instructions.odt & evince '/home/me/Dropbox/00-School/03-TA POLI 397/POLI 397 Assignment.pdf &'
. No need forcd
or a script or anything else. Is that really all you are asking? I mean, is it just a question of opening two specific files? – terdon Mar 15 '21 at 17:32