Problem with passing path containing spaces and brackets

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












I use Thunar file explorer and pass pathname and filename to a script audioplay. audioplay runs play command on my android via ssh which plays a song.



audioplay script :: cat audioplay



if [ $# -eq 1 ] ; then
echo " Playing..." "$@"
sshpass -p $passwd ssh root@192.168.43.10 -p $port play "$@"
else
echo " No Input audio file."
fi


play script on my android :: cat play



 #!/data/bin/bash
# Avoid linker errors due to libOpenSLES.so:
echo "File ::" "$@"
LD_LIBRARY_PATH= exec /data/bin/blob/play-audio "$@"


Problem is Path does not get passed correctly to play command over ssh. Path with multi spaces becomes single space and brackets create syntax error.



Example :



For file 18. Warriors.flac present in /sdcard/Music/Imagine Dragons - Smoke Mirrors -Deluxe Edition-2015



Path sent to audioplay is correct :: /sdcard/Music/Imagine Dragons - Smoke Mirrors -Deluxe Edition-2015/18. Warriors.flac



But the path received by play script is wrong :: /sdcard/Music/Imagine Dragons - Smoke Mirrors -Deluxe Edition-2015/18. Warriors.flac
Multi spaces are turned into single space. Why ?



Then the main play-audio binary gets only path /sdcard/Music/Imagine upto first space only.



Example 2 ::



I tried with file having brackets :: 44. Go Tell Aunt Rhody -Resident Evil- (Short Version).flac
Path received by audioplay script is correct :: /sdcard/Music/44. Go Tell Aunt Rhody -Resident Evil- (Short Version).flac
But I get this error :: sh: syntax error: '(' unexpected



I tried with passing $@ $* "$@" and "$*". But none of them worked for me.










share|improve this question





















  • See How to execute an arbitrary simple command over ssh without knowing the login shell of the remote user?. Also remember to quote your variables
    – Stéphane Chazelas
    Sep 26 '17 at 15:30














up vote
3
down vote

favorite












I use Thunar file explorer and pass pathname and filename to a script audioplay. audioplay runs play command on my android via ssh which plays a song.



audioplay script :: cat audioplay



if [ $# -eq 1 ] ; then
echo " Playing..." "$@"
sshpass -p $passwd ssh root@192.168.43.10 -p $port play "$@"
else
echo " No Input audio file."
fi


play script on my android :: cat play



 #!/data/bin/bash
# Avoid linker errors due to libOpenSLES.so:
echo "File ::" "$@"
LD_LIBRARY_PATH= exec /data/bin/blob/play-audio "$@"


Problem is Path does not get passed correctly to play command over ssh. Path with multi spaces becomes single space and brackets create syntax error.



Example :



For file 18. Warriors.flac present in /sdcard/Music/Imagine Dragons - Smoke Mirrors -Deluxe Edition-2015



Path sent to audioplay is correct :: /sdcard/Music/Imagine Dragons - Smoke Mirrors -Deluxe Edition-2015/18. Warriors.flac



But the path received by play script is wrong :: /sdcard/Music/Imagine Dragons - Smoke Mirrors -Deluxe Edition-2015/18. Warriors.flac
Multi spaces are turned into single space. Why ?



Then the main play-audio binary gets only path /sdcard/Music/Imagine upto first space only.



Example 2 ::



I tried with file having brackets :: 44. Go Tell Aunt Rhody -Resident Evil- (Short Version).flac
Path received by audioplay script is correct :: /sdcard/Music/44. Go Tell Aunt Rhody -Resident Evil- (Short Version).flac
But I get this error :: sh: syntax error: '(' unexpected



I tried with passing $@ $* "$@" and "$*". But none of them worked for me.










share|improve this question





















  • See How to execute an arbitrary simple command over ssh without knowing the login shell of the remote user?. Also remember to quote your variables
    – Stéphane Chazelas
    Sep 26 '17 at 15:30












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I use Thunar file explorer and pass pathname and filename to a script audioplay. audioplay runs play command on my android via ssh which plays a song.



audioplay script :: cat audioplay



if [ $# -eq 1 ] ; then
echo " Playing..." "$@"
sshpass -p $passwd ssh root@192.168.43.10 -p $port play "$@"
else
echo " No Input audio file."
fi


play script on my android :: cat play



 #!/data/bin/bash
# Avoid linker errors due to libOpenSLES.so:
echo "File ::" "$@"
LD_LIBRARY_PATH= exec /data/bin/blob/play-audio "$@"


Problem is Path does not get passed correctly to play command over ssh. Path with multi spaces becomes single space and brackets create syntax error.



Example :



For file 18. Warriors.flac present in /sdcard/Music/Imagine Dragons - Smoke Mirrors -Deluxe Edition-2015



Path sent to audioplay is correct :: /sdcard/Music/Imagine Dragons - Smoke Mirrors -Deluxe Edition-2015/18. Warriors.flac



But the path received by play script is wrong :: /sdcard/Music/Imagine Dragons - Smoke Mirrors -Deluxe Edition-2015/18. Warriors.flac
Multi spaces are turned into single space. Why ?



Then the main play-audio binary gets only path /sdcard/Music/Imagine upto first space only.



Example 2 ::



I tried with file having brackets :: 44. Go Tell Aunt Rhody -Resident Evil- (Short Version).flac
Path received by audioplay script is correct :: /sdcard/Music/44. Go Tell Aunt Rhody -Resident Evil- (Short Version).flac
But I get this error :: sh: syntax error: '(' unexpected



I tried with passing $@ $* "$@" and "$*". But none of them worked for me.










share|improve this question













I use Thunar file explorer and pass pathname and filename to a script audioplay. audioplay runs play command on my android via ssh which plays a song.



audioplay script :: cat audioplay



if [ $# -eq 1 ] ; then
echo " Playing..." "$@"
sshpass -p $passwd ssh root@192.168.43.10 -p $port play "$@"
else
echo " No Input audio file."
fi


play script on my android :: cat play



 #!/data/bin/bash
# Avoid linker errors due to libOpenSLES.so:
echo "File ::" "$@"
LD_LIBRARY_PATH= exec /data/bin/blob/play-audio "$@"


Problem is Path does not get passed correctly to play command over ssh. Path with multi spaces becomes single space and brackets create syntax error.



Example :



For file 18. Warriors.flac present in /sdcard/Music/Imagine Dragons - Smoke Mirrors -Deluxe Edition-2015



Path sent to audioplay is correct :: /sdcard/Music/Imagine Dragons - Smoke Mirrors -Deluxe Edition-2015/18. Warriors.flac



But the path received by play script is wrong :: /sdcard/Music/Imagine Dragons - Smoke Mirrors -Deluxe Edition-2015/18. Warriors.flac
Multi spaces are turned into single space. Why ?



Then the main play-audio binary gets only path /sdcard/Music/Imagine upto first space only.



Example 2 ::



I tried with file having brackets :: 44. Go Tell Aunt Rhody -Resident Evil- (Short Version).flac
Path received by audioplay script is correct :: /sdcard/Music/44. Go Tell Aunt Rhody -Resident Evil- (Short Version).flac
But I get this error :: sh: syntax error: '(' unexpected



I tried with passing $@ $* "$@" and "$*". But none of them worked for me.







bash ssh filenames path arguments






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 26 '17 at 15:26









jonny789

8910




8910











  • See How to execute an arbitrary simple command over ssh without knowing the login shell of the remote user?. Also remember to quote your variables
    – Stéphane Chazelas
    Sep 26 '17 at 15:30
















  • See How to execute an arbitrary simple command over ssh without knowing the login shell of the remote user?. Also remember to quote your variables
    – Stéphane Chazelas
    Sep 26 '17 at 15:30















See How to execute an arbitrary simple command over ssh without knowing the login shell of the remote user?. Also remember to quote your variables
– Stéphane Chazelas
Sep 26 '17 at 15:30




See How to execute an arbitrary simple command over ssh without knowing the login shell of the remote user?. Also remember to quote your variables
– Stéphane Chazelas
Sep 26 '17 at 15:30










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Quote it again:



if [ "$#" -eq 1 ] ; then
echo " Playing..." "$1"
sshpass -p "$passwd" ssh root@192.168.43.10 -p "$port" "play '$1'"
else
echo " No Input audio file."
fi


The thing is that the first layer of quotes, the "$@" are expanded when you run the sshpass command. So by the time you log in, launch the remote shell and execute play, you have an unquoted string, so your script chokes on whitespace. By enclosing it in a second layer of quotes, you ensure that it is passed quoted to the remote command.



This should work assuming that i) the remote machine is running sh or bash or a similar shell and ii) your file names don't contain single quotes. To make it truly robust, please see How to execute an arbitrary simple command over ssh without knowing the login shell of the remote user?.



For properly quoting an arbitrary string (or even a list of arbitrary strings so you can pass more than one argument to play) in the sh syntax, we can use that shquote() function mentioned there:



shquote() 
LC_ALL=C awk -v q=' -v b='\' '
BEGIN
for (i=1; i<ARGC; i++)
gsub(q, q b q q, ARGV[i])
printf "%s ", q ARGV[i] q

print ""
exit
' "$@"

if [ "$#" -ge 1 ] ; then
echo " Playing..." "$@"
sshpass -p "$passwd" ssh root@192.168.43.10 -p "$port" "play $(shquote "$@")"
else
echo " No Input audio file."
fi


It would quote Sgt. Pepper's lonely hearts club band.flac as 'Sgt. Pepper'''s lonely hearts club band.mp3' which is correct for sh (and evil';reboot;: '.flac as 'evil''';reboot;: '''.flac' instead of rebooting the remote machine (do you have to run that play command as root?)).






share|improve this answer






















  • @StéphaneChazelas since it's an Android device, it's safe (I think) to assume the remote is running sh. But yes, it will indeed break on single quotes.
    – terdon♦
    Sep 26 '17 at 15:36










  • It worked. Thanks a lot. I spent hours on it searching and trying.
    – jonny789
    Sep 26 '17 at 15:41










  • @jonny789 you're welcome. But do read Stéphane's answer in the linked post for more detail.
    – terdon♦
    Sep 26 '17 at 15:44










  • Yes. I login to android as root user. Running play as root works without any issue.
    – jonny789
    Sep 26 '17 at 17:06











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f394568%2fproblem-with-passing-path-containing-spaces-and-brackets%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Quote it again:



if [ "$#" -eq 1 ] ; then
echo " Playing..." "$1"
sshpass -p "$passwd" ssh root@192.168.43.10 -p "$port" "play '$1'"
else
echo " No Input audio file."
fi


The thing is that the first layer of quotes, the "$@" are expanded when you run the sshpass command. So by the time you log in, launch the remote shell and execute play, you have an unquoted string, so your script chokes on whitespace. By enclosing it in a second layer of quotes, you ensure that it is passed quoted to the remote command.



This should work assuming that i) the remote machine is running sh or bash or a similar shell and ii) your file names don't contain single quotes. To make it truly robust, please see How to execute an arbitrary simple command over ssh without knowing the login shell of the remote user?.



For properly quoting an arbitrary string (or even a list of arbitrary strings so you can pass more than one argument to play) in the sh syntax, we can use that shquote() function mentioned there:



shquote() 
LC_ALL=C awk -v q=' -v b='\' '
BEGIN
for (i=1; i<ARGC; i++)
gsub(q, q b q q, ARGV[i])
printf "%s ", q ARGV[i] q

print ""
exit
' "$@"

if [ "$#" -ge 1 ] ; then
echo " Playing..." "$@"
sshpass -p "$passwd" ssh root@192.168.43.10 -p "$port" "play $(shquote "$@")"
else
echo " No Input audio file."
fi


It would quote Sgt. Pepper's lonely hearts club band.flac as 'Sgt. Pepper'''s lonely hearts club band.mp3' which is correct for sh (and evil';reboot;: '.flac as 'evil''';reboot;: '''.flac' instead of rebooting the remote machine (do you have to run that play command as root?)).






share|improve this answer






















  • @StéphaneChazelas since it's an Android device, it's safe (I think) to assume the remote is running sh. But yes, it will indeed break on single quotes.
    – terdon♦
    Sep 26 '17 at 15:36










  • It worked. Thanks a lot. I spent hours on it searching and trying.
    – jonny789
    Sep 26 '17 at 15:41










  • @jonny789 you're welcome. But do read Stéphane's answer in the linked post for more detail.
    – terdon♦
    Sep 26 '17 at 15:44










  • Yes. I login to android as root user. Running play as root works without any issue.
    – jonny789
    Sep 26 '17 at 17:06















up vote
2
down vote



accepted










Quote it again:



if [ "$#" -eq 1 ] ; then
echo " Playing..." "$1"
sshpass -p "$passwd" ssh root@192.168.43.10 -p "$port" "play '$1'"
else
echo " No Input audio file."
fi


The thing is that the first layer of quotes, the "$@" are expanded when you run the sshpass command. So by the time you log in, launch the remote shell and execute play, you have an unquoted string, so your script chokes on whitespace. By enclosing it in a second layer of quotes, you ensure that it is passed quoted to the remote command.



This should work assuming that i) the remote machine is running sh or bash or a similar shell and ii) your file names don't contain single quotes. To make it truly robust, please see How to execute an arbitrary simple command over ssh without knowing the login shell of the remote user?.



For properly quoting an arbitrary string (or even a list of arbitrary strings so you can pass more than one argument to play) in the sh syntax, we can use that shquote() function mentioned there:



shquote() 
LC_ALL=C awk -v q=' -v b='\' '
BEGIN
for (i=1; i<ARGC; i++)
gsub(q, q b q q, ARGV[i])
printf "%s ", q ARGV[i] q

print ""
exit
' "$@"

if [ "$#" -ge 1 ] ; then
echo " Playing..." "$@"
sshpass -p "$passwd" ssh root@192.168.43.10 -p "$port" "play $(shquote "$@")"
else
echo " No Input audio file."
fi


It would quote Sgt. Pepper's lonely hearts club band.flac as 'Sgt. Pepper'''s lonely hearts club band.mp3' which is correct for sh (and evil';reboot;: '.flac as 'evil''';reboot;: '''.flac' instead of rebooting the remote machine (do you have to run that play command as root?)).






share|improve this answer






















  • @StéphaneChazelas since it's an Android device, it's safe (I think) to assume the remote is running sh. But yes, it will indeed break on single quotes.
    – terdon♦
    Sep 26 '17 at 15:36










  • It worked. Thanks a lot. I spent hours on it searching and trying.
    – jonny789
    Sep 26 '17 at 15:41










  • @jonny789 you're welcome. But do read Stéphane's answer in the linked post for more detail.
    – terdon♦
    Sep 26 '17 at 15:44










  • Yes. I login to android as root user. Running play as root works without any issue.
    – jonny789
    Sep 26 '17 at 17:06













up vote
2
down vote



accepted







up vote
2
down vote



accepted






Quote it again:



if [ "$#" -eq 1 ] ; then
echo " Playing..." "$1"
sshpass -p "$passwd" ssh root@192.168.43.10 -p "$port" "play '$1'"
else
echo " No Input audio file."
fi


The thing is that the first layer of quotes, the "$@" are expanded when you run the sshpass command. So by the time you log in, launch the remote shell and execute play, you have an unquoted string, so your script chokes on whitespace. By enclosing it in a second layer of quotes, you ensure that it is passed quoted to the remote command.



This should work assuming that i) the remote machine is running sh or bash or a similar shell and ii) your file names don't contain single quotes. To make it truly robust, please see How to execute an arbitrary simple command over ssh without knowing the login shell of the remote user?.



For properly quoting an arbitrary string (or even a list of arbitrary strings so you can pass more than one argument to play) in the sh syntax, we can use that shquote() function mentioned there:



shquote() 
LC_ALL=C awk -v q=' -v b='\' '
BEGIN
for (i=1; i<ARGC; i++)
gsub(q, q b q q, ARGV[i])
printf "%s ", q ARGV[i] q

print ""
exit
' "$@"

if [ "$#" -ge 1 ] ; then
echo " Playing..." "$@"
sshpass -p "$passwd" ssh root@192.168.43.10 -p "$port" "play $(shquote "$@")"
else
echo " No Input audio file."
fi


It would quote Sgt. Pepper's lonely hearts club band.flac as 'Sgt. Pepper'''s lonely hearts club band.mp3' which is correct for sh (and evil';reboot;: '.flac as 'evil''';reboot;: '''.flac' instead of rebooting the remote machine (do you have to run that play command as root?)).






share|improve this answer














Quote it again:



if [ "$#" -eq 1 ] ; then
echo " Playing..." "$1"
sshpass -p "$passwd" ssh root@192.168.43.10 -p "$port" "play '$1'"
else
echo " No Input audio file."
fi


The thing is that the first layer of quotes, the "$@" are expanded when you run the sshpass command. So by the time you log in, launch the remote shell and execute play, you have an unquoted string, so your script chokes on whitespace. By enclosing it in a second layer of quotes, you ensure that it is passed quoted to the remote command.



This should work assuming that i) the remote machine is running sh or bash or a similar shell and ii) your file names don't contain single quotes. To make it truly robust, please see How to execute an arbitrary simple command over ssh without knowing the login shell of the remote user?.



For properly quoting an arbitrary string (or even a list of arbitrary strings so you can pass more than one argument to play) in the sh syntax, we can use that shquote() function mentioned there:



shquote() 
LC_ALL=C awk -v q=' -v b='\' '
BEGIN
for (i=1; i<ARGC; i++)
gsub(q, q b q q, ARGV[i])
printf "%s ", q ARGV[i] q

print ""
exit
' "$@"

if [ "$#" -ge 1 ] ; then
echo " Playing..." "$@"
sshpass -p "$passwd" ssh root@192.168.43.10 -p "$port" "play $(shquote "$@")"
else
echo " No Input audio file."
fi


It would quote Sgt. Pepper's lonely hearts club band.flac as 'Sgt. Pepper'''s lonely hearts club band.mp3' which is correct for sh (and evil';reboot;: '.flac as 'evil''';reboot;: '''.flac' instead of rebooting the remote machine (do you have to run that play command as root?)).







share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 26 '17 at 15:54









Stéphane Chazelas

284k53523859




284k53523859










answered Sep 26 '17 at 15:33









terdon♦

123k28232404




123k28232404











  • @StéphaneChazelas since it's an Android device, it's safe (I think) to assume the remote is running sh. But yes, it will indeed break on single quotes.
    – terdon♦
    Sep 26 '17 at 15:36










  • It worked. Thanks a lot. I spent hours on it searching and trying.
    – jonny789
    Sep 26 '17 at 15:41










  • @jonny789 you're welcome. But do read Stéphane's answer in the linked post for more detail.
    – terdon♦
    Sep 26 '17 at 15:44










  • Yes. I login to android as root user. Running play as root works without any issue.
    – jonny789
    Sep 26 '17 at 17:06

















  • @StéphaneChazelas since it's an Android device, it's safe (I think) to assume the remote is running sh. But yes, it will indeed break on single quotes.
    – terdon♦
    Sep 26 '17 at 15:36










  • It worked. Thanks a lot. I spent hours on it searching and trying.
    – jonny789
    Sep 26 '17 at 15:41










  • @jonny789 you're welcome. But do read Stéphane's answer in the linked post for more detail.
    – terdon♦
    Sep 26 '17 at 15:44










  • Yes. I login to android as root user. Running play as root works without any issue.
    – jonny789
    Sep 26 '17 at 17:06
















@StéphaneChazelas since it's an Android device, it's safe (I think) to assume the remote is running sh. But yes, it will indeed break on single quotes.
– terdon♦
Sep 26 '17 at 15:36




@StéphaneChazelas since it's an Android device, it's safe (I think) to assume the remote is running sh. But yes, it will indeed break on single quotes.
– terdon♦
Sep 26 '17 at 15:36












It worked. Thanks a lot. I spent hours on it searching and trying.
– jonny789
Sep 26 '17 at 15:41




It worked. Thanks a lot. I spent hours on it searching and trying.
– jonny789
Sep 26 '17 at 15:41












@jonny789 you're welcome. But do read Stéphane's answer in the linked post for more detail.
– terdon♦
Sep 26 '17 at 15:44




@jonny789 you're welcome. But do read Stéphane's answer in the linked post for more detail.
– terdon♦
Sep 26 '17 at 15:44












Yes. I login to android as root user. Running play as root works without any issue.
– jonny789
Sep 26 '17 at 17:06





Yes. I login to android as root user. Running play as root works without any issue.
– jonny789
Sep 26 '17 at 17:06


















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f394568%2fproblem-with-passing-path-containing-spaces-and-brackets%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay