0

I need to find whether the file is compressed or not in script.If it is compressed I need to uncompress and send as attachement. My find command results in two files sum12.pdf.Z and sum23.pdf.Z My script is

dir=/home/as1234/bills
cd $dir
for file in `find . -ctime -1 -type f -name "Sum*pdf*"`
do
if [ ${file: -1} == "Z" ]; then
echo "$file is Zipped"
uncompress $file
uuencode $file
fi
done
uuencode $file $file | mailx -s "subject" abc@gmail.com

when I ran this script I got error like

 ${file: -1}: 0403-011 The specified substitution is not valid for this command.

I am using ksh.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Aravind
  • 1,599
  • 8
    Why don't you use the file command to check if it is compressed instead? For example file -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 for compress maybe? File extensions aren't used in the Unix/Linux world as they are in Windows. – garethTheRed Jun 25 '14 at 08:34
  • 1
    The ${file:offset} syntax was introduced in ksh93, you probably have ksh88. Use case $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:37
  • @garethTheRed - in my machine case is not installed. Is there any way to find .Z file? – Aravind Jun 25 '14 at 15:51
  • From the man page for file: 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

1 Answers1

1

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 {} +
Kusalananda
  • 333,661