2

I'm writing a bash shell script whose purpose is to create a php skeleton for new projects.

To create certain php documents within the newly created directory structure, I'm using HEREDOCs with tons of codelines..

sudo tee $projectname/www/index.php <<- EOF | > null
<?php           

ob_start();

require_once 'inc/header.inc';

ob_end_flush();
?>
EOF

## Create header.inc
sudo tee $projectname/www/inc/header.inc <<- EOF 1>&2

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
....
EOF

The problem is: All HEREDOC lines are echoed to the screen. That is really not what I want, it looks very messy. So, I tried to issue this, by redirecting output to nulland /dev/null. Unfortunately without success.

Research:

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
kzpm
  • 133
  • 11

1 Answers1

4

Your redirect syntax is incorrect. Lose the |

sudo tee $projectname/www/index.php <<-EOF >/dev/null

The other redirection attempt you have 1>&2 would merely redirect standard output to standard error, which (normally) lands on your screen in the end anyway. It's useful, but not for you are trying to accomplish.

It would be better hygiene to create the project as yourself and use a separate (version control and) deployment infrastructure to publish a production version only when you have something of suitable quality and completeness, though. Then you don't need sudo, and then you don't need tee.

cat <<-EOF >$projectname/www/index.php

The sudo tee file >/dev/null trick is a bit of an antipattern to enable you to write files like with cat when you are using sudo.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • @Etan Reisner Thanks for your suggestions! The <<-EOF >/dev/null part solved the output problem! – kzpm Dec 29 '14 at 14:19
  • FWIW, I prefer `sudo dd of=/path/to/file <<-EOF` to the `sudo tee` hack, but obviously avoiding the sudo in the first place is better. – rici Dec 29 '14 at 14:24
  • 2
    @kzpm: If the webserver can execute a sudo, then you might as well just give it write permissions to a directory; obviously, it has them if it needs them. Better solutions exist but that would be a different question, and arguably better asked on a sysadmin QA site. Anyway, I still prefer `sudo dd` to `sudo tee` (because you don't have to deal with discarding the output), but tastes vary. – rici Dec 29 '14 at 14:38
  • @rici Thanks for your observation rici, I will try 'dd'. Never thought one could use dd also for this specific purpose. – kzpm Dec 30 '14 at 00:26