#!/bin/bash
while :
do
if lsof -i :4444 | grep ESTABLISHED ;
then
paplay Alarm_Buzzer.ogg
fi
done我正在尝试创建一个脚本来连续检查端口的状态。如果状态发生变化,脚本应该播放声音(一次)并继续检查更改。
港口有两个州:LISTEN和ESTABLISHED
发布于 2020-03-18 13:56:15
澄清后;
获得当前端口state
last,则播放声音(如果状态为changed)
#!/bin/bash
# Port state at begin of script
# The awk part parses the string to only the last column containing;
# (LISTEN) OR (ESTABLISHED)
last=$(lsof -i :4444 | awk '{print $NF}')
# Continuously
while true; do
# Get fresh port state
state=$(lsof -i :4444 | awk '{print $NF}')
# If it changed
if [ "$state" != "$last" ]; then
# Play sound
paplay Alarm_Buzzer.ogg
# Remember the new state
last="$state"
fi
# No need to check so fast, sleep for a while (Thx @tomgalpin)
sleep 1
donehttps://stackoverflow.com/questions/60740924
复制相似问题