Ash(bash) how to get result from a command to a variable

i am sending email and after the mail sended there is the result in console
like Mail sent sucssefully or some errors
I need to get this result to a variable

This is the command i tried

result=$(mailsend -f "alarr@ram" -t "bu@hu.du" -starttls -port "25" -auth -user "alarr" -pass "1Theant_" -smtp "smtp.hu.du" -sub "FROM router!" -M " Testing sdhit" -attach "/tmp/check_192.168.78.78")
echo "result:$result"


the mail sent but result is empty!!

Maybe

result=$(mailsend blah blah blah 2>&1)
echo "result:$result"
1 Like

still emty result maybe some other numbers to try ? but i dont really rememeber those outputs numbers
mailsend puts output in console

test this:

file1=$(mktemp)
file2=$(mktemp)

mailsend -f "alarr@ram" -t "bu@hu.du" -starttls -port "25" -auth -user "alarr" -pass "1Theant_" -smtp "smtp.hu.du" -sub "FROM router!" -M " Testing sdhit" -attach "/tmp/check_192.168.78.78" > $file1 2> $file2

cat $file1
cat $file2

after that you can proceed to extract the information you want from the files ...

i did like this

file1=$(mktemp)
file2=$(mktemp)

mailsend -f "" -t "" -starttls -port "25" -auth -user "ar" -pass "1T_" -smtp "sm" -sub "FRO!" -M " Testing sdhit" -attach "/tmp/check_192.168.78.78" > $file1 2> $file2

echo  "file1: $file1 "
echo  "file2: $file2 "

and result is


root@BlackAdmins:~# ./mailsendar.sh
file1: /tmp/tmp.HcEINf
file2: /tmp/tmp.efGBie

and those temp files emty

and this ...

file1=$(mktemp)

mailsend -f "alarr@ram" -t "bu@hu.du" -starttls -port "25" -auth -user "alarr" -pass "1Theant_" -smtp "smtp.hu.du" -sub "FROM router!" -M " Testing sdhit" -attach "/tmp/check_192.168.78.78" -log $file1

cat $file1
1 Like

now it gets records from log and and puts it in file

26-Nov-2024 01:34:16.000: mailsend v@(#) mailsend v1.19
26-Nov-2024 01:34:16.000: Mail sent successfully

all i wanted is get result to a varible and them analise the variable

like check if the mail sent sucssefully i guess i f i wont find other way i have to use this method

do a grep on the created file and search for the string of interest and export the variable ...

file1=$(mktemp)

mailsend -f "alarr@ram" -t "bu@hu.du" -starttls -port "25" -auth -user "alarr" -pass "1Theant_" -smtp "smtp.hu.du" -sub "FROM router!" -M " Testing sdhit" -attach "/tmp/check_192.168.78.78" -log $file1

# result=1 send mail
# result=0 no send mail
result='cat $file1 | grep "Mail sent successfully" | wc -l'
echo $result

rm $file1
1 Like
result=$(mailsend blah blah blah 2>&1)
echo "result:$result"

this one actually not empty if some error accured amybe i will use this one instead

2 Likes
mailsend
echo $?
mailsend || yes oops
mailsend && yes cool
1 Like

just cheked it if a message mail is sent then it returns 0
if not 1
well thanks you too this is also the way

Thats like the original way.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.