我正在使用ksh作为shell创建一个shell脚本,脚本将日期作为参数并搜索名为:camp_base_prueba_date.txt和rangos.txt的文件并创建数组idcmps和rank,shell脚本为:< / p>
#!/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的值不正确(显然是垃圾)。我究竟做错了什么?或者我错过了什么?,我有几天坚持这些东西。非常感谢提前。
答案 0 :(得分:1)
当我在本地测试它时,这是有效的:
#!/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
...说,它仍然不是很好的代码 - 使用字符串拆分来填充数组,就像在前两行中所做的那样,充满了错误。