Re: Big bug in shell scripting in Applescript
Re: Big bug in shell scripting in Applescript
- Subject: Re: Big bug in shell scripting in Applescript
- From: "Zavatone, Alex" <email@hidden>
- Date: Thu, 04 Aug 2011 15:29:32 -0700
- Acceptlanguage: en-US
- Thread-topic: Big bug in shell scripting in Applescript
In the meantime, I tried both of these
on SedNuketheFirstCharIfItIsAPeriod(myText)
set mySuffix to "<<<" & quoted form of myText
set cmd to "sed 's:^a::'" & mySuffix
set myText to do shell script cmd without altering line endings
set myText to characters 1 through -2 of myText as string
return myText
end SedNuketheFirstCharIfItIsAPeriod
That works.
So does this, but I need the "without altering line endings" part
on SedNuketheFirstCharIfItIsAPeriod(myText)
set mySuffix to "<<<" & quoted form of myText
set cmd to "sed 's:^a::'" & mySuffix
set myText to do shell script cmd -- without altering line endings
-- set myText to characters 1 through -2 of myText as string
return myText
end SedNuketheFirstCharIfItIsAPeriod
It appears that this only happens with the "without altering line endings" phrase.
The thing is that I ended up getting thousands of extra linefeeds at the end of a string when I tested this out with a multi line string. Try it and see.
On Aug 4, 2011, at 5:17 PM, Mark J. Reed wrote:
> Not a bug, but something you have to be careful with.
>
> sed (and many other CLI tools) is line-oriented. It only deals with
> lines, and expects all its input to have at least one newline (at the
> end).
>
> Because of this, bash's "here-string" syntax (<<<'string goes here')
> *always appends a newline to the contents of the string*. Even if the
> string already has a newline on the end.
>
> If you want to avoid that, you can use a different construct. Instead of this:
>
> do shell script "some command <<<" & (quoted form of myString)
>
> Do this:
>
> do shell script "echo " & (quoted form of myString) "| some command"
>
>
>
> then the command will get a string with *two* newlines at the end.
>
>
>
>
> On Thu, Aug 4, 2011 at 6:08 PM, Zavatone, Alex <email@hidden> wrote:
>> Think I found a big bug in sed or shell scripting in AppleScript.
>> Copy the code below, run and look at the output.
>> \n gets appended to the last line of the return string every time this is
>> run.
>> on run
>> Main()
>> end run
>> on Main()
>> set myString to "abcdefg"
>> repeat with myIndex from 1 to 100
>> log the length of myString
>> set myString to SedNuketheFirstCharIfItIsAPeriod(myString)
>> end repeat
>> log the length of myString
>> end Main
>> on SedNuketheFirstCharIfItIsAPeriod(myText)
>> set mySuffix to "<<<" & quoted form of myText
>> set cmd to "sed 's:^a::'" & mySuffix
>> do shell script cmd without altering line endings
>> return result
>> end SedNuketheFirstCharIfItIsAPeriod
>>
>> This email message and any attachments are for the sole use of the intended
>> recipient(s) and may contain information that is proprietary to U.S.
>> Foodservice, Inc and/or its subsidiaries or otherwise confidential or
>> legally privileged. If you have received this message in error, please
>> notify the sender by reply, and delete all copies of this message and any
>> attachments. If you are the intended recipient you may use the information
>> contained in this message and any files attached to this message only as
>> authorized by U.S. Foodservice. Files attached to this message may only be
>> transmitted using secure systems and appropriate means of encryption, and
>> must be secured using the same level password and security protection with
>> which the file was provided to you. Any unauthorized use, dissemination or
>> disclosure of this message or it's attachments is strictly prohibited.
>>
>> _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> AppleScript-Users mailing list (email@hidden)
>> Help/Unsubscribe/Update your Subscription:
>> Archives: http://lists.apple.com/archives/applescript-users
>>
>> This email sent to email@hidden
>>
>>
>
>
>
> --
> Mark J. Reed <email@hidden>
This email message and any attachments are for the sole use of the intended recipient(s) and may contain information that is proprietary to U.S. Foodservice, Inc and/or its subsidiaries or otherwise confidential or legally privileged. If you have received this message in error, please notify the sender by reply, and delete all copies of this message and any attachments. If you are the intended recipient you may use the information contained in this message and any files attached to this message only as authorized by U.S. Foodservice. Files attached to this message may only be transmitted using secure systems and appropriate means of encryption, and must be secured using the same level password and security protection with which the file was provided to you. Any unauthorized use, dissemination or disclosure of this message or it's attachments is strictly prohibited.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden