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

}

1 Answer 1

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.

4
  • 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 Commented Dec 10, 2014 at 5: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. Commented Dec 10, 2014 at 5:51
  • I changed it to what you said and i got 3 more errors -f2: command not found, unary operator expected, unary operator expected, Commented Dec 10, 2014 at 6:01
  • Thanks sputnick! the text/plain instead of ASCII did it for me! smooth sailing :D Commented Dec 10, 2014 at 6:21

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.