繁体   English   中英

如何在bash中间接分配变量以从Standard In,File和执行输出中获取多行数据

[英]How do I indirectly assign a variable in bash to take multi-line data from both Standard In, a File, and the output of execution

我在这里和其他地方发现了许多片段,可以回答部分问题。 我什至设法以低效的方式分多个步骤进行了此操作。 如果可能的话, 我真的很想找到可以执行此任务的单行执行 ,而不必分配给变量并复制几次以执行任务。

例如

executeToVar ()
{
    # Takes Arg1: NAME OF VARIABLE TO STORE IN
    # All Remaining Arguments Are Executed
    local STORE_INvar="${1}" ; shift
    eval ${STORE_INvar}=\""$( "$@" 2>&1 )"\"
}

总体上是可行的,即$ executeToVar SOME_VAR ls -l * #实际上SOME_VAR使用从其余参数中获取的ls -l *命令的执行输出来填充SOME_VAR 但是,如果命令要在末尾输出空行,(例如- echo -e -n '\\n\\n123\\n456\\n789\\n\\n'在开始和结束处应该有2 x新行)会被bash的子执行过程剥离。 我在类似的其他帖子中看到,已通过在流的末尾添加标记'x'来解决此问题,例如将子执行变成类似以下内容:

eval ${STORE_INvar}=\""$( "$@" 2>&1 ; echo -n x )"\" # <-- ( Add echo -n x )
# and then if it wasn't an indirect reference to a var:
STORE_INvar=${STORE_INvar%x} 
# However no matter how much I play with:
eval "${STORE_INvar}"=\""${STORE_INvar%x}"\"  
# I am unable to indirectly remove the x from the end.

无论如何,我还需要2个其他变体,一个将STDIN stream分配给var,另一个将文件内容分配给var,我认为这是涉及$( cat ${1} ) ,或者$( cat ${1:--} )给我一个'-'(如果没有文件名)。 但是,所有这些都将无法正常工作,直到我可以确定需要删除的x以确保准确分配多行变量为止。

我也尝试过(但无济于事):

IFS='' read -d '' "${STORE_INvar}" <<<"$( $@ ; echo -n x )"
eval \"'${STORE_INvar}=${!STORE_INvar%x}'\"

这接近最佳值,但会降低eval

executeToVar() { local varName=$1; shift; printf -v "$1" %s "$("$@")"; }

该公式仍然存在的一个问题是$()删除尾随的换行符。 如果要防止这种情况,则需要在子外壳中添加自己的尾随字符,然后自己删除它。

executeToVar() {
  local varName=$1; shift;
  local val="$(printf %s x; "$@"; printf %s x)"; val=${val#x}
  printf -v "$varName" %s "${val%x}"
}

如果要将所有内容从stdin读取到变量中,这特别容易:

# This requires bash 4.1 for automatic fd allocation
readToVar() {
  if [[ $2 && $2 != "-" ]]; then
    exec {read_in_fd}<"$2"              # copy from named file
  else
    exec {read_in_fd}<&0                # copy from stdin
  fi
  IFS= read -r -d '' "$1" <&$read_in_fd # read from the FD
  exec {read_in_fd}<&-                  # close that FD
}

...用作:

readToVar var < <( : "run something here to read its output byte-for-byte" )

...要么...

readToVar var filename

测试这些:

bash3-3.2$ executeToVar var printf '\n\n123\n456\n789\n\n'
bash3-3.2$ declare -p var
declare -- var="

123
456
789

"

...和...

bash4-4.3$ readToVar var2 < <(printf '\n\n123\n456\n789\n\n')
bash4-4.3$ declare -p var2
declare -- var2="

123
456
789

"

存储在文件中怎么了:

   $ stuffToFile filename $(stuff)

其中“ stuffToFile”测试一个。 > 1个参数,b。 在管道上输入

   $ ... commands ... | stuffToFile filename   

   $ stuffToFile filename < another_file

其中“ stoffToFile”是一个函数:

function stuffToFile
{
    [[ -f $1 ]] || { echo $1 is not a file; return 1; }

    [[ $# -lt 2 ]] && { cat - > $1; return; }

    echo "$*" > $1
}

因此,如果“材料”具有空白行的开头和结尾,那么您必须:

   $ stuff | stuffToFile filename

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM