As filenames can have both spaces and new-lines a probable approach would be:
printf "%s\n" "$ts" > "$logclean_file"
# Set IFS blank
# -r Backslash does not act as an escape character. The backslash
# is considered to be part of the line. In particular, a
# back-slash-newline pair may not be used as a line continuation.
# -d delim
# The first character of delim is used to terminate the input
# line, rather than newline.
# Here setting -d to nul or 0x00. This enables us to capture any file-
# names with the print0 from find.
# fn The variable to read into.
while IFS= read -r -d $'\x00' fn; do
printf "Will perhaps be compressing file %s\n" "$fn" >> "$logclean_file"
# If file + gz does not exist
if [[ ! -e "$fn.gz" ]]; then
if gzip command "$fn"; then
echo "Horray! success!" >> "$logclean_file"
echo "Harf! Gzip failed." >> "$logclean_file"
echo "Nah. Already exists." >> "$logclean_file"
done < <(find . -type f \( ! -name '*.gz' \) -a \( ! -name '*.Z' \) -print0)
# Notice -print0 at end which means find will print filenames, - separating
# them with 0x00 instead of new-line
print are really portable. While
print is unique to ksh93,
echo only got standardized very late in the POSIX process, and older versions cannot be relied on to produce predictable results. See echo vs print and Why is printf better ....
printf "Some %s\n" "$var" >> "$foo"
echo "Some $var" >> "$foo"
- quote variables.
- Consider using lowercase of user variables (your own variables).
For the TLDP guides, and if you are using stylish, I'd recommend one of these for screen-reading.