2

I have this multilinie heredocument which I desire to translate into a uniline herestring:

cat <<-"PHPCONF" > /etc/php/*/zz_overrides.ini
  [PHP]
  post_max_size = 200M
  upload_max_filesize = 200M
  cgi.fix_pathinfo = 0
PHPCONF

The closest I could get is as follows:

cat >"/etc/php/*/zz_overrides.ini" <<< "[PHP] post_max_size = 200M upload_max_filesize = 200M cgi.fix_pathinfo = 0"

but I don't think line breaks after each directive is possible, given the end result is one string. Maybe there is some "unorthodox" way after all?


Both the heredoc, and herestring, are aimed to replace this heavy sed operation:

sed -i "s/post_max_size = .M/post_max_size = 200M/ ; s/upload_max_filesize = .M/upload_max_filesize = 200M/ ; s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/" /etc/php/*/fpm/php.ini
7
  • 3
    Um, I may be misunderstanding here - but can't you just use printf? e.g. printf ' [PHP]\n post_max_size = 200M\n upload_max_filesize = 200M\n cgi.fix_pathinfo = 0\n' Commented Jan 27, 2018 at 1:31
  • I used heredocument/herestring to create the file on the way, but touch + && may be handy, because AFAIK, one cannot create a file with printf. Commented Jan 27, 2018 at 3:40
  • 1
    Huh? you can redirect the printf output to a file (which will create it if it doesn't exist) Commented Jan 27, 2018 at 3:43
  • Oh, sure, I didn't think of that, I'm quite new to redirections and I need to internalize this concept better. Thanks! Commented Jan 27, 2018 at 3:44
  • Note that cat doesn't create files either. The redirection that you use creates the file. Commented Jan 27, 2018 at 5:25

2 Answers 2

4

Leaving aside the fact that you could just printf "this\nthat\n" > /some/file, you can make a here-string with newlines with e.g. ANSI-C quoting:

$ cat <<< $'this\nthat'
this
that

Also, because of the quotes, this will try to create a file in a directory literally named *:

cat > "/etc/php/*/zz_overrides.ini"

This would work in Bash, but only if the glob matches one file (if you have /etc/php/foo/zz_overrides.ini and /etc/php/bar/zz_overrides.ini, Bash gives an error)

cat > /etc/php/*/zz_overrides.ini
2

I think bash allow you to use a string with newlines directly, no need for a heredoc, or calling external commands (like cat):

$ php_conf='
  [PHP]
  post_max_size = 200M
  upload_max_filesize = 200M
  cgi.fix_pathinfo = 0
'

$ echo "$php_conf"

 [PHP]
 post_max_size = 200M
 upload_max_filesize = 200M
 cgi.fix_pathinfo = 0

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.