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 )
Comments
-
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 over 10 yearsSounds like XY problem. Why would you want to do this? What's wrong with
bash /path/to/file
? -
d-_-b over 10 yearsHi 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 over 10 yearsEverything else aside, is this php script actually. Allen from the web? This seems very risky.
-
d-_-b over 10 years@mattdm haha good catch! but yes this is a very secure environment, not some public external file.
-
-
Jeff Hewitt over 10 yearsWhy do you insist on
cat
ing the file? A simple redirection would be enough:xargs ... <commands.php
-
Ole Tange over 10 yearsAnd if you want to do it in parallel, use GNU Parallel: cat commands.php | parallel