here-document gives 'unexpected end of file' error

后端 未结 7 2446
生来不讨喜
生来不讨喜 2020-11-22 04:42

I need my script to send an email from terminal. Based on what I\'ve seen here and many other places online, I formatted it like this:



        
7条回答
  •  无人及你
    2020-11-22 05:28

    Here is a flexible way to do deal with multiple indented lines without using heredoc.

      echo 'Hello!'
      sed -e 's:^\s*::' < <(echo '
        Some indented text here.
        Some indented text here.
      ')
      if [[ true ]]; then
        sed -e 's:^\s\{4,4\}::' < <(echo '
          Some indented text here.
            Some extra indented text here.
          Some indented text here.
        ')
      fi
    

    Some notes on this solution:

    • if the content is expected to have simple quotes, either escape them using \ or replace the string delimiters with double quotes. In the latter case, be careful that construction like $(command) will be interpreted. If the string contains both simple and double quotes, you'll have to escape at least of kind.
    • the given example print a trailing empty line, there are numerous way to get rid of it, not included here to keep the proposal to a minimum clutter
    • the flexibility comes from the ease with which you can control how much leading space should stay or go, provided that you know some sed REGEXP of course.

提交回复
热议问题