1

I am trying to make a check to see if the file being attached to the email is a text file, and if it is not it returns an error. However during testing, I provide a valid text.txt and it returns the "Invalid Attachment" message.

send_email()                
{
  message=
  address=
  attachment=
  validuser=1
echo "Enter the email address: "
read address
echo ""
getent passwd | grep -q $address
if [ "$?" = "0" ]
  then
    echo -n "Enter the subject of the message: "
    read message
    echo ""

    echo "Enter the file you want to attach: "
    read attachment
    attachmenttype='file $attachment | cut -d\  -f2'
    if [ $attachmenttype = "ASCII" ]
  then 
  mail -s "$message" "$address"<"$attachment"
  press_enter
elif [ $attachmenttype = "cannot" ]
  then 
  mail -s "$message" "$address"<"$attachment"
  press_enter
else
  echo "Invalid attachment"
  press_enter
fi
 else
    echo "Invalid username"
    press_enter
fi

}

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Duncan
  • 33
  • 1
  • 1
  • 4

1 Answers1

3

Instead of

attachmenttype='file $attachment | cut -d\  -f2'

you should write :

attachmenttype=$(file "$attachment" | cut -d' ' -f2)

See http://wiki.bash-hackers.org/syntax/expansion/cmdsubst


or to get mime-type :

$ file -i "$attachmenttype" | cut -d' ' -f2
text/plain;

and decide what you want to do with the file depends of the type.

Gilles Quénot
  • 31,569
  • 7
  • 64
  • 82
  • the part im trying to do it on is actually not for passwd, thats verifying the user. attachmenttype='file $attachment | cut -d\ -f2' if [ $attachmenttype = "ASCII" ] is where i attempt to identify the file – Duncan Dec 10 '14 at 05:48
  • The `cut` command you are using should use the a whitespace as the delimiter, instead of a `\`. That is what @sputnick has pointed out. – Sreeraj Dec 10 '14 at 05:51
  • I changed it to what you said and i got 3 more errors -f2: command not found, unary operator expected, unary operator expected, – Duncan Dec 10 '14 at 06:01
  • Thanks sputnick! the text/plain instead of ASCII did it for me! smooth sailing :D – Duncan Dec 10 '14 at 06:21