-4

Good morning to everybody!

I have created a super-simple script that delete several files in the folder:

#/bin/bash
rm *.ext1 *.ext2 file1.txt file2.txt

When I execute the script using the shell all works fine. But I'd like to be able to double-click on the script to execute it...and this actually works but it is like the script is executed in another folder and then the answer I get is a list of "rm -filename- : No such file or directory".

How can I modify my script in order to tell him to be executed in the folder in which it is located? NB: I don't know a priori the folder path, then I cannot specify the full path!

Thanks!!

philus
  • 51
  • 1
  • 3
  • See [this popular question](https://stackoverflow.com/q/59895/253056). Get the parent directory of your script and then use this in the paths you pass to `rm`. – Paul R Mar 21 '18 at 07:50

1 Answers1

1

You could ask the Finder (on Mac) to tell you which folder it is currently displaying:

#!/bin/bash

# Ask Finder which folder it is currently displaying
dir=$( /usr/bin/osascript <<EOF
    tell application "Finder"
    try
            set currFolder to POSIX path of (folder of the front window as alias)
    on error
            set currFolder to "ERROR"
    end try
    currFolder
    end tell
EOF
)
echo "Finder is currently in: $dir"
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432