ns-tools

Namespace utilities to reuse Open Source packaging efforts.
git clone git://r-36.net/ns-tools
Log | Files | Refs | README | LICENSE

ns-path (1244B)


      1 #!/bin/sh
      2 
      3 usage() {
      4 	printf "usage: %s [-rn] ns cmd\n" "$(basename $1)" >&2
      5 	exit 1
      6 }
      7 
      8 dorecursive=0
      9 stripns=0
     10 while getopts "rn" opt;
     11 do
     12 	case $opt in
     13 	r)
     14 		dorecursive=1
     15 		;;
     16 	n)
     17 		stripns=1
     18 		;;
     19 	*)
     20 		usage $0
     21 		;;
     22 	esac
     23 done
     24 shift $(($OPTIND - 1))
     25 
     26 [ $# -lt 2 ] && usage $0
     27 
     28 findpath() {
     29 	errorn=1
     30 	justone=0
     31 	[ "$1" = "-1" ] && justone=1 && shift 1
     32 	
     33 	fnsroot="$1"
     34 	fcmd="$2"
     35 	NSPATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
     36 	[ -e "$fnsroot/.ns/rc.conf" ] && . "$fnsroot/.ns/rc.conf"
     37 
     38 	for p in $(echo $NSPATH | tr ':' ' ');
     39 	do
     40 		if [ -x "$fnsroot${p}/$fcmd" -o -L "$fnsroot${p}/$fcmd" ];
     41 		then
     42 			errorn=0
     43 			if [ $stripns -eq 1 ];
     44 			then
     45 				printf "%s/%s\n" "$p" "$fcmd"
     46 			else
     47 				printf "%s%s/%s\n" "$fnsroot" "$p" "$fcmd"
     48 			fi
     49 			[ $justone -eq 1 ] && return 0
     50 		fi	
     51 	done
     52 	
     53 	return $errorn
     54 }	
     55 
     56 nsroot="$(ns-root "$1")"
     57 [ $? -gt 0 ] && exit $?
     58 
     59 cmd="$2"
     60 rerrno=1
     61 if [ $dorecursive -eq 1 ];
     62 then
     63 	cd "$nsroot";
     64 	for ns in $(ls -1d */);
     65 	do
     66 		findpath "$nsroot/$(printf "%s\n" "$ns" | tr -d "/")" "$cmd"
     67 		[ $? -eq 0 ] && rerrno=0
     68 	done
     69 else
     70 	findpath -1 "$nsroot" "$cmd"
     71 	rerrno=$?
     72 fi
     73 
     74 if [ $rerrno -gt 0 ];
     75 then
     76 	printf "Could not find %s in namespace %s.\n" "$cmd" "$nsroot" >&2
     77 	exit 1
     78 else
     79 	exit 0
     80 fi
     81