touch_avr 发表于 2010-3-6 23:38:03

linux shell脚本关于管道的疑问

#!/bin/sh
until who | grep “$1” > /dev/null
do
sleep 60
done
# now ring the bell and announce the expected user.
echo -e \\a
echo “**** $1 has just logged in ****”
exit 0

怎么理解这段代码,是先
until who
do
sleep 60
done
# now ring the bell and announce the expected user.
echo -e \\a
echo “**** $1 has just logged in ****”
exit 0
然后才 | grep “$1” > /dev/null
还是每次循环一次就| grep “$1” > /dev/null一次
“$1”是传递的第一个参数,| grep “$1” > /dev/null有什么用?

sunliezhi 发表于 2010-3-7 08:50:29

命令who是查看都有谁登录了, 全部显示在屏幕上;
who | grep X 是指定查看X有没有登录,有的话就显示X在屏幕上;
who | grep X > /dev/null是指定查看X有没有登录,有的话也不显示在屏幕上,即查看结果输出重定向到“无底黑洞”。

每循环一次就 who | grep “$1” > /dev/null一次, 直到 “$1”登录为止。

touch_avr 发表于 2010-3-7 09:19:58

谢谢,我还以为who是一个自己定义的变量
但是“$1”是谁给传递的参数进去?

sunliezhi 发表于 2010-3-7 10:21:13

假如你写的脚本文件名是 a.sh ,那么你运行脚本时就像执行函数一样要带参数(如有必要) : a.sh XiaoWang
要学shell的话最好还是买本关于shell编程的书吧,里面什么都有。 呵呵。

touch_avr 发表于 2010-3-7 10:26:53

我在看linux程序设计(英文版),这是里面的代码。
我现在要的是速成,留给我的时间不多了,谢谢!

sunliezhi 发表于 2010-3-7 10:33:45

3楼中的a.sh 后面的XiaoWang 就是代码中的参数$1
页: [1]
查看完整版本: linux shell脚本关于管道的疑问