如何通过管道将混合字符串从 php 传递到 bash 脚本

Howto pass a mixed string from php through a pipe to a bash script

我正在寻找在 proc_open(exec、passthru 或其他)的帮助下通过管道将混合的 PHP 多行字符串发送到 bash 脚本的可能性.最后,在此脚本中,我想获取混合的多行字符串并将其存储到一个变量中。

PHP:

// 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");
...

Bash:

#!/bin/bash
mixed_multiline_string=$(</dev/stdin)
echo -e "$mixed_multiline_string"
...

如何在命令中使用之前正确转义混合内容?我已经尝试过 escapeshellargs 和 escapeshellcmd,但是要么有一个未转义的 charackter,它正在停止进程,要么它正在工作但处理时间太长(1.5 分钟)。

这是一个 link 示例混合内容字符串: http://playmobox.com/js/test.txt

非常感谢!

我不知道 PHP 但 bash 应该像 one post 右侧顶部相关的那样做:

#!/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_[@]}"

假设脚本的名字是some_script.sh你可以

% echo '&Ω↑ẞÐĦØđ¢ø' | bash some_script.sh
&Ω↑ẞÐĦØđ¢ø

while IFS= read -r line 解释如下:Bash, read line by line from file, with IFSbash 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