如何将输出重定向到多个变量?

时间:2018-06-29 08:20:25

标签: shell sh

我必须将以下shell输出的每一行写入一个变量:

/bin/grep abc /etc/passwd | /bin/cut -d : -f 1

最后,每个用户应该有一个变量。喜欢:

$user1 = abc
$user2 = abca
$user3 = abcb
...

脚本必须在sh上运行,所以我不能使用数组。我在想像这样的for循环:

for (int i = 0; i <= linecountOfOutput; i++)
   user + i = line i

这在shell中可能吗?还是有一种更简单的方法来吸引这些用户?

1 个答案:

答案 0 :(得分:0)

我不知道您将如何使用它,但是您可以使用eval:

txt=$(grep sys /etc/passwd | cut -d: -f1)
cnt=$(echo "$txt" | wc -l)
ite=$(seq 1 "$cnt")
for i in $ite; do
        line=$(echo "$txt" | sed "${i}q;d")
        eval "user$i=\"${line}\""
done
echo $user1
echo $user2
echo $user3
echo $user4