Frage

I'm making a shell script using ksh as shell, the script takes a date as a parameter and searches for files named: camp_base_prueba_date.txt and rangos.txt and creates the arrays idcmps and ranks, the shell script is:

#!/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

The file camp_base_prueba_$1.txt (where $1 is the current date) contains:

13416
38841
10383
10584
10445
10384

and the rangos.txt file contains:

0000-1999
2000-9999
10000-29999

when i run my shell as:

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

I obtain this stuff:

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 expected output should be:

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
****************************

But apparently is creating the array with garbage, because the output shows the value of variables rank and idcmp incorrectly (apparently garbage). What am I doing wrong? or what am I missing?, I have several days stuck with this stuff. Thanks a lot in advance.

War es hilfreich?

Lösung

This works when I test it locally:

#!/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

...that said, it's still not very good code -- using string-splitting to populate the arrays, as done in the first two lines, is full of bugs.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top