管道无法在heredoc中的复杂shell命令中工作,它将传递给su执行

时间:2017-03-15 13:12:53

标签: shell

首先,我创建了一个名为test的目录,其中包含两个文件:

$ find ./test -type f
./test/111
./test/222

然后我将以下代码放在脚本中进行测试:

test-su.bash
-----------
#!/usr/bin/env bash

topdir=$(
cd -P -- "$(dirname -- "$0")" &&
pwd -P
)

cd $topdir

su -- root -p -c 'exec 0<&3 3<&- && "$SHELL" ${1+"$@"}' \
  su 3<<EOF
find ./test -type f |
while IFS= read -r line; do
  echo "the file under test dir is: $line"
done
EOF
-----------

然后我将上面的脚本放在test目录的父目录中 跑吧:

$ bash test-su.bash 
Password: 
the file under test dir is: 
the file under test dir is: 
^C
$

正如你所看到的,在后一种情况下,`read&#39; while循环中的命令 无法获得find的输出。

有关此问题的任何提示吗?

此致

0 个答案:

没有答案
相关问题