0

I'm writing a shell (sh) script to be run on a BusyBox terminal. The script redirects a system command output to a file for logging purposes using the >> operator. How do I determine the end of line (EOL) character(s) being used, so that the script can accurately calculate the anticipated file size? Anticipated file size is important in my application because there are strict disk usage requirements.

(I'm aware that I could just look at the file in a binary editor and see exactly what's it's using for line ends, but my script needs to be portable. So, either I need to be reasonably confident that the EOL character(s) won't change--e.g. OS defined--or I need to be able to capture and count.)

4
  • What is the command being redirected? Commented Nov 2, 2020 at 18:23
  • @glennjackman candump Commented Nov 2, 2020 at 18:31
  • I think it would be application specific rather than OS specific, but that's not a definitive statement. Commented Nov 2, 2020 at 18:42
  • 1
    POSIX requires the end-of-line character to be a single byte (though it doesn't specify the encoding); if you've got reason to believe you'll be on a very atypical system this might be worth thinking about. Commented Nov 2, 2020 at 19:07

1 Answer 1

0

From your question, it sounds like you're not as concerned about what the EOL character(s) actually are, just how many bytes or characters an EOL takes.

How about measuring it?

EOL_BYTES="$(echo | wc --bytes)"

or

EOL_CHARS="$(echo | wc --chars)"

Run one of these at the top of your script, then use the value in your calculations.

1
  • Thanks. I had to modify slightly to work with my version of BusyBox. EOL_BYTES="$(echo | wc -c)" Commented Nov 2, 2020 at 19:56

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.