Environment: linux shell or better, ash shell in busybox (example system: Openwrt).
Given a string variable with large text "${a}"
constructed in this way:
for index in $(seq 1 40000); do #it is 40000 for a reason, it helps if you want to reproduce the error on your system.
a="${a}""\n""word ${index}"
done
and given the fact that trying to use this variable as argument for common command produce an error, for example:
echo "${a}" #fails
awk -v VAR_A="${a}" '<program>' #fails
(the failures are due: http://www.in-ulm.de/~mascheck/various/argmax/ )
How do you write such a variable to a text file, possibly using only simple commands and not sed/awk/perl/other powerful interpreter.
Ideally the text file should look like:
word 1
word 2
word 3
...
and not like:
word 1\nword 2\nword 3\n...
Update1: Someone asks "Why you cannot change the script that produce the variable $a
?". Because i cannot due to, let's say, lack of authorization ("the script was working until today, so you can fix only the printing part"). So $a
and its format is given, i can only find a way to print it to a file.
Update2: Using the suggested "here docs" solve most of the problems but still i have the content printed in one line. Maybe is my config?
echo
failing.echo
is usually built-in and should not trigger the E2BIG error. – Stéphane Chazelas Aug 04 '14 at 20:43$a
at all. Consider:for i in 1 2 3 4 5 ; do echo "$i" ; done >file
. But doingfor i in $(seq $num) ; do echo word $i; done
is redundant because you can just doseq -s'word' $num
– mikeserv Aug 05 '14 at 06:45$a
, you want to print it to a file but you cannot change it. You receive this variable and your goal is printing it out. So your solution is ok but it is referring to another problem. – Pier A Aug 05 '14 at 08:34