Tuesday, March 15, 2016

Bash Script to get Path to Inself

I use bash enough and fumble my way through it that I have little nuggets like this but I never retain them in my brain. So, in contributing to the global bash community and my external brain storage, here is how to get the path that your currently running script lives in.

PATH=$(cd $(dirname $0) ; pwd -P)
And since people typically post scripts like this, even one liners, and don't explain them, I'll explain each piece. 1. The first argument is the full file name to the script: $0 2. Get the directory of the script. You'd think we'd stop there, but we keep going: $(dirname $0) 3. Change directories to the directory containing the script: cd $(dirname $0) 4. Run another bash command: ; 5. Get the current working directory, and -P resolves symlinks: pwd -P 6. Assign the directory containing the script and that is not a symlink so the real path to the PATH variable: PATH=$(cd $(dirname $0) ; pwd -P) There you have it!

Tuesday, March 1, 2016

External Hard Disk Doesn't Eject On El Capitan


Ever since upgrading to El Capitan all external hard disks won't eject because Spotlight is always running. I have to stop Spotlight with this command before ejecting or risk corrupting the hard disk:


sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.metadata.mds.plist

And then restart Spotlight:

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.metadata.mds.plist