Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: shell script -- naming variables in a loop?

  1. #1
    Join Date
    Feb 2007
    Beans
    102
    Distro
    Ubuntu 8.04 Hardy Heron

    shell script -- naming variables in a loop?

    hey guys, just had some quick questions about variable naming....

    i'm trying to parse a datafile and store the elements into variable names.

    Main.dat contains lines of data entries separated by a non-constant whitespace

    here's my code:

    Code:
    lines=`wc -l Main.dat | cut -f 1 -d ' '`
    echo $lines
    counter=1
    
    while [ $counter -le $lines ]
    do
    eval word$counter=`cut -f 1 -d ' ' Main.dat | head -n $counter | tail -n 1`
    echo \$word$counter
    counter=`expr $counter + 1`
    done
    the problem is that when i run that part of the script, i cannot access the variables that i've created. ie word1, word2, word3, word4, word5

    the echo \$word$counter part will echo
    $word1
    $word2
    $word3
    $word4
    $word5

    but if i remove the leading \ all i get is the value for $counter
    1
    2
    3
    4
    5

    i can't find a way to access the variable

    any ideas?

  2. #2
    Join Date
    Aug 2006
    Location
    Boston
    Beans
    216
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: shell script -- naming variables in a loop?

    Have you tryd quoting your VAR in the expr
    Code:
    counter=`expr "$counter" + 1`

  3. #3
    Join Date
    Aug 2006
    Location
    Boston
    Beans
    216
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: shell script -- naming variables in a loop?

    Oh also run you script for debug

    sh -x script


    and post that back

  4. #4
    Join Date
    Aug 2006
    Location
    Boston
    Beans
    216
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: shell script -- naming variables in a loop?

    Sorry for the three posts before your response! but you should also quote your VAR in your test strings

    Code:
    lines=`wc -l Main.dat | cut -f 1 -d ' '`
    echo $lines
    counter=1
    
    while [ "$counter" -le "$lines" ]
    do
    eval word$counter=`cut -f 1 -d ' ' Main.dat | head -n $counter | tail -n 1`
    echo \$word$counter
    counter=`expr $counter + 1`
    done


    Now not sure any of that will make the differance, but I will wait now for your reply

  5. #5
    Join Date
    Feb 2007
    Beans
    102
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: shell script -- naming variables in a loop?

    well, counter will update, it's just that the variable $word$counter cant be printed....

    i shortened the loop, but you get the idea
    Code:
     ./myacct
    + set -v on
    lines=`wc -l Main.dat | cut -f 1 -d ' '`
    + wc -l Main.dat
    + cut -f 1 -d  
    + lines=5
    echo $lines
    + echo 5
    5
    counter=4
    + counter=4
    
    while [ $counter -le $lines ]
    do
    
    
    
    
    eval word$counter=`cut -f 1 -d ' ' Main.dat | head -n $counter | tail -n 1`
    echo $word$counter
    
    counter=`expr $counter + 1`
    done
    + [ 4 -le 5 ]
    + cut -f 1 -d   Main.dat
    + head -n 4
    + tail -n 1
    + eval word4=Fruits
    + word4=Fruits
    + echo 4
    4
    + expr 4 + 1
    + counter=5
    + [ 5 -le 5 ]
    + cut -f 1 -d   Main.dat
    + head -n 5
    + tail -n 1
    + eval word5=Tennis-racket
    + word5=Tennis-racket
    + echo 5
    5
    + expr 5 + 1
    + counter=6
    + [ 6 -le 5 ]

  6. #6
    Join Date
    Feb 2007
    Beans
    102
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: shell script -- naming variables in a loop?

    and here's the output with \$word$counter...
    Code:
    ./myacct
    + set -v on
    lines=`wc -l Main.dat | cut -f 1 -d ' '`
    + wc -l Main.dat
    + cut -f 1 -d  
    + lines=5
    echo $lines
    + echo 5
    5
    counter=4
    + counter=4
    
    while [ "$counter" -le "$lines" ]
    do
    
    eval word$counter=`cut -f 1 -d ' ' Main.dat | head -n $counter | tail -n 1`
    echo \$word$counter
    
    counter=`expr $counter + 1`
    done
    + [ 4 -le 5 ]
    + cut -f 1 -d   Main.dat
    + head -n 4
    + tail -n 1
    + eval word4=Fruits
    + word4=Fruits
    + echo $word4
    $word4
    + expr 4 + 1
    + counter=5
    + [ 5 -le 5 ]
    + cut -f 1 -d   Main.dat
    + head -n 5
    + tail -n 1
    + eval word5=Tennis-racket
    + word5=Tennis-racket
    + echo $word5
    $word5
    + expr 5 + 1
    + counter=6
    + [ 6 -le 5 ]

  7. #7
    Join Date
    Aug 2006
    Location
    Boston
    Beans
    216
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: shell script -- naming variables in a loop?

    Copy and paste this into a test script and tell me what it does when you run it.

    Code:
    #!/bin/sh
    
    lines=`wc -l Main.dat | cut -f 1 -d ' '`
    echo $lines
    counter=1
    
    while [ "$counter" -le "$lines" ]
    do
    eval word"$counter"= `cut -f 1 -d ' ' Main.dat | head -n $counter | tail -n 1`
    echo $word$counter
    counter=`expr $counter + 1`
    donee

  8. #8
    Join Date
    Aug 2006
    Location
    Boston
    Beans
    216
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: shell script -- naming variables in a loop?

    watch the typo on the DONE sorry.

  9. #9
    Join Date
    Feb 2007
    Beans
    102
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: shell script -- naming variables in a loop?

    Code:
    ./shelltest
    + wc -l Main.dat
    + cut -f 1 -d  
    + lines=5
    + echo 5
    5
    + counter=1
    + [ 1 -le 5 ]
    + cut -f 1 -d   Main.dat
    + head -n 1
    + tail -n 1
    + eval word1= Basketball
    + word1= Basketball
    eval: 1: Basketball: not found
    + echo 1
    1
    + expr 1 + 1
    + counter=2
    + [ 2 -le 5 ]
    + cut -f 1 -d   Main.dat
    + head -n 2
    + tail -n 1
    + eval word2= Candy
    + word2= Candy
    eval: 1: Candy: not found
    + echo 2
    2
    + expr 2 + 1
    + counter=3
    + [ 3 -le 5 ]
    + cut -f 1 -d   Main.dat
    + head -n 3
    + tail -n 1
    + eval word3= Car
    + word3= Car
    eval: 1: Car: not found
    + echo 3
    3
    + expr 3 + 1
    + counter=4
    + [ 4 -le 5 ]
    + cut -f 1 -d   Main.dat
    + head -n 4
    + tail -n 1
    + eval word4= Fruits
    + word4= Fruits
    eval: 1: Fruits: not found
    + echo 4
    4
    + expr 4 + 1
    + counter=5
    + [ 5 -le 5 ]
    + cut -f 1 -d   Main.dat
    + head -n 5
    + tail -n 1
    + eval word5= Tennis-racket
    + word5= Tennis-racket
    eval: 1: Tennis-racket: not found
    + echo 5
    5
    + expr 5 + 1
    + counter=6
    + [ 6 -le 5 ]

    do you have aim/msn/yahoo? pm me if you do

  10. #10
    Join Date
    Aug 2006
    Location
    Boston
    Beans
    216
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: shell script -- naming variables in a loop?

    aim name is hatekast

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •