Skip to content

Commit

Permalink
Workaround for awk and sed deficits in Solaris
Browse files Browse the repository at this point in the history
  • Loading branch information
agkozak committed Sep 11, 2018
1 parent 9d5a3fe commit ebb29b9
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions z.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
echo "ERROR: z.sh's datafile (${_Z_DATA:-$HOME/.z}) is a directory."
}

_z_pick_awk_flavor() {
if [ -z "$_Z_AWK" ]; then
local awk; for awk in mawk gawk original-awk nawk awk; do
command -v $awk > /dev/null 2>&1 && _Z_AWK=$awk && break
done
fi
}

_z() {

local datafile="${_Z_DATA:-$HOME/.z}"
Expand Down Expand Up @@ -62,7 +70,7 @@ _z() {

# maintain the data file
local tempfile="$datafile.$RANDOM"
_z_dirs | awk -v path="$*" -v now="$(date +%s)" -F"|" '
_z_dirs | $_Z_AWK -v path="$*" -v now="$(date +%s)" -F"|" '
BEGIN {
rank[path] = 1
time[path] = now
Expand Down Expand Up @@ -95,7 +103,7 @@ _z() {

# tab completion
elif [ "$1" = "--complete" -a -s "$datafile" ]; then
_z_dirs | awk -v q="$2" -F"|" '
_z_dirs | $_Z_AWK -v q="$2" -F"|" '
BEGIN {
q = substr(q, 3)
if( q == tolower(q) ) imatch = 1
Expand All @@ -120,7 +128,12 @@ _z() {
l) list=1;;
r) typ="rank";;
t) typ="recent";;
x) sed -i -e "\:^${PWD}|.*:d" "$datafile";;
x)
local tempfile="$datafile.$RANDOM"
sed -e "\:^${PWD}|.*:d" "$datafile" > "$tempfile"
env mv -f "$tempfile" "$datafile" 2> /dev/null \
|| env rm -f "$tempfile"
;;
esac; opt=${opt:1}; done;;
*) fnd="$fnd${fnd:+ }$1";;
esac; last=$1; [ "$#" -gt 0 ] && shift; done
Expand All @@ -136,7 +149,7 @@ _z() {
[ -f "$datafile" ] || return

local cd
cd="$( < <( _z_dirs ) awk -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -F"|" '
cd="$( < <( _z_dirs ) $_Z_AWK -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -F"|" '
function frecent(rank, time) {
# relate frequency and time
dx = t - time
Expand Down Expand Up @@ -212,6 +225,8 @@ _z() {
fi
}

_z_pick_awk_flavor

alias ${_Z_CMD:-z}='_z 2>&1'

[ "$_Z_NO_RESOLVE_SYMLINKS" ] || _Z_RESOLVE_SYMLINKS="-P"
Expand Down

0 comments on commit ebb29b9

Please sign in to comment.