Trailing LFs in shell scripts
Trailing LFs in shell scripts
- Subject: Trailing LFs in shell scripts
- From: Paul Berkowitz <email@hidden>
- Date: Thu, 14 Nov 2002 08:53:26 -0800
Can someone explain to me how the 'altering line endings' parameter of 'do
shell script' is meant to work? It is the default.
In the following script, I assemble some text that already contains Mac CR
line endings (in real life this will be a 'given'). Then I use 'echo' to get
the text and output it to a file on the desktop. I'm doing it all in one go,
not line by line, so the intermediate line ends are all ASCII character 13.
But when I look at the file I see there is a final newline, and checking
that one shows it's a linefeed, ASCII character 10. Why is it there?
--just prepare some text
set p to "here goes "
set ls to {}
repeat with i from 1 to 99
set end of my ls to (p & i)
end repeat
set AppleScript's text item delimiters to {ASCII character 10}
set txt to ls as string
set AppleScript's text item delimiters to {""}
--write the text to a file
set testPath to (path to desktop as string) & "test1"
set utestPath to POSIX path of testPath
do shell script "echo '" & txt & "' > \"" & utestPath & "\"" --with altering
line endings
--------------------
(You can add 'with altering line endings or not' - it's the default.)
Now check up on the file:
set testPath to (path to desktop as string) & "test1"
set testFile to alias testPath
set r to read testFile
set l to length of paragraph 1 of r
set lineEnd to ASCII number (character (l + 1) of r)
--> 13
set finalChar to ASCII number (character -1 of r)
--> 10
If I use (ASCII character 10) instead of {return} when making up my txt
variable in the first script, so all line-endings are LF, they're all still
LF in the file. Since I'm not writing, or even operating within the shell
script, line by line, but rather just providing a "wadge" of text, that's
actually what I would expect.
But why the final trailing LF? How does this work? Is it intended, or a bug?
--
Paul Berkowitz
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.