#!/bin/csh -f # rmlink # ------------------------------------------------------------------------- # C shell script to delete a file only if it's a symlink. # ------------------------------------------------------------------------- # Revision History: # $Log$ # ------------------------------------------------------------------------- # Collect command line options set option_verbose = "false" set rm_options = "" while ($#argv > 0) if ("$1" == "-h" || "$1" == "--help") then echo "Like rm, but delete only symlinks" echo "Usage: $0:t [options] file..." echo "Options:" echo " -h = Show this help text" echo " --help = Show this help text" echo " -f = Passed to rm command" echo " -i = Passed to rm command" echo " -v = Verbose about things like whether or" echo " not the symlink refers to a file that" echo " exists." echo " Also passed to rm command." exit 1 else if ("$1" == "-f") then set rm_options = "$rm_options $1" shift else if ("$1" == "-i") then set rm_options = "$rm_options $1" shift else if ("$1" == "-v") then set option_verbose = "true" set rm_options = "$rm_options $1" shift else if ("-" == "`echo $1:q | cut -c 1`") then beep "Error: Invalid option: $1:q" $0 --help exit 2 else # Not an option. Assume it's the first argument break endif end if ($#argv < 1) then beep "Error: Must specify at least one file to delete." $0 -h exit 3 endif # Check all files before trying to delete any foreach file ($*:q) if (! -l $file:q) then if (-e $file:q) then beep "Error: $file is not a symlink." exit 5 else beep "Error: $file does not exist." exit 4 endif endif end foreach file ($*:q) if ("$option_verbose" == "true") then # Note: -e tests for existence of target, not just of symlink, # following a series of links to the ultimate target file. if (-e $file:q) then echo "Deleting symlink $file to an existing file." else echo "Deleting symlink $file to a non-existent file." endif (set echo; rm $rm_options $file:q) else rm $rm_options $file:q endif end