2011-10-15
awk中使用shell命令的方法:
方法1:system
方法2:getline (获取shell命令结果的第一行输出)
[1] system
#!/bin/sh
echo "gerry" | awk '{ system("date") }' | awk -F " " '
BEGIN{
print "begin"
}
{
#operation
# save time
time=$4
print time
}
END{
print "end"
}
'
[2] getline(获取shell命令结果的第一行输出)
#!/bin/sh
echo "gerry" | awk '{ "date" | getline tm; print tm }' | awk -F " " '
BEGIN{
print "begin"
}
{
#operation
# save time
time=$4
print time
}
END{
print "end"
}
'
[3] 动态获取时间
# write_time.sh
#!/bin/sh
while [ 1 ]; do
date >> outfile
sleep 1
done
# dynamic_read.sh
#!/bin/sh
tail -f outfile | awk -F " " '
BEGIN{
print "begin"
}
{
#operation
# save time
time=$4
print time
}
END{
print "end"
}
'
测试:
./write_time.sh &
./dynamic_read.sh
获取awk返回的结果
g_val=$( date | awk -F " " '{ print $4 }' ); echo $g_val
或者
echo ${g_val=$( date | awk -F " " '{ print $4 }' )}