#!/bin/csh -f # rmempty # ------------------------------------------------------------------------- # C shell script to delete a file only if it's empty. # ------------------------------------------------------------------------- # 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 empty files" 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 file is empty." 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 (! -e $file:q) then beep "Error: $file does not exist." exit 4 endif if (! -z $file:q) then beep "Error: $file is not empty." exit 5 endif end foreach file ($*:q) if ("$option_verbose" == "true") then echo "Deleting empty file $file." (set echo; rm -v $rm_options $file:q) else rm -v $rm_options $file:q endif end