Rather than reading the various strings interactively, just have the user pass them on the script's command line.
The following script would be called like
./script -d dirname -t templatefile -- string1 string2 string3 string4
... and it would create dirname
if it doesn't already exist, and then copy templatefile
into that directory with the root names given by the strings. If the template file has a filename suffix, this would be added to the end of each string to create the new filenames (after removing the suffix from the string as to not duplicate an already existing suffix).
You may bypass the step in the script that creates the directory by using the -n
option, which makes the script assume that the directory specified by -d
already exists.
#!/bin/sh
# Unset strings set via options.
unset -v dirpath do_mkdir templatepath
# Do command line parsing for -d and -t options.
while getopts d:nt: opt; do
case $opt in
d)
dirpath=$OPTARG
;;
n)
do_mkdir=false
;;
t)
templatepath=$OPTARG
;;
*)
echo 'Error in command line parsing' >&2
exit 1
esac
done
shift "$((OPTIND - 1))"
# Sanity checks.
: "${dirpath:?Missing directory path (-d)}"
: "${templatepath:?Missing template file path (-t)}"
if [ ! -f "$templatepath" ]; then
printf 'Can not find template file "%s"\n' "$templatepath" >&2
exit 1
fi
if "${do_mkdir-true}"; then
# Create destination directory.
mkdir -p -- "$dirpath" || exit
fi
if [ ! -d "$dirpath" ]; then
printf 'Directory "%s" does not exist\n' "$dirpath" >&2
exit 1
fi
# Check to see whether the template file has a filename suffix.
# If so, save the suffix in $suffix.
suffix=${templatepath##*.}
if [ "$suffix" = "$templatepath" ]; then
# No filename suffix.
suffix=''
else
# Has filename suffix.
suffix=.$suffix
fi
# Do copying.
for string do
# Remove the suffix from the string,
# if the string ends with the suffix.
string=${string%$suffix}
cp -- "$templatepath" "$dirpath/$string$suffix"
done
To recreate your example at the end of your question, you would use this script like so:
./script -t ~/Documents/Library/Template.py -d "$round" -- A B C D1 D2