标签: heredoc

PHP 中 html 的 Heredoc

Heredoc 不适用于以下代码

    $html = <<<HTML 
             <video width="$width" height="$height" controls preload autoplay >
             <source src="$video_url_direct" type="video/mp4" /> 
    <object id="flowplayer" width="$width" height="$height" data="$player_url" type="application/x-shockwave-flash">

        <param name="allowfullscreen" value="true" />
        <param name="wmode" value="transparent" />
        <param name="flashvars" value='config={"clip":"$video_url", "plugins": {"controls": {"autoHide" : false} }}' />

    </object></video>
HTML;
Run Code Online (Sandbox Code Playgroud)

我也可以使用heredoc作为flashvars值(即另一个heredoc内的heredoc)。

php heredoc

1
推荐指数
1
解决办法
5307
查看次数

如何让 EOT 在 bash 中的 IF ELSE 语句上工作?

我正在尝试制作一个脚本,如果 /etc/environment 文件当前为空,则启用代理设置;如果文件有文本,则禁用设置。我已经编写了一些代码,但不确定为什么 /etc/environment 文件没有被编辑。我已经清空了我正在使用的实际代理。任何帮助将不胜感激!

#!/bin/bash

        if [ -s /etc/environment ]
        then
            cat<<EOT >> /etc/environment
            http_proxy="blank"
            https_proxy="blank"
            ftp_proxy="blank"
            export http_proxy https_proxy ftp_proxy
            EOT
        else
            > /etc/environment
        fi
Run Code Online (Sandbox Code Playgroud)

bash proxy heredoc environment-variables eof

1
推荐指数
1
解决办法
2679
查看次数

在函数内部使用 heredoc 会更改其下方代码的语法突出显示

<<<heredoc heredoc;在函数内部(在类中)使用它,它弄乱了其下面所有代码的语法突出显示。

可以在函数外部使用它,或者在函数内的一行上使用它:

php

但是如果我在函数中使用它(而不是在一行上),它会弄乱其下面的突出显示,并且我的编辑器(在 Atom 或 Sublime Text 中相同)似乎认为它与函数和类之外的函数和类一起关闭。发生了什么?

php

<?php

class SimpleCMS {
    var $host = 'localhost';
    var $username = 'root';
    var $password = '';
    var $table = '';

    public function display_public() {

    }

    public function display_admin() {
        return <<<ADMIN_FORM 
        ADMIN_FORM;
    }

    public function write() {

    }

    public function connect() {
        mysql_connect($this->host, $this->username, $this->password) or die('Could not connect to the database. ' . mysql_error());
        mysql_select_db($this->table) or die('Could not select database. ' . mysql_error())

        // build the database
        return …
Run Code Online (Sandbox Code Playgroud)

php heredoc

1
推荐指数
1
解决办法
272
查看次数

案例内的heredoc有问题。bash脚本

如果在我的终端中我写

cat <<-EOF
hello
EOF
Run Code Online (Sandbox Code Playgroud)

我得到了预期的输出,你好。

现在,在我正在写的脚本中

PARAMS=""
while (( "$#" )); do
  case "$1" in
    -h|--help)
      cat <<-EOF
      hello
      EOF
      exit 0  
      ;;
    --) # end argument parsing
      shift
      ...
Run Code Online (Sandbox Code Playgroud)

但是 vscode 会突出显示该行之后的所有内容,cat<<-EOF就好像它都是一个字符串一样,基本上忽略了 EOF。事实上,当我运行脚本时,我得到了

syntax error: unexpected end of file
Run Code Online (Sandbox Code Playgroud)

错误

编辑:

如果我像这样缩进代码:

while (( "$#" )); do
  case "$1" in
    -h|--help)
      cat <<EOF
      ciao
EOF
      exit 0  
      ;;
    --) # end argument parsing
      shift
      ...
Run Code Online (Sandbox Code Playgroud)

EOF 位于左侧,vscode 会按照应有的方式识别它,将文件的其余部分突出显示为正常的 bash 脚本,一切正常。但就缩进而言,这很糟糕。有没有办法用 cat 命令缩进 EOF?

bash shell heredoc

1
推荐指数
1
解决办法
1488
查看次数

Ruby:方法中是否可以有一个heredoc?

我想在方法中放置一个 heredoc,以便在调用该方法时将其显示为 cli 工具的帮助消息。但是,我不断收到“在 EOF 之前的任何地方都找不到字符串 'error_string'”的消息。

我认为这是因为它在方法内部,在类内部,并且终止符需要自己的行,而在方法/类中缩进时则不需要。最好我希望在方法或最坏的类中定义帮助消息,这是可能的还是唯一的方法来在文件中的其他所有内容之外定义它(作为全局变量)并在方法中调用它?

为简洁起见,我的代码如下。

class TodoTool

  def help
    puts <<USAGE
    Usage:
    - Create log: todo_list create <task log title> <task title> <task content>
    - View logs and tasks: todo_list view
    - Add task: todo_list add <log to add to> <task title to add> <task content to add>
    - Remove task: todo_list remove <log to remove from> <task to remove>
    USAGE
  end

end
Run Code Online (Sandbox Code Playgroud)

ruby methods syntax class heredoc

1
推荐指数
1
解决办法
43
查看次数

在 Bash 与 ZSH 中结合 heredoc 和输入重定向

以下代码在 ZSH 上运行没有问题,将 heredoc 与文件 test.csv 的内容相结合:

cat <<EOF <test.csv
id,name,age
EOF
Run Code Online (Sandbox Code Playgroud)

如何在 Bash 中编写相同的命令?

bash zsh heredoc cat io-redirection

1
推荐指数
2
解决办法
40
查看次数

这里的文档如何在 shell 中工作

在进行输入重定向时,shell 使用文件描述符 0 打开“<”右侧的文件,但在定界文档的情况下,没有这样的文件可以打开,我想知道 shell 在这种情况下到底做了什么。

cat > file.txt << EOF
line1
line2
EOF
Run Code Online (Sandbox Code Playgroud)

bash shell heredoc

1
推荐指数
2
解决办法
1855
查看次数

shell脚本中的行错误

我在shell脚本中有以下代码.这似乎只当它工作不是在一个函数定义.有问题的行是包含"<<"的行.错误消息是

"./run:第210行:语法错误:意外的文件结束"

如何在函数中正确写入?

init_database()
{
    cd ../cfg
    db.sh << ENDC
    $DB_ADMIN

    0
    y
    n
    ENDC

    check_status

    sqlplus $DB_SCHEMA@$DB_NAME < initial_data.sql

    cd -
}
Run Code Online (Sandbox Code Playgroud)

shell heredoc indentation

0
推荐指数
1
解决办法
867
查看次数

如何执行在此使用doc的所有脚本参数?

我有一个简单的脚本,称为,可在我的整个代码库中使用sshpass

#!/bin/bash

expect << EOF
spawn $@
expect {
    "*assword" {
        send "$SSHPASS\n"
    }
}
expect eof
EOF
Run Code Online (Sandbox Code Playgroud)

我目前使用此脚本的方式是这样的-

./sshpass scp archive.tgz $SERVER:$DIR
Run Code Online (Sandbox Code Playgroud)

当SSH命令为单行代码时,这非常有效。我的问题是,当我尝试sshpass使用此处文档执行命令时。

./sshpass ssh $user@$server /bin/bash << EOF
    echo "do this..."
    echo "do that..."
    echo "and the other..."
EOF
Run Code Online (Sandbox Code Playgroud)

上面的失败是因为$@仅解析了ssh $user@$server /bin/bash

关于我如何处理SSH身份验证,请不要发表评论。当被迫使用Cygwin时,有些特定的事情(例如密钥身份验证和管理特权)根本无法工作。

bash shell heredoc

0
推荐指数
1
解决办法
1539
查看次数

这里的托架操作员 - 作为参数记录?

我看到一个here-document用作参数,里面有方括号运算符.它看起来像:

method(<<EOF)[0][0]
lots of text
EOF
Run Code Online (Sandbox Code Playgroud)

方括号运算符的含义可能是什么?有谁知道那是什么样的成语?

ruby heredoc

0
推荐指数
1
解决办法
71
查看次数