Friday, May 27, 2022

#1 2010-10-23 04:54:02 am

ljr_nbg
Member
From:: Aix-la-Chapelle/Germany
Registered: 2010-10-23
Posts: 26
Website

do shell script: echo double backslash

Hello everyone,

this is my first post. First of all: thanks to everyone posting here. Macscripter has been a _very_ helpful resource for me in the past.

Here is my problem:

I'm trying to match Windows UNC paths (\\networkShare\folder) with perl's regex. Usually I do something like the following:

Applescript:

do shell script "echo " & quoted form of "something" & "| perl -pe 's/some/any/g'" --> anything

which has always been working well for me.

When trying to 'echo' two backslashes, I found the following behavior that seems rather strange to me:

Applescript:

do shell script "echo " & quoted form of "\\\\" --> "\\" (AS escaped)

'echo' seems to automatically replace double backslashes with a single one, no matter where the double backslash is located. Therefore it also does the following:

Applescript:

do shell script "echo " & quoted form of "bla\\\\bla" --> "bla\\bla" (AS escaped)

And finally when echoing a UNC path:

Applescript:

do shell script "echo " & quoted form of "\\\\networkShare\\folder"

it returns two lines in the result window with a single leading backslash:

"\\networkShare
older"


What am I not getting here? Usually the 'quoted form' works very well in such cases.

Any help welcome. Thanks a lot!

Luke

AppleScript: 2.1.2
Browser: Safari 533.18.5
Operating System: Mac OS X (10.6)

Last edited by ljr_nbg (2010-10-23 09:42:47 am)

Offline

 

#2 2010-10-23 08:14:22 am

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: do shell script: echo double backslash

That is an applescript effect, not an echo effect. For example, try to create a string like this in applescript... I said "Hello".

Since the quote mark is special to applescript (because that's what we use to encapsulate a string), in order to use it in a string you must "escape" the quote mark.The following is what you would have to type to include a quote mark in a string...

Applescript:

set theString to "I said \"Hello\""
display dialog theString

As you can see the escape character \ is another special applescript character. Therefore to include a backslash character in a string in applescript you must also escape it...

Applescript:

set theString to "Here is a backslash character in an applescript string \\"
display dialog theString

So basically you need to escape special applescript characters in strings, such that applescript will pass the proper string to wherever you want to use the string... in your case with the echo shell command. The "quoted form of" use with a shell command is an applescript command that properly escapes strings for use with the shell. The shell has different rules for escaping characters and luckily applescript has the "quoted form of" command to properly handle the shell rules for you. So one command is for use in applescript and one command is for use with the shell.

Last edited by regulus6633 (2010-10-23 08:39:09 am)

Offline

 

#3 2010-10-23 08:37:30 am

ljr_nbg
Member
From:: Aix-la-Chapelle/Germany
Registered: 2010-10-23
Posts: 26
Website

Re: do shell script: echo double backslash

Hello Hank,

thanks for your answer.

I am aware of the fact that some chars have to be escaped in AppleScript. That's why my two backslashes are properly AS escaped using four backslashes in total and then passed to the shell using 'quoted form of'.
Info: I also used the escaped form for the results that I posted (-->).

My problem is that the 'do shell script "echo..."' literally replaces any occurrence of \\ with \.

Please try it out using my example number 2 and you will see what I mean.

It seems to me that this only happens to double backslashes. Things like

Applescript:

do shell script "echo " & quoted form of "\"\"" --> "\"\""

return the expected results.

Does anyone know anything about this?
Is it a bug or some special shell voodoo that I am not aware of?

Thanks
Luke

Last edited by ljr_nbg (2010-10-23 08:41:19 am)

Offline

 

#4 2010-10-23 08:43:28 am

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: do shell script: echo double backslash

My problem is that the 'do shell script "echo..."' literally replaces any occurrence of \\ with \.


I'm not seeing what you describe. Try this... applescript and the echo command both properly return \\.

Applescript:

set a to "\\\\"
display dialog a
do shell script "echo " & quoted form of a

Last edited by regulus6633 (2010-10-23 08:46:24 am)

Offline

 

#5 2010-10-23 08:46:16 am

ljr_nbg
Member
From:: Aix-la-Chapelle/Germany
Registered: 2010-10-23
Posts: 26
Website

Re: do shell script: echo double backslash

Please try this and you will see what I mean:

Applescript:

set a to "\\\\"
display dialog a --> two backslashes
set a to do shell script "echo " & quoted form of a
display dialog a --> only one backslash left

Please do not forget that strings in the results window remain AppleScript escaped. It may look like two, but it is just one backslash.

Thanks for your help.

Last edited by ljr_nbg (2010-10-23 08:49:32 am)

Offline

 

#6 2010-10-23 08:49:54 am

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: do shell script: echo double backslash

That's an interesting example. Now I see what you mean. Try this though. It seems that the proper command is sent to the shell, although when the result of the shell command is passed back to applescript is where your problem arises. And I'm not sure what to do about that.

Applescript:

set a to "\\\\"
display dialog a
tell application "Terminal"
   do script "echo " & quoted form of a
end tell

Last edited by regulus6633 (2010-10-23 08:53:15 am)

Offline

 

#7 2010-10-23 08:59:13 am

ljr_nbg
Member
From:: Aix-la-Chapelle/Germany
Registered: 2010-10-23
Posts: 26
Website

Re: do shell script: echo double backslash

Good point. Thank you.

I was about to think that writing the output to a file and then read it back in would work around this problem. But then I tried this:

Applescript:

do shell script "echo " & quoted form of "\\\\" & " > ~/Desktop/test.txt"

Only one backslash in 'test.txt' ;-)

Very strange (aaaaaaahhhh....)

Offline

 

#8 2010-10-23 09:05:28 am

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: do shell script: echo double backslash

Very strange indeed as this works...

Applescript:

set a to "\\\\"
tell application "Terminal"
   do script "echo " & quoted form of a & " > ~/Desktop/test.txt"
end tell
set b to read file ((path to desktop as text) & "test.txt")

So the "do script" in a terminal window is different than a "do shell script" command... never knew that.

Offline

 

#9 2010-10-23 09:15:44 am

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: do shell script: echo double backslash

Just a thought... a terminal window uses the bash shell whereas the "do shell script" command uses the sh shell... so is that the reason for the difference?

Offline

 

#10 2010-10-23 09:20:34 am

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: do shell script: echo double backslash

Yep, that's the difference. It's how sh is handling it versus bash. If you type this in the Terminal the output file will contain only 1 backslash...

Applescript:

hmcshane@macbook:~ $ sh
hmcshane@macbook:~ $ echo '\\' > ~/Desktop/test2.txt

You see the first "sh" command puts us in the sh shell and it sees the double backslash as one slash... whereas bash doesn't. So we need a way to make "do shell script" use the bash shell. I'm not sure how to do that but I'll think about it.

Last edited by regulus6633 (2010-10-23 09:23:11 am)

Offline

 

#11 2010-10-23 09:28:12 am

ljr_nbg
Member
From:: Aix-la-Chapelle/Germany
Registered: 2010-10-23
Posts: 26
Website

Re: do shell script: echo double backslash

Neither did I. You live and learn ;-)

Yes, making 'do shell script' use the bash shell is exactly what I was thinking after reading your last post (#9) as well. To bad that I don't know how to do so yet ;-)

I tried to do it like in terminal

Applescript:

do shell script "bash; echo " & quoted form of "\\\\"

but that didn't do it (I guess that would have been too easy).

I guess I could write the UNC path to a file using Standard Addtions, process that file in the shell and then read the result back in. But I'd rather do it the 'normal' way using 'do shell script' and 'echo ...|...'.

Thanks for your support.

Does anyone know by heart how to change the shell that 'do shell script' uses or an alternative solution?

Last edited by ljr_nbg (2010-10-23 09:41:44 am)

Offline

 

#12 2010-10-23 09:55:15 am

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: do shell script: echo double backslash

ljr_nbg wrote:

Does anyone know by heart how to change the shell that 'do shell script' uses or an alternative solution?


See here for that. Look under the section titled "Q: How do I run my command with a shell other than sh?"

I can't get it to work properly with bash. However tsch works.

Applescript:

set a to "\\\\"
do shell script "/bin/tcsh -c 'echo \"" & a & "\"'"

Notice that I did not use "quoted form of". Putting the echo command in single quotes (e.g. quoted form of) doesn't work... even on the command line... double-quotes does work. All of this is really strange!

Offline

 

#13 2010-10-23 10:13:18 am

ljr_nbg
Member
From:: Aix-la-Chapelle/Germany
Registered: 2010-10-23
Posts: 26
Website

Re: do shell script: echo double backslash

Hank, thanks for the link and the section. I scrubbed through this page earlier but obviously missed that section.

I tried something similar:

Applescript:

do shell script "/bin/bash -c \"echo " & quoted form of "\\\\" & "\"" --> "\\" (AS escaped)

but that doesn't do it either.

I like your tcsh command but unfortunately I wanna pipe an entire email content from Apple Mail into perl and really need to use the 'quoted form' to be sure that everything is properly escaped.

Offline

 

#14 2010-10-23 10:33:47 am

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: do shell script: echo double backslash

I understand. The best solution I think would be to create a bash shell script on-the-fly. Then run it and get the result, then delete the shell script. It's a little messy but it works!

Applescript:

set a to "\\\\"
set cmd to "echo " & quoted form of a

-- create a bash shell script
set LF to character id 10 -- a unix line feed
set bashScriptPath to (path to desktop as text) & "bashScript"
set bashText to "#!/bin/bash" & LF & cmd

-- write the bash script
set openFile to open for access file bashScriptPath with write permission
write bashText to openFile as text
close access openFile

-- make the bash script executable
do shell script "chmod a+x " & quoted form of POSIX path of bashScriptPath

-- run the bash script
set theResult to do shell script quoted form of POSIX path of bashScriptPath

-- delete the bash script
do shell script "rm " & quoted form of POSIX path of bashScriptPath

return theResult

Offline

 

#15 2010-10-23 10:36:05 am

ljr_nbg
Member
From:: Aix-la-Chapelle/Germany
Registered: 2010-10-23
Posts: 26
Website

Re: do shell script: echo double backslash

Here we go. Thanks, Hank! With your help I think I came up with a solution that works.

First I tried this, which works:

Applescript:

set a to "\\\\netWorkShare\\folder\\file"
set a to quoted form of a
set b to quoted form of ("echo " & a)
do shell script "/bin/tcsh -c " & b --> "\\\\netWorkShare\\folder\\file" (AS escaped)

But when I tried to pass the following

test line "1"
test line "2"

\\netWorkShare\folder\file

test line "3"
test line "4"


it failed (Unmatched single quote).
NOT WORKING:

Applescript:

set a to "test line \"1\"\ntest line \"2\"\n\n\\\\netWorkShare\\folder\\file\n\ntest line \"3\"\ntest line \"4\""
set a to quoted form of a
set b to quoted form of ("echo " & a)
do shell script "/bin/tcsh -c " & b --> ERROR: Unmatched '.

So I tried this with /bin/bash:

Applescript:

set a to "test line \"1\"\ntest line \"2\"\n\n\\\\netWorkShare\\folder\\file\n\ntest line \"3\"\ntest line \"4\""
set a to quoted form of a
set b to quoted form of ("echo " & a)
do shell script "/bin/bash -c " & b --> returns correct string, double backslashes still intact

and it returns the expected result.

Also piping it into perl seems to work well:

Applescript:

set a to "test line \"1\"\ntest line \"2\"\n\n\\\\netWorkShare\\folder\\file\n\ntest line \"3\"\ntest line \"4\""
set a to quoted form of a
set b to quoted form of ("echo " & a)
do shell script "/bin/bash -c " & b & " | perl -pe 's/\\\\/a/g'" --> all backslashes replaced by 'a'

Thanks again, Hank, for your time and your interest.

Hoever, I still agree that this is _very_ strange. If anyone has an explanation, I'll be very grateful.

Offline

 

#16 2010-10-23 10:39:54 am

ljr_nbg
Member
From:: Aix-la-Chapelle/Germany
Registered: 2010-10-23
Posts: 26
Website

Re: do shell script: echo double backslash

Ups, we posted at the same time.

I will definitely use your solution if I still run into more (strange, inexplicable) problems. I cannot understand what is happening here.

Really, Hank, thanks for your support. This was a real pain which is now eased.

Offline

 

#17 2010-10-23 10:45:24 am

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: do shell script: echo double backslash

I'm happy to help and glad you got something working. This was at least interesting although very frustrating too. Anyway good luck.c:D

Offline

 

#18 2011-07-04 03:58:18 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: do shell script: echo double backslash

ljr_nbg wrote:

Please try this and you will see what I mean:

Applescript:

set a to "\\\\"
display dialog a --> two backslashes
set a to do shell script "echo " & quoted form of a
display dialog a --> only one backslash left

Please do not forget that strings in the results window remain AppleScript escaped. It may look like two, but it is just one backslash.

Thanks for your help.


Well you're simply using the wrong echo here. I always use the POSIX echo because it doesn't have this problem. You're using another echo by default but when you're using echo in the /bin folder you'll see both times two slashes.

Applescript:

set a to "\\\\"
display dialog a --> two backslashes
set a to do shell script "/bin/echo " & quoted form of a
display dialog a --> two backslashes

Offline

 

#19 2011-07-04 04:02:48 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: do shell script: echo double backslash

Also I found it because the default echo also doesn't support the ignoring trailing new line when piping to another command.  So in my opinion use also '/bin/echo' instead because the default echo will always send a trailing newline when piping.

Offline

 

#20 2012-01-20 03:33:25 am

ljr_nbg
Member
From:: Aix-la-Chapelle/Germany
Registered: 2010-10-23
Posts: 26
Website

Re: do shell script: echo double backslash

Sorry, haven't checked in in a while.

Thanks for your tip, DJ Bazzie! It's very helpful! Great finding!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)