1

I use this script to open Microsoft Office documents in Office Online to convert them to PDF (for my work) and preserve the formatting. Script to view office documents in Microsoft Office Online

The script basically copies the docx file to my ~/Dropbox/Public directory. Then it grabs the public URL and appends it to the Office Online link to view documents in the browser.

I often forget to clear out the ~/Dropbox/Public directory after opening in Office Online. The following command works from terminal to clear out this directory, but I have been unable to incorporate it successfully into my script. (I have tried adding it to the end of it and have tried other ideas.) The command below deletes all files in the ~/Dropbox/Public directory but one. Any help or suggestions are much appreciated.

sleep 60
rm /home/brock/Dropbox/Public/!(My-Document.docx) 

Another possibility (see script for variable reference). This one I assume would delete only the copied file.

sleep 60
rm /home/brock/Dropbox/Public/$Filename
2

Unquoted strings (such as *, ???, a?c, abc) are interpreted by bash as globbing patterns, and they are expanded to the list of filenames matching the globbing pattern in the current working directory;

From this perspective, an unquoted string not containing wildcards (such as abc) is still interpreted as a globbing pattern matching only a filename such as abc;

Basic globbing performs e.g. the expansion of * to any number of any character and the expansion of ? to a single character;

However negating a globbing pattern is part of the extended globbing features of bash;

Thus in order to expand !(<globbing_pattern>), extended globbing must be enabled in the current bash instance;

Extended globbing is enabled by default when bash is called interactively:

user@user-X550CL:~/tmp$ tree -a
.
|-- 1
|-- 2
|-- 3
`-- script.sh

0 directories, 4 files
user@user-X550CL:~/tmp$ shopt extglob
extglob         on
user@user-X550CL:~/tmp$ rm !(script.sh)
user@user-X550CL:~/tmp$ tree -a
.
`-- script.sh

0 directories, 1 file

However extended globbing is not enabled by default when bash is called non-interactively (e.g. when called to run a script):

user@user-X550CL ~/tmp % tree -a
.
|-- 1
|-- 2
|-- 3
`-- script.sh

0 directories, 4 files
user@user-X550CL ~/tmp % cat script.sh
#!/bin/bash
shopt extglob
rm ~/tmp/!(script.sh)
exit 0
user@user-X550CL ~/tmp % bash script.sh
extglob         off
script.sh: line 3: syntax error near unexpected token `('
script.sh: line 3: `rm ~/tmp/!(script.sh)'
user@user-X550CL ~/tmp % tree -a
.
|-- 1
|-- 2
|-- 3
`-- script.sh

0 directories, 4 files

So the solution is simply to enable extended globbing in the script using the shopt builtin: shopt -s extglob;

user@user-X550CL ~/tmp % tree -a
.
|-- 1
|-- 2
|-- 3
`-- script.sh

0 directories, 4 files
user@user-X550CL ~/tmp % cat script.sh
#!/bin/bash
shopt -s extglob
shopt extglob
rm ~/tmp/!(script.sh)
exit 0
user@user-X550CL ~/tmp % bash script.sh
extglob         on
user@user-X550CL ~/tmp % tree -a
.
`-- script.sh

0 directories, 1 file
  • Perfect! I thought it might be something along those lines. This was a very thorough answer. The best answers such as this one not only provide a solution, but provide a teaching guide for the questioner and other users to grow in their knowledge. Thank you! – jbrock Sep 22 '15 at 14:46
  • @jbrock Glad that it helped :) It was a little cryptic though; you got it already however I added some background informations also for future readers, hopefully to make it easier / quicker to be understood – kos Sep 22 '15 at 15:57
  • Very nice. Thanks. With a little CLI / bash knowledge it was easy enough to follow. However, the additional info does help. – jbrock Sep 22 '15 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.