Solaris中带有ksh的shell脚本中的输出不正确



我正在制作一个使用ksh作为shell的shell脚本,该脚本以日期为参数,搜索名为:camp_base_prueba_date.txt和rangos.txt的文件,并创建数组idcmps和ranks,shell脚本为:

#!/bin/ksh    
set -A idcmps $(more /home/test/camp_base_prueba_$1.txt | awk '{print $1}')
set -A ranks $(more /home/test/rangos.txt | awk '{print $1}')
rm camp_plani_prueba_$1.txt
for idcmp in ${idcmps[@]}
do
   echo 'the id es: '$idcmp
    for rango in ${ranks[@]}
    do
      echo "the rank: "$rango
      liminf=$(echo $rango|cut -d'-' -f1)
      limsup=$(echo $rango|cut -d'-' -f2)
      echo 'limits: '$liminf'-'$limsup
      echo "****************************"
     done
done
exit

文件camp_base_prueba_$1.txt(其中$1是当前日期)包含:

13416
38841
10383
10584
10445
10384

rangos.txt文件包含:

0000-1999
2000-9999
10000-29999

当我将shell运行为:时

nohup ksh test.sh 14042014 > test.log 2>test.err

我得到这个东西:

the id es: ::::::::::::::
the rank: ::::::::::::::
limits: ::::::::::::::-::::::::::::::
****************************
the rank: /home/test/rangos.txt
limits: /home/test/rangos.txt-/home/test/rangos.txt
****************************
the rank: ::::::::::::::
limits: ::::::::::::::-::::::::::::::
****************************
the rank: 0000-1999
limits: 0000-1999
****************************
....

预期输出应为:

the id es: 13416
the rank: 0000-1999
limits: 0000-1999
****************************
the rank: 2000-9999
limits: 2000-9999
****************************
the rank: 10000-29999
limits: 10000-29999
****************************
the id es: 38841
the rank: 0000-1999
limits: 0000-1999
****************************
the rank: 2000-9999
limits: 2000-9999
****************************

但显然是在用垃圾创建数组,因为输出错误地显示了变量rank和idcmp的值(显然是垃圾)。我做错了什么?或者我错过了什么?,我有好几天都在做这种事。提前非常感谢。

当我在本地测试它时,这是有效的:

#!/bin/ksh
set -A idcmps $(awk '{print $1}' <"camp_base_prueba_$1.txt")
set -A ranks $(awk '{print $1}' <rangos.txt)
rm "camp_plani_prueba_$1.txt"
for idcmp in "${idcmps[@]}"; do
  echo "the id es: $idcmp"
  for rango in "${ranks[@]}"; do
    echo "the rank: "$rango
    liminf=${rango%%-*}
    limsup=${rango#*-}
    echo "limits: $liminf-$limsup"
    echo "****************************"
  done
done

也就是说,它仍然不是很好的代码——使用字符串分割来填充数组,就像前两行中所做的那样,充满了错误。

最新更新