Issue
When I tried to pass some program arguments from shell script to java program, the escape characters are not working.
Here is the code and command I tried to use.
Shell Script:
#!/usr/bin/bash
filename="DX.jar"
uid="111,110"
onCallName1="Tom"
onCallName2="Jerry"
onCallName3="Brun"
text="These are people who are on call: @$onCallName1, @$onCallName2 and @$onCallName3. \\n'
Please focus on the cases of online problems. \\n'
Safe cases monitor: https://a.b.com/page/992645614 \\n'
Old cases log: https://a.b.com/page/1012464399 \\n'
Cases feedback: https://a.b.com/page/1041338613"
# run!
java -jar $filename -uid $uid -text $text
The argument text
should be parsed in the Java program and output itself like follows:
These are people who are on call: @$onCallName1, @$onCallName2 and @$onCallName3. Please focus on the cases of online problems. Safe cases monitor: https://km.sankuai.com/page/992645614 Old cases log: https://km.sankuai.com/page/1012464399 Cases feedback: https://km.sankuai.com/page/1041338613
But when I run this shell script like bash a.sh
, it only outputs these:
These
Could anyone help me and give me some advice?
Thanks in advances.
Solution
Try rewriting your script as follow and please tell if it works or not for you too:
#!/usr/bin/bash
filename="DX.jar"
uid="111,110"
onCallName1="Tom"
onCallName2="Jerry"
onCallName3="Brun"
text="These are people who are on call: @$onCallName1, @$onCallName2 and @$onCallName3. \n'
Please focus on the cases of online problems. \n'
Safe cases monitor: https://a.b.com/page/992645614 \n'
Old cases log: https://a.b.com/page/1012464399 \n'
Cases feedback: https://a.b.com/page/1041338613"
# run!
java -jar "$filename" -uid "$uid" -text "$text"
In other words:
- Leave carriage returns unescaped.
- Put java command line arguments inside double quotes.
Answered By - Antonio Petricca Answer Checked By - David Marino (WPSolving Volunteer)