1

I am trying to take the contents of a list list1 and loop through them comparing them to another list generated by finger. When a name from list1 is found in finger it should be removed from list1.

for i in $(cat list1); do
if finger | grep $i
    echo "$i is online"
    sed '/$i/d' <list1 >templist
    mv templist list1
fi
done

list1 does not change. Once the script has run, the contents of list1 are the same. I think the problem has to do with the cat at the beginning but I'm not sure.

Thanks, Ryan

4

4 回答 4

1

尝试双引号:

sed "/$i/d" <list1 >templist

(列表中实际上没有任何内容$i。)

于 2013-03-18T18:28:58.703 回答
1

根本不需要循环:

comm -23 <(sort list1) <(finger | sort) > tmpfile && 
mv list1 list1.bak &&
mv tmpfile list1

comm -23 file1 file2返回 file1 中未出现在 file2 中的行。file1 和 file2 必须排序。

于 2013-03-18T18:54:28.947 回答
0

如果list1不是很大(例如,最多几百个项目),您可以这样做:

finger | egrep -v "$(paste -sd'|' list1)" > templist && mv templist list1

这是如何工作的:paste将列表转换为管道分隔的列表,该列表使用$(...)构造内联到命令中,被 egrep 解释为正则表达式,并用-v标志取反以仅包含不匹配的行。

于 2013-03-18T18:29:58.187 回答
0

保持原作的精神,我可能会按照这些思路重写它,以避免重复的手指和列表中的其他事情。

finger > fingerlist
while read aline ; do
    if grep "$aline" fingerlist > /dev/null ; then
        echo "$aline is online"
    else
        echo "$aline" > templist
    fi
done < list1
[ -s templist ] && mv -f templist list1
rm fingerlist
于 2013-03-18T18:54:40.270 回答