Get the Directory Path of a Bash Script
So you have a bash script and you want to run it from crontab or some other context where the current working directory isn’t known. Your script wants to use relative path information (from the script itself), perhaps to create a directory structure in the script’s base directory.
The question is: How do you get the directory path of the script? dirname? Not reliably.
#!/bin/bash ABSPATH="$(cd "${0%/*}" 2>/dev/null; echo "$PWD"/"${0##*/}")" CWD=`dirname "$ABSPATH"`
Nice!
Less-Common Tools for Linux
As found here: 10 Tools to Add Some Spice to Your UNIX Shell Scripts. This is no substitute for the great write-up and screenshots that the author put together. It’s a summary for myself.
The highlights (and how I really feel about them):
- notify-send (I never knew — better than tweat deck)
- tput (eh, maybe)
- setleds (i don’t have lock-key LEDs on my keyboard)
- zenity (has potential)
- kdialog (better than zenity? looks like VB functionality — good?)
- dialog (like kdialog, but uses (n)curses, not kde)
- logger (good to know!)
- setterm (practical jokes… and maybe useful for modal/portal stuff)
- smbclient (does that still work? Couldn’t get past authentication prompt)
- bash sockets (interesting… ICP? wasteful)
STDERR redirect into STDOUT redirect into file
When you want to redirect stderr to a file, you have choices. Either redirect only stderr to a file, or redirect both stderr and stdout to the same file.
The right way:
Redirect stderr and stdout to [[file]]:
[[command]] > [[file]] 2>&1
Redirect stderr to [[efile]] and stdout to [[file]]:
[[command]] 2> [[efile]] > [[file]]
The wrong way:
Doesn’t do anything useful:
[[command]] 2>&1 > [[file]]