0

I have a file in the path mentioned below that is above 10M size. I need to write a shell script to send an email as an attachment only if the file size is less than 5M. If the file size is less than 10M size, I need to highlight only the path of the file in the email. Can anyone guide how to achieve this.

I tried the below script but it is giving - Message file too long warning.

#!/bin/bash
cd /opt/alb_test/alb/albt1/Source/alb/al/conversion/scripts
#Sending Email

ls *.xlsx -1 > test.txt
while read line
do
mailx -s "Test Email" -a ${line} -s "Attaching files less than 5 MB" abc@testmail.com,def@gmail.com << EOM

Hi, Sending the Files
EOM
done<test.txt
Jenifer
  • 387
  • 2
  • 11
  • 1
    You did not put any file size check. Moreover, you say 10M (bytes I assume) in your question, but it is written 5MB in your code. – Itération 122442 Dec 15 '22 at 09:29
  • 1
    Does this answer your question? [How do I send a file as an email attachment using Linux command line?](https://stackoverflow.com/questions/17359/how-do-i-send-a-file-as-an-email-attachment-using-linux-command-line) – Bodo Dec 15 '22 at 17:58

1 Answers1

0

You need to clearly break down the scenarios that you need to handle, then code for those. You need to distinguish between 4 file states.

It helps for maintainability when you clearly define parameters for each one of those states, separately.

The placement of the "-1" in your ls statement is an error. Also, to force one per line, there is no need to specify that, which is the default when directed to a file. "-1" is only needed for output to the terminal.

So here is the proposed solution to your problem:

#!/bin/bash

filepath="/opt/alb_test/alb/albt1/Source/alb/al/conversion/scripts"

distro_list='abc@testmail.com,def@gmail.com'

THRESH_file=5000000
THRESH_path=10000000

SIGNATURE="
Me
MyPosition
MyEmail"

cd ${filepath}

#Sending Email

ls *.xlsx > test.txt

while read line
do
    if [ -s "${line}" ]
    then
        SIZE=$(stat --printf="%s" "${line}" )

        if [ ${SIZE} -lt ${THRESH_file} ]
        then
            SUBJECT="-s 'Attaching files under 5 MB'"
            MSG="Hi, Sending the undersized file."
            ATTACH="-a '${filepath}/${line}'"
        else
            if [ ${SIZE} -lt ${THRESH_path} ]
            then
                SUBJECT="-s 'File size at least 5 MB but less than 10 MB'"
                MSG="Hi, Sending the midsized file path:  '${line}' [${filepath}] "
                ATTACH=""
            else
                SUBJECT="-s 'File size at least 10 MB'"
                MSG="Hi, Sending the normal sized file."
                ATTACH="-a '${filepath}/${line}'"
            fi
        fi
    else
        SUBJECT="-s 'File is empty!'"
        MSG="File '${line}' is empty [${filepath}] ..."
        ATTACH=""
    fi

    echo -e "${MSG}\n${SIGNATURE}" |
        mailx ${SUBJECT} ${ATTACH} ${ditro_list}
done <test.txt
Eric Marceau
  • 1,601
  • 1
  • 8
  • 11
  • [Don't use `ls` in scripts](https://mywiki.wooledge.org/ParsingLs) and of course the temporary file is completely useless; simply `for line in *.xlsx` instead (though perhaps rename the variable to `file`) ... and [don't use upper case for your private variables.](https://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization) – tripleee Jan 05 '23 at 06:35
  • I used caps for variables here as a means to visibly highlight the repeated elements for each scenario. I acknowledge the wisdom published in the link you provided. – Eric Marceau Jan 05 '23 at 19:51
  • @triplee, did you delete my earlier comment about using ls ? – Eric Marceau Jan 05 '23 at 19:52
  • Huh? Me? No. But if you left that comment after mine and somebody flagged it as redundant, a mod might have removed it. If a comment gets flagged as superfluous by multiple users it will be deleted automatically. But I can only speculate; I don't recall seeing a comment about that by you. – tripleee Jan 05 '23 at 19:57
  • The use of string variables for options is also a problem for robustness; see also https://mywiki.wooledge.org/BashFAQ/050 – tripleee Jan 05 '23 at 20:00