如何从文件向Bash脚本传入参数?以dumphfdl工具应用为例
问题
我用多台接收器监控HFDL流量,想给每台接收器编写启动脚本。dumphfdl工具需要配置诸多参数,除固定参数外,监控频率以一组参数形式传入。请问能否将这些频率写入文本文件,再传入脚本中?
示例命令行(直接传入频率参数):
dumphfdl --soapysdr driver=sdrplay --freq-as-squawk \ --sample-rate 2000000 --system-table /home/systable.conf \ --system-table-save /home/systable.conf \ 2941 2944 2992 2998 3007 3016 3455 3497 3900 4654 4660 4681 4687 \ --output decoded:basestation:tcp:address=127.0.0.1,port=60011
期望命令行形式(从文件读取频率):
dumphfdl --soapysdr driver=sdrplay --freq-as-squawk \ --sample-rate 2000000 --system-table /home/systable.conf \ --system-table-save /home/systable.conf 02M-04M.txt \ --output decoded:basestation:tcp:address=127.0.0.1,port=60011
解决方案
dumphfdl本身不支持直接传入频率文件作为参数,但可以通过Shell的命令替换或xargs工具实现从文件读取频率的需求,以下是几种可行方法:
方法1:使用命令替换
先将频率文件02M-04M.txt中的频率整理为空格、换行或制表符分隔的格式(HFDL频率均为数字,无需特殊处理),然后通过$()读取文件内容并作为参数传入:
dumphfdl --soapysdr driver=sdrplay --freq-as-squawk \ --sample-rate 2000000 --system-table /home/systable.conf \ --system-table-save /home/systable.conf \ $(cat 02M-04M.txt) \ --output decoded:basestation:tcp:address=127.0.0.1,port=60011
方法2:使用xargs传递参数
如果需要更灵活的参数处理逻辑,可借助xargs工具传递频率参数:
cat 02M-04M.txt | xargs dumphfdl --soapysdr driver=sdrplay --freq-as-squawk \ --sample-rate 2000000 --system-table /home/systable.conf \ --system-table-save /home/systable.conf \ --output decoded:basestation:tcp:address=127.0.0.1,port=60011
方法3:封装为启动脚本
为每台接收器创建独立的启动脚本(比如sdr-hfdl-01.sh),方便批量管理和参数修改:
#!/bin/bash # 接收器1的HFDL监控启动脚本 FREQ_FILE="/path/to/02M-04M.txt" OUTPUT_PORT="60011" dumphfdl --soapysdr driver=sdrplay --freq-as-squawk \ --sample-rate 2000000 --system-table /home/systable.conf \ --system-table-save /home/systable.conf \ $(cat "$FREQ_FILE") \ --output decoded:basestation:tcp:address=127.0.0.1,port=$OUTPUT_PORT
给脚本添加执行权限:
chmod +x sdr-hfdl-01.sh
后续只需修改脚本中的FREQ_FILE和OUTPUT_PORT变量,即可适配不同接收器的配置需求。
内容的提问来源于stack exchange,提问作者Faramir
相关产品推荐
相关产品推荐

