I'm searching for a possibility to send a mixed PHP multline string with the help of proc_open (exec, passthru or whatever) via a pipe to a bash script. Finally, within this script I want to grab the mixed multiline string and store it into a variable.
// static way
$some_multiline_string = 'some $%§%& mixed &/(/(
content
with newlines';
// dynamic way:
// the mixed content is coming from the database
// so actually it is not initialized like in the previous lines, but more like this:
$some_multiline_string = $db_result['some_multiline_string'];
// escaping
$some_multiline_string = escapeshellargs($some_multiline_string);
// execution
$process = proc_open("printf $some_multiline_string | some_script.sh args");
...
#!/bin/bash
mixed_multiline_string=$(</dev/stdin)
echo -e "$mixed_multiline_string"
...
How to escape the mixed content properly before using it within the command? I've tried already escapeshellargs and escapeshellcmd, but either there is an unescaped charackter, which is stopping the process or it's working but is taking too much time (1.5 min) for processing.
Here is a link to an example mixed content string: http://playmobox.com/js/test.txt
Thanks a lot!
I don't know PHP but bash should do something like one post of the top-related on the right side:
#!/usr/bin/env bash
declare line ; line=
declare -a line_ ; line_=()
while IFS= read -r line ; do
line_+=( "${line}" )
done < /dev/stdin
printf "%s\n" "${line_[@]}"
Assumed the script's name is some_script.sh
you can do
% echo '&Ω↑ẞÐĦØđ¢ø' | bash some_script.sh
&Ω↑ẞÐĦØđ¢ø
while IFS= read -r line
is explained here: Bash, read line by line from file, with IFS
and on the bash wiki
- IFS is set to the empty string to prevent read from stripping leading and trailing whitespace from each line. – Richard Hansen
- -r raw input - disables interpretion of backslash escapes and line-continuation in the read data