0

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.

14
  • I don't think this is the cause of your problem, but under the motto "less code is better code", try changing your 2 array assignments to the form set -A idcmps $(awk '{print $1}' /home/test/camp_base_prueba_$1.txt) . Something about more may be screwing this up. If you really "need" to send data thru a pipe to awk (for some unstated reason), they you definitely want to use cat file | awk .... Good luck. Commented Apr 20, 2014 at 16:03
  • What is set - A? I don't see it as one of the options - set [--abefhkmnptuvxBCEHPT] [-o option-name] [argument …] Commented Apr 20, 2014 at 16:04
  • from man ksh93 To assign values to an indexed array, use vname=(value . . .) or set -A vname value . . . . The value of all non-negative subscripts must be in the range of 0 through 4,194,303. Commented Apr 20, 2014 at 16:06
  • @shellter Thanks. It wasn't present in the bash set options so got a little confused. Commented Apr 20, 2014 at 16:08
  • 3
    What is your script supposed to actually accomplish? I'm guessing your end goal could be achieved in about three lines if Awk (and a lot less tortured, too). Commented Apr 20, 2014 at 16:13

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

great answer dude, but i dont understand the liminf=${rango%%-} and limsup=${rango#-} expressions.
@franvergara66, see mywiki.wooledge.org/BashFAQ/073 -- that's a bash reference, but these particular PEs are ones that made it into POSIX sh (and thus bash) by way of ksh.
I think the garbage in arrays is due to the "more" command because when i use the "cat" command, i dont have those problems
Thanks buddy, I'm pretty grateful to you.
@franvergara66, even cat is unnecessary inefficiency compared to just using <file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.