As filenames can have both spaces and new-lines a probable approach would be:
#!/bin/bash
logclean_file="logclean_file.txt"
ts="$(date)"
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"
else
echo "Harf! Gzip failed." >> "$logclean_file"
fi
else
echo "Nah. Already exists." >> "$logclean_file"
fi
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
About portability:
Neither echo nor 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"
# Or
echo "Some $var" >> "$foo"
About Style:
- quote variables.
- Consider using lowercase of user variables (your own variables).
Some ref:
For the TLDP guides, and if you are using stylish, I'd recommend one of these for screen-reading.
echo, notprint. – vonbrand Mar 15 at 19:05printin the shell that morgon specified (ksh). – Stephane Chazelas Mar 15 at 20:44$LOGCLEAN_FILE? If it's a relative path, have you changed directories? – Gilles Mar 15 at 22:13