Use xargs input in eval command

5,160

Solution 1

Figured out a way:

cat commands.php | xargs -L 1 -I {} sh -c "{}"

runs each command out of shell... simple enough!

Solution 2

If your PHP file outputs shell commands, you could probably get away with

$( php /path/to/your/script.php )

Or

while read line; do
    $line
done < $( php /path/to/your/script.php )
Share:
5,160
d-_-b
Author by

d-_-b

(your about me is currently blank)

Updated on September 18, 2022

Comments

  • d-_-b
    d-_-b over 1 year

    I have a file "commands.php" that dynamically generates bash commands. For example, one line is:

    # - commands.php
    ssh [email protected] echo "text" > file; reboot;
    

    How can I use the output of xargs to execute these commands... eval, maybe?

    cat commands.php | xargs -L 1 -I {} 'eval "$({})"';
    
    • phemmer
      phemmer over 10 years
      Sounds like XY problem. Why would you want to do this? What's wrong with bash /path/to/file?
    • d-_-b
      d-_-b over 10 years
      Hi Patrick, very well could be! I have a file that dynamically generates bash commands, and it is not a .sh file. (I could copy the output to an sh file, but am interested in how to execute each line of a non-sh file
    • mattdm
      mattdm over 10 years
      Everything else aside, is this php script actually. Allen from the web? This seems very risky.
    • d-_-b
      d-_-b over 10 years
      @mattdm haha good catch! but yes this is a very secure environment, not some public external file.
  • Jeff Hewitt
    Jeff Hewitt over 10 years
    Why do you insist on cating the file? A simple redirection would be enough: xargs ... <commands.php
  • Ole Tange
    Ole Tange over 10 years
    And if you want to do it in parallel, use GNU Parallel: cat commands.php | parallel