阻止屏幕输出文档


Prevent screen output heredoc

我正在编写一个bash shell脚本,其目的是为新项目创建一个php框架。

为了在新创建的目录结构中创建某些php文档,我使用了带有大量代码行的HEREDOC。

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

问题是:所有 HEREDOC 行都会回显到屏幕上。那真的不是我想要的,看起来很乱。因此,我试图通过将输出重定向到null/dev/null来发布此问题。不幸的是没有成功。

研究:

  • 阻止批处理中的命令输出
  • Echo in heredoc/nowdoc syntax
  • 在赫雷多克中输出变量
  • http://wiki.bash-hackers.org/syntax/redirection

您的重定向语法不正确。 失去|

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

1>&2的另一个重定向尝试只会将标准输出重定向到标准错误,该错误(通常)最终都会落在您的屏幕上。 它很有用,但不适合您尝试完成。

不过,最好以

自己的身份创建项目,并使用单独的(版本控制和)部署基础结构来发布生产版本,前提是您具有合适的质量和完整性。 那么你不需要sudo,然后你就不需要tee.

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

sudo tee file >/dev/null技巧是一种反模式,使您能够在使用sudo时像cat一样编写文件。