How do I pass a string with spaces to an execute command in Java to execute a bash script?
I'm trying to use a script to generate and send a email using the unix mail
command but it ignores the quotes surrounding the message string:
#!/bin/bash
######################################################
#
# Params:
# 1) Email recipient
# 2) Subject line (in quotes)
# 3) Message (in quotes)
#
# Returns:
# 0 if success, else non-zero
#####################################################
MAIL_TO=$1
SUBJECT_LINE=$2
MESSAGE=$3
echo "Mail To= ${MAIL_TO}" >> /logs/terminalLog.txt
echo "Subject= ${SUBJECT_LINE}" >> /logs/terminalLog.txt
echo "Message= ${MESSAGE}" >> /logs/terminalLog.txt
echo "" >> /logs/terminalLog.txt
echo "$MESSAGE" | mail -s "$SUBJECT_LINE" $MAIL_TO >> /logs/terminalLog.txt
and this is how I'm calling it in java :
Process proc = Runtime.getRuntime().exec(scriptName+" me@someplace.com \"My Test Subject Line\" \"This is the test message!!\"");
The problem is it takes "My
as the subject argument and Test
as the message argument and ignores the rest.
I have tried using single quotes, exec(command, args)
where
args = {"me@someplace.com","My Test Subject Line","This is the test message!!" }
but still has the same result.
I have searched here and online but most people seem to suggests what I have tried and while it worked for them, it did not for me.
Update
On the Advice of a number of comments/Answers I have changed from using the exec
to
ProcessBuilder pb = new ProcessBuilder(scriptName, "me@somePlace.com","My Test Subject Line", "This is the test message!!");
pb.start();
I now successfull get the correct Arguments as shown by the output to log BUT I never recieve the email. If I call the script via the terminal manually I do recieve the email