标签: heredoc

if/else与heredoc不符合预期

我正在使用heredoc构建一个简单的文本电子邮件,但由于某种原因,我在if/else条件周围得到了奇怪的结果:

<?php 
$message = <<<EOD
Hi Username
EOD;

echo $message.'<hr>';

if(true) {
    $message .= <<<EOD
Thanks for logging in
EOD;                    
} else {
    $message .= <<<EOD
Thanks for signing up.  
EOD;
}       

echo $message.'<hr>';   

$message .= <<<EOD
Good Bye                    
EOD;
echo '<pre>'.$message.'</pre>';
Run Code Online (Sandbox Code Playgroud)

输出:

Hi UsernameThanks for logging in
EOD;                    
} else {
    Hi Username .= <<<EOD
Thanks for signing up.  Good Bye    
Run Code Online (Sandbox Code Playgroud)

由于某种原因,它输出我的PHP - 如果我将true更改为false,我只会得到Hi UsernameGood Bye更令人费解的.

php heredoc

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

为什么在Ruby中不使用"gsub"删除管道?

我想从notes一切开始删除example_header.我试着这样做:

example_header = <<-EXAMPLE
    -----------------
    ---| Example |---
    -----------------
EXAMPLE

notes = <<-HTML
    Hello World
    #{example_header}
    Example Here
HTML

puts notes.gsub(Regexp.new(example_header + ".*", Regexp::MULTILINE), "")
Run Code Online (Sandbox Code Playgroud)

但输出是:

    Hello World
    ||
Run Code Online (Sandbox Code Playgroud)

为什么||不删除?

ruby regex heredoc gsub

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

PHP heredoc不保留格式

使用以下代码,我希望看到显示5条单独的行.相反,它将所有内容包装在一条线上......没有标签,没有前导空格,没有空行.不是我所期待的.

在查看了其他代码之后,程序员插入了我尝试过的一系列代码(使用
代替)并得到了我正在寻找的一些结果.我期待太多了吗?我永远无法获得领先的空间和标签.这可能是一个设置问题吗?

<?php
$author = "Alfred E Newman";

$out = <<<_END
       This is a Headline

   This is the first line.
   This is the second.
      - Written by $author.
_END;

print $out;
// echo $out; // ... didn't format as expected either
?>
Run Code Online (Sandbox Code Playgroud)

结果:这是标题这是第一行.这是第二个. - 由Alfred E Newman撰写.

php format heredoc

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

<< DESC在红宝石中意味着什么?

我正在学习Ruby,在我使用的书中,有一个这样的示例代码

#...
restaurant = Restaurant.new
restaurant.name = "Mediterrano"

restaurant.description = <<DESC
One of the best Italian restaurants in the Kings Cross area, 
Mediterraneo will never leave you disappointed
DESC

#...
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释一下<< DESC在上面的例子中意味着什么吗?它与常见的字符串双引号有何不同?

ruby heredoc

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

无法访问heredoc中的数组

为什么此代码在我尝试访问的行上出错$sector_value['sector_info']['purchase_order'];?但不是在我打印变量的第五行$sector_counter

//SECTOR
$email_body .=  <<<SECTOR_DETAILS
    <tr>
        <td colspan="2">
            Sector $sector_counter;
        </td>
    </tr>

    <tr>
        <td colspan="2">
            Purchase order
        </td>
    </tr>
    <tr>
        <td colspan="2">
        $sector_value['sector_info']['purchase_order'];
        </td>
    </tr>

    <tr>
        <td>
            Proof
        </td>
    </tr>

SECTOR_DETAILS;
Run Code Online (Sandbox Code Playgroud)

这是错误:

解析错误:语法错误,意外T_ENCAPSED_AND_WHITESPACE,期待第44行/home/www2isco/public_html/test/bch/queries/submit_order.php中的T_STRING或T_VARIABLE或T_NUM_STRING

谢谢!

php heredoc

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

这里的JQuery-doc无法正常工作

是否可以在perl的here-doc部分中包含JQuery.我试过但没有成功.这是我的代码.

my $cgi = CGI->new();print header();print start_html("JQuery in perl");

print <<JS;
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script><!--
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
 });
});
//--></script>

<p>Click to hide</p>
<p>Click to hide</p>
<p>Click to hide</p>
</body>
JS
print end_html();
Run Code Online (Sandbox Code Playgroud)

但是当我编写一个javascript函数时,它工作正常

my $cgi = CGI->new();print header();print start_html("hello");
print <<JS;
<script><!--
function show() {alert("Hello");}
//--></script>
<input type="button" value="Check"  onclick="show()">
</body>
JS
print end_html();
Run Code Online (Sandbox Code Playgroud)

我错过了一些明显的东西吗?请建议.

javascript perl jquery cgi heredoc

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

如何将python程序插入bash脚本?

我有一个小的python程序,它解析文本文件并将输出写入另一个文件.目前,我正在写一个bash脚本来多次调用这个程序,它看起来像:

for i in $(seq 1 100); do

          python /home/Documents/myProgram.py myTextFile$i

done
Run Code Online (Sandbox Code Playgroud)

这工作正常,但我想知道是否有可能在bash脚本文件中包含python程序,所以当另一个用户运行脚本时,他们不需要在他们的内存中有python程序; 即,是否可以将python程序复制并粘贴到bash脚本中并从脚本本身运行?

python bash heredoc

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

在 bash 脚本中保持 SSH 开放

在任何人说使用公钥之前,这不是一个选项,因为我正在与客户服务器打交道。

我有一个脚本来维护一个文件,其中包含我客户服务器的所有 IP/用户名/密码。通过脚本,我选择了一个服务器并使用 here 文档来登录该服务器。

#### Code up here to set the variables.
exp_internal 1
/usr/bin/expect << EOFFF

if { $USE_TUNNEL == 1 } {
        spawn ssh -L 4567:localhost:3306 $user\@$ip
} else {
        spawn ssh $user\@$ip
}
expect {
    -re ".*es.*o.*" {
        exp_send "yes\r"
        exp_continue
    }
    -re ".*sword.*" {
        exp_send "$pass\r"
    }
}
expect {
    "$ " { send "<<<Send stuff>>>" }
    "# " { send "<<<Send stuff>>>" }
}
interact
EOFFF
# End of bash script
Run Code Online (Sandbox Code Playgroud)

我的问题是,在 here 文档结束后,它会关闭 …

ssh bash heredoc expect spawn

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

在heredoc中使用shell变量的双引号

我编写了一个bash脚本,并通过read获得了一些用户输入.我想把变量,我通过读取,放在一个文件中cat << EOF >> file.我的问题是每个变量都得到"双重报价".我怎么能阻止这个?

echo "Whats your name?"
read yourname

cat << EOF >> /path/to/file
Your Name is "${yourname}"

EOF
Run Code Online (Sandbox Code Playgroud)

该文件的内容是:

Your Name is "djgudb"
Run Code Online (Sandbox Code Playgroud)

它应该是:

Your Name is djgudb
Run Code Online (Sandbox Code Playgroud)

bash shell heredoc double-quotes

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

如何在 ssh pkill bash 命令中正确转义双引号?

$ ssh root@123.123.123.123
123.123.123.123# pkill -f "stalled process name"; commands_to_restart; some_more_commands;
many many lines of output demonstrating success
123.123.123.123# exit;
Run Code Online (Sandbox Code Playgroud)

一切正常

$ ssh root@123.123.123.123 "pkill -f "\""stalled process name"\"";"\
> "commands_to_restart; some_more_commands;";
Run Code Online (Sandbox Code Playgroud)

没有输出,什么都不做。

$ ssh root@123.123.123.123 "echo "\""pkill -f "\"\\\"\""stalled process name"\"\\\"\""; "\
> "commands_to_restart; some_more_commands;"\"";";
pkill -f "stalled process name"; commands_to_restart; some_more_commands;
Run Code Online (Sandbox Code Playgroud)

所以......报价转义的两个阶段按预期工作......

如何获得单层引号转义以使用 ssh/bash?由于引用在两层中完美运行,我觉得它与引用的关系不大,而与ssh终端处理的某些方面有关。然而,据我所知,这些命令只对标准输出进行简单和常规的 IO 操作,没有输入。

ssh bash escaping heredoc quoting

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

标签 统计

heredoc ×10

bash ×4

php ×3

ruby ×2

ssh ×2

cgi ×1

double-quotes ×1

escaping ×1

expect ×1

format ×1

gsub ×1

javascript ×1

jquery ×1

perl ×1

python ×1

quoting ×1

regex ×1

shell ×1

spawn ×1