Hamilton LaboratoriesHamilton C shell 2012User guideSamples

basename.csh

Oregon Coast

basename.csh
Previous | Next

#  Emulate the POSIX basename command to extract just the last component
#  of a pathname, deleting any suffix, if specified.

#  Usage:   basename string [ suffix ]

#  Copyright (c) 1996-2012 by Hamilton Laboratories.  All rights reserved.

proc basename( string, suffix )
   local base, i, j

   if ( $#string == 0 ) then
      echo -2 basename: No file or directory specified.
      @ status = 1
      return
   end

   if ( $#string > 1 ) then
      echo -2 basename: Only one file or directory should be specified.
      @ status = 1
   end

   @ base = $string:t
   if (base == "") @ base = "\"

   if (suffix == "" && suffix != base) return base

   @ i = strlen(base)
   @ j = strlen(suffix)
   return i > j && lower(substr(base, i - j + 1)) == lower(suffix) ? ^
      substr(base, 1, i - j) : base
end

basename $argv

Previous | Next