Listing 1: The mvdir script

  1	# mvdir
  2	AWKTYPE=/usr/bin/awk;    export AWKTYPE
  3	CUT=/usr/bin/cut;        export CUT
  4	MV=/bin/mv;              export MV
  5	CPIO=/bin/cpio;          export CPIO
  6	MKDIR=/bin/mkdir;        export MKDIR
  7	FIND=/bin/find;          export FIND
  8	BASENAME=/bin/basename;  export BASENAME
  9     DF=/bin/df;              export DF
 10	
 11	if [ $# != 2 ]
 12	then
 13	   echo "Usage: mvdir srcdir destdir"
 14	   exit 1
 15	fi
 16	srcdir=$1        # source directory to be moved
 17	destdir=$2       # destination directory
 18	
 19	if [ $srcdir = . ]
 20	then # may not move pwd
 21	   echo "mvdir: cannot move '.'"
 22	   exit 1
 23	fi
 24	
 25	if [  ! -d $srcdir ]
 26	then  # may only move a directory
 27        echo "mvdir: $srcdir must be a directory"
 28	   exit 1
 29	fi
 30
 31	basedir=`$BASENAME $srcdir`
 32	if [ -d $destdir ]
 33	then
 34	   destdir=$destdir/$basedir
 35	else # check for a legal path minus the basename
 36	   retdir=`echo $destdir|$AWKTYPE '
 37	         {
 38	         pos=index($0,"/")  # Where is the first slash?
 39	         if (pos)
 40	            {
 41	            nofields=split($0, s, "/")
 42	            stp = nofields - 1
 43	            for (i=1; i<=stp; i++)
 44	               {
 45	               if (s[i] == "" && pos == 1 && i == 1)
 46	                  { # the first position is a slash
 47	                  retstr="/"
 48	                  continue
 49	                  }
 50	               if (i == stp) # no trailing slash on last field
 51	                  retstr=retstr s[i]
 52	               else
 53	                  retstr=retstr s[i]"/"
 54	               }
 55	             print retstr
 56	            }
 57	         else  # destination is not a directory
 58	            print "NO-PATH"
 59	         }
 60	      '` # end awk script
 61	   if [ ! -d $retdir -a $retdir != "NO-PATH" ]
 62	   then  # no illegal directory names
 63	      echo "mvdir: $retdir directory does not exist  "
 64	      exit 1
 65	   fi
 66	fi
 67	
 68	if [ -f $destdir  -o -d $destdir ]
 69	then # destination directory may not be a file or exist
 70	   echo "mvdir: $destdir exists"
 71	   exit 1
 72	fi
 73	
 74	count=`expr $destdir : $srcdir/`
 75	if [ $count != 0 ]
 76	then
 77	   echo "mvdir: directories have common path"
 78	   exit 1
 79	fi
 80	
 81	firstsrc=`echo $srcdir|$CUT -c1`
 82	firstdest=`echo $destdir|$CUT -c1`
 83	if [ $firstdest != / -a $firstsrc != / ]
 84	then
 85	   $MV $srcdir $destdir
 86	else # possible cross file system move
 87	   if [ $firstsrc != / ]
 88	   then
 89	      pwdsrc=`pwd`
 90	   else
 91	      pwdsrc=$srcdir
 92	   fi
 93	   if [ $firstdest != / ]
 94	   then
 95	      pwdsrc=`pwd`
 96	   else
 97	      pwddest=$destdir
 98	   fi
 99	
100	   srcfilesys=`echo $pwdsrc|$CUT -d"/" -f1,2`
101	   destfilesys=`echo $pwddest|$CUT -d"/" -f1,2`
102	   retval=`$DF|$AWKTYPE '
103	      BEGIN {tripflag = 0}
104	      {
105	      if (dsys == $1 && dsys != ssys)
106	         {
107	         tripflag = 1
108	         exit 1
109	         }
110	      }
111	      END {print tripflag}
112	   ' dsys=$destfilesys ssys=$srcfilesys`
113	
114	   if [ $retval -eq 1 ]
115	   then
116	      echo "Copy files across file system!"
117	      cd $srcdir
118	      if [ $? = 0 ]
119	      then
120	         $MKDIR $destdir
121	         if [ $? = 0 ]
122	         then
123	            $FIND . -depth -print|$CPIO -pd $destdir
124	         fi
125	      fi
126	   else
127	      $MV $srcdir $destdir
128	   fi
129	fi

