3

I was hoping to make a shell script that enabled me to open all the files that I use during a certain task. In this case, grading undergraduate assignments. The second file is in the directory above the first.

The following only opened the first file:

#! /bin/bash
cd '/home/me/Dropbox/00-School/03-TA POLI 397/Assignment'
libreoffice grading-instructions.odt
cd ../
evince 'POLI 397 Assignment.pdf'

How can I write a shell script that opens every file I'm looking to have opened, at the same time?

terdon
  • 100,812
  • I don't understand. All you need is 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 for cd 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
  • @terdon Hey, no, its a matter of opening more than two files, but my example had two. The reason is that for some things (like grading assignments) I consistently need the same group of files open at the same time. I don't want to keep them open all the time, even if they're in another workspace, so this lets me really quickly stop and start a session of work without having to type much or browse through multiple directories in my file manager to get to all of them. – Reed Merrill Mar 15 '21 at 18:58

2 Answers2

6

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.

terdon
  • 100,812
  • Thanks for pointing out that the cd isn't necessary. I tried using xdg-open with the pdf file and it didn't work, though it opens in evince from the file manager, so it seems like evince is the default viewer. – Reed Merrill Mar 15 '21 at 19:00
  • 1
    @ReedMerrill that's odd. It should work. You can use xdg-mime query default application/pdf to see what the default tool is and xdg-mime default org.gnome.Evince.desktop application/pdf should set it to evince if it isn't set already. – terdon Mar 16 '21 at 09:22
1

After a bit more tinkering, I found my own answer. Reading and implementing multiple peices of advice from this question got me what I was looking for and I thought I would combine all that into this question to help future noobs looking to do the same thing I was.

  1. If I used cd ../ bash tries to open the files from the same directory the script is in. Entering the path to the file directly worked.

  2. Placing & after each line that opens a file causes the next lines to be executed. My actual script opens a few more files, but this example seems sufficient.

My working script:

#! /bin/bash
cd '/home/me/Dropbox/00-School/03-TA POLI 397/Assignment'
libreoffice grading-instructions.odt &
cd '/home/me/Dropbox/00-School/03-TA POLI 397'
evince 'POLI 397 Assignment.pdf'
  • 1
    Eh I'm a little skeptical of some of this. In particular, it's not true in general that "absolute paths is the only thing that works". (In fact your posted code sample uses relative paths, not absolute paths.) – David Z Mar 16 '21 at 01:41
  • @DavidZ Thanks, I ammended the answer. – Reed Merrill Mar 16 '21 at 02:10