#!/bin/csh -f # Weird bug. When rename is called directly as either of: # % rename a "" file1 file2 # % rename a '' file1 file2 # the incoming params are correctly: # $1: "a" # $2: "" # $3: "file1" # but when called via ren as either of: # % ren a "" file1 file2 # % ren a '' file1 file2 # the incoming params are erroneously shifted left past the empty string: # $1: "a" # $2: "file1" # $3: "file2" # # How to make it handle the empty string properly? # # To see the problem, uncomment these lines: # echo "In ren" # echo "1: $1:q" # echo "2: $2:q" # echo "3: $3:q" # (set echo; rename $*:q) # exit # # And add these lines to rename: # echo "In rename" # echo "1: $1:q" # echo "2: $2:q" # echo "3: $3:q" # echo "4: $4:q" # echo "5: $5:q" # echo "6: $6:q" # exit # Kludge to fix the problem: Detect empty string and pass it explicitly. set options = "" while ($#argv > 0) if ("-" == "`echo $1:q | cut -c 1`") then # Starts with a hyphen. Assume it's a valid option. # Collect it and remove it from argv set options = "$options $1:q" shift else # Not an option. Must be the first arg. break endif end # At least 2 arguments required if ($#argv < 2) then echo "Error: At least 2 arguments required." rename -h exit 1 endif # Explicitly grab the 1st 2 args set p1 = $1:q set p2 = $2:q shift shift # Explicitly pass the 1st 2 args, even if empty string. Let rename deal # with them. Just make sure they get passed, so the other args don't shift # left. if ("$p1" == "") then if ("$p2" == "") then (set echo; rename $options "" "" $*:q) else (set echo; rename $options "" "$p2" $*:q) endif else if ("$p2" == "") then (set echo; rename $options "$p1" "" $*:q) else (set echo; rename $options "$p1" "$p2" $*:q) endif endif