shell使用文件通配符{},变量自动添加单引号,无法识别问题

脚本关键部分如下:

获取一周七天的日期,然后利用通配符,直接查看七天内的文件

weekdays=`/opt/uploadcheck/calculateDate.sh $start_week $end_week`
weekdays=`echo $weekdays | awk '{ gsub(/ /,","); print $0 }'`
echo $weekdays

zcat /opt/a_"{$weekdays}"_00_001.csv.gz

执行出现错误,通配符没有被解析,而是被当作整体处理:

cat: /opt/a_{20230618,20230619,20230620,20230621,20230622,20230623,20230624}_00_001.csv.gz: No such file or directory

通过sh -x查看分析,原因为shell自动为该变量添加单引号,导致无法解析

修改如下,可执行,先获取实际的文件名:

weekdays=`/opt/uploadcheck/calculateDate.sh $start_week $end_week`
weekdays=`echo $weekdays | awk '{ gsub(/ /,","); print $0 }'`
echo $weekdays

weekfiles=`eval ls /opt/a_"{$weekdays}"_00_001.csv.gz`

zcat /opt/a_"{$weekfiles}"_00_001.csv.gz

参考链接:

linux - SHELL(bash)脚本编程六:执行流程 - 学习 - SegmentFault 思否


 

你可能感兴趣的:(运维,linux)