我是PHP/SQL的新手,我试图在heredoc中使用变量,因为我需要输入大量文本.我只包括第一句话,因为它足以显示问题).
我的问题是在heredoc中,变量(见下文:$data['game_name]和$data['game_owner'])不被识别为变量而是纯文本.我怎么解决这个问题?
<?php
try
{
//i am connecting the the database base mysql 'test'
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$bdd = new PDO('mysql:host=localhost;dbname=test', 'root', '', $pdo_options);
//the i read the data in the databse 'video_dame'
$response = $bdd->query('SELECT * FROM video_game');
//pour qu'elle soit visible à l'écran, on affiche chaque entrée une à une
while ($data= $response->fetch())
{
echo <<<'EX'
<p>Game: $data['game_name]<br/>
the owner of the game is $data['game_owner']
</p>
EX;
}
//i end the sql request
$response->closeCursor(); …Run Code Online (Sandbox Code Playgroud) 如果我有方法
def some_method p = {}
string = <<-MY_TERMINATOR
Example text blah blah
lorem ipsum something or another
MY_TERMINATOR
end
Run Code Online (Sandbox Code Playgroud)
如何从heredoc中访问变量p [:name]?
有没有一种方法可以批量指定多行字符串,类似于unix shell中的heredoc.类似的东西:
cat <<EOF > out.txt
bla
bla
..
EOF
Run Code Online (Sandbox Code Playgroud)
我们的想法是从模板文件创建自定义文件.
对于我所从事的个人开发和项目,我们使用四个空格而不是制表符.但是,我需要使用heredoc,我不能在不打破缩进流的情况下这样做.
我能想到的唯一可行的方法是:
usage() {
cat << ' EOF' | sed -e 's/^ //';
Hello, this is a cool program.
This should get unindented.
This code should stay indented:
something() {
echo It works, yo!;
}
That's all.
EOF
}
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?
如果这属于Unix/Linux Stack Exchange,请告诉我.
在bash中,我可以根据本网站创建一个带有here-doc的脚本:http: //tldp.org/LDP/abs/html/abs-guide.html#GENERATESCRIPT
(
cat <<'EOF'
#!/bin/bash
#? [ ] / \ = + < > : ; " , * |
#/ ? < > \ : * | ”
#Filename="z:"${$winFn//\//\\}
echo "This is a generated shell script."
App='eval wine "C:\Program Files\foxit\Foxit Reader.exe" "'$winFn'"'
$App
EOF
) > $OUTFILE
Run Code Online (Sandbox Code Playgroud)
如果我$OUTFILE是一个需要sudo权限的目录,我在哪里放置sudo命令或我还能做些什么来使其工作?
是否可以将此文档作为bash函数参数传递,并且在函数中将参数保留为多行变量?
以下内容:
function printArgs {
echo arg1="$1"
echo -n arg2=
cat <<EOF
$2
EOF
}
printArgs 17 <<EOF
18
19
EOF
Run Code Online (Sandbox Code Playgroud)
或者可能:
printArgs 17 $(cat <<EOF
18
19
EOF)
Run Code Online (Sandbox Code Playgroud)
我有一个here文档,我想将ssh作为执行命令提供给ssh会话,并从bash函数调用ssh会话.
here-document的定义如下:http: //en.wikipedia.org/wiki/Here_document
如何在here-document中键入选项卡?比如这样:
cat > prices.txt << EOF
coffee\t$1.50
tea\t$1.50
burger\t$5.00
EOF
Run Code Online (Sandbox Code Playgroud)
更新:
这个问题涉及的问题:
我试图理解为什么Bash在使用${parameter:+word}(使用备用值)进行变量扩展时,在here-document中删除双引号(但不是单引号),例如:
% var=1
% cat <<EOF
> ${var:+"Hi there"}
> ${var:+'Bye'}
> EOF
Hi there
'Bye'
Run Code Online (Sandbox Code Playgroud)
根据手册,后面的"单词" :+用波浪扩展,参数扩展,命令替换和算术扩展处理.这些都不应该做任何事情.
我错过了什么?如何在扩展中获得双引号?
在python中,我可以构造一个HTML字符串,而不必担心通过简单地将字符串括在三个引号中来转义像<或"这样的特殊字符,如:
html_string = """
<html>
<body>
<p>My text with "quotes" and whatnot!<p>
</body>
</html>
"""
Run Code Online (Sandbox Code Playgroud)
在Java中有类似的方法吗?
有没有一种方便的方法来引用在JavaScript中同时包含单引号和双引号的大块HTML?
有什么像HERE-doc <<EOF,多引号字符"""或自定义分隔符q{}?
这个问题的任何创造性或创造性解决方案