需要定期查询5G模组当前的PLMN和CellID,写了一个shell脚本,测试通过,跟大家分享下
#!/bin/bash
#指定串口设备
serial_device="/dev/ttyUSB3"
#指定输出文件
output_file="/tmp/output"
#发送AT指令
send_command() {
printf "AT+QENG=\"servingcell\"\r\n" > $serial_device
}
#读取返回数据
read_serial() {
#输出当前时间
echo -n $(date "+%Y-%m-%d-%H:%M:%S") >> $output_file
#提取plmn和cellid,不需要提取直接打印结果可用cat $serial_device >> $output_file &
cat $serial_device | head -n 2 | tail -n 1 | awk -F',' '{printf " %s%s %s\n", $5,$6,$7}' >> $output_file &
}
#关闭阻塞进程cat
close_serial() {
kill `ps | grep 'cat' | head -n 1 | awk -F' ' '{printf "%s", $1}'`
}
main() {
while true; do
send_command
read_serial
close_serial
#延时,单位秒
sleep 1
done
}
main