-1

Assume that you have a POSIX/UNIX shell script that prints a long text:

$ sh ./shell_script.sh --help

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut imperdiet risus finibus nulla ullamcorper blandit. Aenean tempor lacinia aliquet.

Morbi imperdiet libero ligula, sit amet placerat ipsum facilisis nec. Aenean sed tellus a risus mollis convallis in sed sapien.

The following is how I usually script this. I follow the 80-character rule even with deep indentations:

while ...
  case $1 in
    --help)
      printf "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut "
      printf "imperdiet risus finibus nulla ullamcorper blandit. Aenean "
      printf "tempor lacinia aliquet.\n\n"
      printf "Morbi imperdiet libero ligula, sit amet placerat ipsum "
      printf "facilisis nec. Aenean sed tellus a risus mollis convallis in "
      printf "sed sapien.\n"
      ...
done

It is utterly tedious to manually split all lines of that text within 80 characters.

Moreover, I can update that text later. For example, I can replace Lorem with A new kind of dachshund Lorem. In this case, I need to update the script again.

Is there any efficient way to manage long texts in (shell) scripts (or codes in general)? Or, should I dismiss the 80-character rule in such a case?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Culip
  • 559
  • 8
  • 24
  • 1
    Is this relevant to Python? – PacketLoss Jan 19 '21 at 03:24
  • I believe so. In Python it is recommended to follow the 80-character rule as well. The "shell script" in the original question might be a red herring. Please feel free to update the question and the title. – Culip Jan 19 '21 at 03:26
  • Then in Python, or anything really... this should answer your question. https://stackoverflow.com/a/2657703/7942856 – PacketLoss Jan 19 '21 at 03:26
  • You meant, I should break every 64/80 characters by using an external script? It is doable, but meh. – Culip Jan 19 '21 at 03:28
  • No? Use regex to insert a new line every 80 chars. Then just store your text in a single string, apply the regex to it and print – PacketLoss Jan 19 '21 at 03:29
  • In that case, I don't need to follow the 80-character rule if I am allowed to store the string with >80 lines somewhere else.. Also assume that you would want to indent deeper; say, you add an if condition; then every line will have 4 characters fewer.. – Culip Jan 19 '21 at 03:33
  • https://stackoverflow.com/a/10840765/7942856 This would be to go to way to handle that in Python. Will reduce your variables and prints to 1 – PacketLoss Jan 19 '21 at 03:36
  • Imagine you replace the first word _"This"_ by _"I like chocolate cake. This"_; and then the first line exceeds 80 characters. – Culip Jan 19 '21 at 03:41
  • 1
    I'm honestly not sure what you're expecting. The string, if you change it in the code itself will always have to be modified manually to fit within the 80 char limit. You can make this easier by storing it in one variable thats easy to read using triple-quotes. If not, why not just store it in a file and read the text from there? – PacketLoss Jan 19 '21 at 03:42
  • @GinoMempin You meant, auto-formatting the script/code itself, rather than the output? – Culip Jan 19 '21 at 03:45
  • Yeah, I misunderstood. I was talking about formatting the code, but you are talking about *formatting the printed output*. It's still unrelated to the [tag:python] tag though. See related questions over at Unix & Linux: [Format output to a specific line length](https://unix.stackexchange.com/q/146089/370183) – Gino Mempin Jan 19 '21 at 03:50
  • @PacketLoss - This is what I know so far, i.e., I need to break lines within 80 characters _manually_ or store a text file somewhere else. For example, I made a simple Bash script file with only 100 lines, including 10 lines of a help text. To me it doesn't sound right that I prepare a dedicated help text file only for that particular script file.. – Culip Jan 19 '21 at 03:52
  • 1
    @GinoMempin You're right, I am talking about the code, not the output! Sorry my explanation in English wasn't very clear.. WRONG: You meant, auto-formatting the script/code itself, rather than the output? CORRECT: Are you sure you are talking about the script/code (which is what I'm talking about) not the output (which is _not_ what I am interested in--please assume that I understand regex, sed, awk, Python basics, etc.) – Culip Jan 19 '21 at 03:53

1 Answers1

0

So, basically, you have a shell script which is supposed to output a literal string, which is longer than 80 characters, but for aesthetic reason or easier editing, you don't want to extend the input line past 80 characters. Right?

The basic idea is that you write your string into several lines, but tell bash to ignore the line endings, and treat it as a single, long line. Example:

echo This output \
is really \
a single line

produces This output is really a single line.

You must make sure that the line ending immediately follows the backslash. There must not be any whitespace after the backslash. I strongly recommend to use for this task a texteditor which can show whitespaces, because this is a kind of error, which can be very nasty and difficult to debug.

user1934428
  • 19,864
  • 7
  • 42
  • 87