Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
6
rated 0 times [  6] [ 0]  / answers: 1 / hits: 4663  / 3 Years ago, wed, july 7, 2021, 8:25:33

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?


More From » bash

 Answers
4

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.


[#1812] Wednesday, July 7, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
scrubuz

Total Points: 194
Total Questions: 96
Total Answers: 127

Location: Monaco
Member since Fri, Sep 24, 2021
3 Years ago
;