Busy box Read file line by line
10,624
read
is a shell builtin (it couldn't set a shell variable if it were not).
So, if your busybox sh
is based on ash
, it's:
while IFS= read -r line <&3; do
printf '%s\n' "$line"
done 3< "$InputFile"
Like in any POSIX shell. But like with any shell, using while read
loops to process text is generally bad shell scripting practice.
Above, you need:
-
IFS=
otherwise leading and trailing unescaped spaces and tabs are stripped from the lines -
-r
, otherwise backslashes are treated as an escape character and removed (unless escaped) -
printf
, notecho
which wouldn't work for lines that are for instance-nene
-
"$line"
quoted (not$line
) otherwise the content of the line is split on spaces and tabs, and globbing patterns expanded. -
<&3
and3< ...
, if you need access to the original stdin within the loop.
If the file contains characters after the last line and you want to display them, you can add after the loop:
[ -z "$line" ] || printf %s "$line"
Note that that loop cannot handle binary data (the NUL
character).
Related videos on Youtube
![limovala](https://i.stack.imgur.com/rXDJI.png?s=256&g=1)
Author by
limovala
Updated on September 18, 2022Comments
-
limovala almost 2 years
There is no read applet comming with busy box.
Is there any way to read a txt file line by line using busybox?
What I have now is
while read line do echo $line done < "$InputFile"
-
jordanm almost 11 yearsThat doesn't work? Busybox uses
ash
for a shell, which is not POSIX complaint, but still includes a POSIXread
. -
Gilles 'SO- stop being evil' almost 11 years
-
-
jordanm almost 11 yearsThis looks like unnecessary complexity to what should be a working example from the OP.
-
Stéphane Chazelas almost 11 years@jordanm, I've edited the answer to give reasons for the added complexity.
-
Stéphane Chazelas almost 11 yearsWhy do you need to prefix
busybox
to every command? Are you not entering that command line inbusybox
shell? Does that shell not have aread
builtin? (what doestype read
tell you there?). -
limovala almost 11 years@StephaneChazelas I am writing this script for an embedded device, so to make sure the busybox applet is called instead of the actual command, I use busybox prefix I am using Bourne shell '/bin/sh'
-
Stéphane Chazelas almost 11 yearsBut by doing so you end up using it differently from what your embedded device would (you don't want to call utilities like that as that means reexecuting busybox for every command which is a waste since nowadays busybox can call utilities without reexecuting itself).
busybox
will call its own commands in priority. If you don't want to call utilities from/bin
or/usr/bin
, remove those directories from$PATH
. -
limovala almost 11 years@StephaneChazelas thanks, I will Update my scripts
-
Admin over 8 yearsMaybe it could be better with
while IFS= read -r line <&3 || [ -n "$line" ] ; do
to avoid repeating the internals of the loop on non-null last line. Yes, it still will not handle a NUL character.