You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bash脚本执行报ambiguous redirect错误,请求协助排查

Bash脚本"ambiguous redirect"错误修复及问题解析

问题根源及修复点

  • 歧义重定向错误:if [ $qeueno[j] < $lineno[i] < $queueno[j + 1] ]里的<会被Bash识别为文件重定向符,而非数值比较运算符。同时Shell不支持连续比较写法,需拆分为两个条件用&&连接,且数值比较要使用-lt(小于)而非<。
  • 数组元素引用错误:数组元素正确写法是${数组名[索引]},比如${lineno[i]},而非$lineno[i];另外存在拼写错误qeueno,应为queueno。
  • 循环索引越界:mapfile生成的数组索引范围是0到长度-1,循环条件应改为i < $lenoflineno,而非i <= $lenoflineno,避免访问空元素。
  • 冗余命令简化:获取数组长度无需调用echo,直接用lenoflineno=${#lineno[@]}即可。

修正后的脚本

mapfile lineno < /home/admin/mqconfig_backup/playground/linesno.txt
mapfile queueno < /home/admin/mqconfig_backup/playground/queueno.txt
echo ${lineno[@]}
echo ${queueno[@]}
declare -a P1
lenoflineno=${#lineno[@]}
lenofqueueno=${#queueno[@]}
for((i=0; i < lenoflineno; i++));
do
    for((j=0; j < lenoflineno; j++));
    do
        echo ${lineno[i]}
        echo ${queueno[j]}

        # 去除mapfile读取时带入的换行符,避免数值比较出错
        local curr_queueno=$(echo "${queueno[j]}" | xargs)
        local curr_lineno=$(echo "${lineno[i]}" | xargs)
        local next_queueno=$(echo "${queueno[j+1]}" | xargs)
        
        if [ "$curr_queueno" -lt "$curr_lineno" ] && [ "$curr_lineno" -lt "$next_queueno" ];
        then
            P1+=("${queueno[i]}")
        fi
    done
done

额外说明

  • 若数组元素是字符串而非数值,需将-lt换成\<并使用[[ ... ]]语法,比如[[ "$curr_queueno" < "$curr_lineno" && "$curr_lineno" < "$next_queueno" ]]。

内容的提问来源于stack exchange,提问作者Shubham Gurao

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 19:10:33