3

I'm trying to do

echo "foo
foo
foo
....[many many lines of foo here]
foo" > "/Users/myname/destination.txt"

through Applescript, but it returns an error when the characters in the lines of foo hit some amount (It looks like it's 255392 characters.).

By "through Applescript," I mean I'm trying this with AppleScript's "do shell script", which stops process when the exit status is non-zero and returns this: "error "The command exited with a non-zero status." number 255

Here's my Applescript code:

set mytext to "foo
foo
foo
....[many many lines of foo here]
foo"

do shell script ("echo " & quoted form of mytext & " > " & "/Users/myname/destination.txt")

The actual big text data (20000+ lines, 285831 characters) I use for input has a lot of quotation marks and backslashes, and I don't want to mess up my files by put my text data directly into terminal and have it misinterpret some parts of my input text data as commands or file names, so I'm not checking this directly with bash yet.

It might be more of an Applescript's problem, but I thought I might get response sooner here, so I'm posting this here.

Is there a limit of characters of input text in echo, or something? How can I avoid this?

Edit: I wrote this in a shell script and ran it on terminal and it worked but I'd like to do this through Applescript and I don't have no idea why doesn't this work in Applescript :

#!/bin/bash

echo "aaaaaaa[100000+ "a"s in here]aaaa" > "/Users/myname/destination.txt"
5
  • I suspect that's a limit on the length of a given shell command, not specific to echo. The fix is simple: Don't do it all in one command. And don't use echo. What are you actually trying to achieve, though? This seems several layers removed from your actual use case, which may be handled directly with a shell script.
    – Wildcard
    Commented Jun 8, 2016 at 3:41
  • I'd like to automate prepending text to my big text file. I managed to do appending text to it, by echo "foo" >> "/Users/myname/destination.txt", but I can't prepend text to it.
    – stacko
    Commented Jun 8, 2016 at 3:51
  • The example you list will have shown up in your file as aaaaaaa[100000+ as]aaaa if you look in your file.
    – Wildcard
    Commented Jun 8, 2016 at 3:56
  • Sorry, I just replaced the actual long string of "a"s with [100000+ "a"s in here]. The actual input string I tested didn't have quotation marks.
    – stacko
    Commented Jun 8, 2016 at 4:02
  • 1
    <irony-mode> There's actually an invention that's designed to contain really really big sequences of bytes, like 100K or more, and be easily handled by any programming language with an extremely short reference to it. It's even built into Mac OS X! It's called a "file." </irony-mode> ;)
    – Wildcard
    Commented Jun 8, 2016 at 4:05

1 Answer 1

1

To prepend text to a big text file, try one of the following. (These are all shell commands; I don't know applescript at all.)

echo "new text" > newfile
cat oldfile >> newfile
mv newfile oldfile

This is probably the simplest way.

Here is a very advanced way to do this, which uses ex for in-place file editing:

printf '%s\n' '0i' "Text to prepend" "Another line of text to prepend" . x | ex myfile

However, from your edit I suspect you are simply running into a lack of understanding of how quoting works in the shell.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .