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
  • 104,119

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
  • 104,119
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'