#!/bin/csh -f # mdcp # ------------------------------------------------------------------------------ # Shell script to copy a file, creating nested target directories as needed. # ------------------------------------------------------------------------------ # Usage: See Usage section below or run with no arguments to see usage. # Assumptions: # Effects: # - Copies a file and creates containing target dirs. # Notes: # Implementation Notes: # Portability Issues: # Revision History: # $Log$ # ------------------------------------------------------------------------------ set option = if ("$1" == "-i") then set option = "$1" shift endif if ($#argv < 2) then echo "Usage: $0:t [-i] source_filename target_filename_or_existing_dir" exit 1 endif # Create nested target directories as needed. set rc = 0 set target_path = "$2" if (-d "$target_path") then # Target param was an existing path. No need to create the path. # The copy command will copy the file to that path. else # Target param is not an existing path. May be a file or non-existent. # Try to create the parent path, if any, to hold the target file. # Note: :h strips off last path segment, unless there is only one, # in which case it returns that one, NOT the empty string. if ("$2:h" != "$2") then set target_path = "$2:h" mkdir -p -v "$target_path" set rc = $status else # Target param is not an existing path and is a simple name like # foo, not bar/foo. Treat it as a target filename. endif endif if ($rc == 0) then # Use :q instead of "" in case option is empty. Generates nothing instead of # an empty quoted string which would be counted as first arg. cp -iv $option:q "$1" "$2" set rc = $status # Tolerate failures to copy symlinks to writable files or directories # Probably caused by the target file of the symlink not existing. #?? Could go to the trouble of getting the target of the symlink and #?? confirming its non-existence, but is that really necessary? if ($rc != 0 && -l "$1" && -w "${target_path}") then set rc = 0 endif endif exit $rc