#!/usr/bin/nawk -f # epath - edit $path or $PATH variables sanely # # This software is Public Domain. # Report all bugs to Greg Ercolano (erco@3dsite.com). # # setenv PATH `echo $PATH | epath ins ~/bin` # setenv PATH `echo $PATH | epath rm /usr/old/bin` # set path = (`echo $path | epath ins /usr/local/prman/bin`) # # These aliases show how to make epath easy to use: # # alias addpath 'setenv PATH `echo $PATH | epath add \!*`' # alias inspath 'setenv PATH `echo $PATH | epath ins \!*`' # alias delpath 'setenv PATH `echo $PATH | epath rm \!*`' # # Duplicate paths are removed for optimization. # # rm - removes all occurances of path # add - removes all prev occurances, append to end of PATH # ins - removes all prev occurances, insert at head of PATH # opt - just optimize (remove duplicate occurances) # BEGIN { addpath = ""; inspath = ""; if (ARGV[1] == "add") { ARGV[1] = "-"; addpath = ARGV[2]; delete ARGV[2]; } if (ARGV[1] == "ins") { ARGV[1] = "-"; inspath = ARGV[2]; delete ARGV[2]; } if (ARGV[1] == "rm") { ARGV[1] = "-"; rmpath = ARGV[2]; delete ARGV[2]; } if (ARGV[1] == "opt") { ARGV[1] = "-"; } } { total = split($0, path, ":"); } END { # REMOVE (TRAILING) DUPLICATE PATHS # Shortens paths from growing ridiculously for (t=1; t<=total; t++) for (r=t+1; r<=total; r++) if (path[t] == path[r]) path[r] = ""; # REMOVE THINGS WE'RE MODIFYING # Shortens paths from growing ridiculously for (t=1; t<=total; t++) for (r=t+1; r<=total; r++) if (path[t] == path[r]) path[r] = ""; # REMOVE THINGS WE'RE MODIFYING for (t=1; t<=total; t++) if (path[t] == rmpath || path[t] == addpath || path[t] == inspath ) path[t] = ""; if (inspath != "" ) printf("%s:", inspath); r=0; for (t=1; t<=total; t++) if (path[t] != "" ) printf("%s%s", (r++)?":":"",path[t]); if (addpath != "" ) printf("%s%s", (r++)?":":"", addpath); printf("\n"); }