#!/bin/sh -- # A comment mentioning perl, to prevent perl from looping. Indented to work with bash. eval 'exec perl -S $0 ${1+"$@"}' if 0; #!/usr/bin/perl chop($Program = `basename $0`); $Usage = <<"END"; $Program: Split an environment variable or string. Usage: $Program [-s|-c splitchar] [] Options: -s Treat last arg as string to split (otherwise use environment variable of that name, default is PATH). -c Use "c" as the splitchar. (can be any string) -r Use "r" as the regex to split upon. Notes: END $String = ''; $Char = ':'; $Regex = ''; LOOP: while (@ARGV) { $_ = shift; CASE: { /^-c/ && ($Char = shift, last CASE); /^-r/ && ($Regex = shift, last CASE); /^-s/ && ($String = 1, last CASE); /^--$/ && (last LOOP); # -- means end of switches /^-(-.*)$/ && (unshift(@ARGV, $1), last CASE); /^(-h|-help)$/ && ((print $Usage), exit 0, last CASE); if ( /^-(..+)$/ ) { # split bundled switches: local($y, $x) = ($1, ''); foreach $x (reverse(split(//, $y))) { unshift(@ARGV,"-$x") }; last CASE; } /^-/ && ((print "Invalid arg: $_\n$Usage"), exit 1, last CASE); unshift(@ARGV,$_); last LOOP; } } if (! @ARGV) { push(@ARGV, 'PATH'); } if ( $Regex ne '' ) { $match = $Regex; } else { $match = $Char; $match =~ s/(\W)/\\$1/g; } foreach $arg (@ARGV) { if ( $String ) { $string = $arg; } else { $string = $ENV{$arg}; } foreach $dir (split(/$match/,$string)) { print "$dir\n"; } }