Find the answer to your Linux question:
Results 1 to 2 of 2
Perhaps it would be easiest to show you what I'm trying to do with the code and output... Code: #!/bin/sh national=( "us_radar" "-120,26" "-60,52" "1500" "650" ) iowalocal=( "iowa" "-99.25,37.5" ...
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Location
    Ames, IA
    Posts
    1

    Question bash - Passing arrays into for-loop variable?

    Perhaps it would be easiest to show you what I'm trying to do with the code and output...

    Code:
    #!/bin/sh
    
    national=( "us_radar" "-120,26" "-60,52" "1500" "650" )
    iowalocal=( "iowa" "-99.25,37.5" "-87.25,45.5" "900" "600" )
    
    echo ${#national[*]}
    echo ${#iowalocal[*]}
    
    for site in ${national} ${iowalocal}; do
    
        echo ${#site[*]}
    
    done
    Output:
    Code:
    5
    5
    1
    1
    I've verified that national[0] or iowalocal[0] is the one item that actually gets passed into site. Is there some way to pass entire arrays into a for-loop variable? I can restructure my code with five arrays instead and just do for num = 0 1 etc., but that's a bit messy for this script's purpose.

    Thanks for any help you can provide!
    Last edited by j.patton; 03-05-2008 at 10:28 PM. Reason: fixed title

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    To print the elements of an array you can do something like:

    Code:
    for i in $(seq 0 $((${#national[*]} - 1)))
    do
      echo "${national[$i]}"
    done
    Have a read of this:

    http://tldp.org/LDP/abs/html/arrays.html

    Regards

Posting Permissions

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