The filename suffix after the final dot may be had with ${file##*.}
.
In this case though, I would consider doing the uncompressing and the uuencoding with find -exec
directly like this:
#!/bin/sh
dir=/home/as1234/bills
find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
for pathname do
filename=$( basename "${pathname%.pdf*}.pdf" )
if [ "${pathname##*.}" = "Z" ]; then
uncompress -c "$pathname"
elif [ "${pathname##*.}" = "gz" ]; then
gzip -cd "$pathname"
else
cat "$pathname"
fi |
uuencode "$filename" |
mailx -s "subject ($filename)" abc@gmail.com
done' sh {} +
This way, you would support pathnames with spaces and other troublesome characters. The sh -c
script also does not store the uncompressed files but uncompresses them, uuencodes them and sends them in one go.
I also added handling of gzip
-compressed files.
Related:
Alternative implementation of the sh -c
script using case ... esac
instead of multiple if
and elif
statements.
find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
for pathname do
filename=$( basename "${pathname%.pdf*}.pdf" )
case $pathname in
*.Z) uncompress -c "$pathname" ;;
*.gz) gzip -cd "$pathname" ;;
*) cat "$pathname"
esac |
uuencode "$filename" |
mailx -s "subject ($filename)" abc@gmail.com
done' sh {} +
file
command to check if it is compressed instead? For examplefile -ib <filename.Z>
will give you the MIME type of the file which you can then use in your tests -application\x-compress; charset=binary
. You could then check for that exact string or grep forcompress
maybe? File extensions aren't used in the Unix/Linux world as they are in Windows. – garethTheRed Jun 25 '14 at 08:34${file:offset}
syntax was introduced in ksh93, you probably have ksh88. Usecase $file in *.Z) ...
(and don't forget to quote your variables!) (and don't use command substitution on the output of find, use-exec
instead) – Stéphane Chazelas Jun 25 '14 at 08:37man
page forfile
: There has been a file command in every UNIX since at least Research Version 4 (man page dated November, 1973). What is your machine? – garethTheRed Jun 25 '14 at 16:52