does bash have arrays that i can push into and run a for loop against?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting does bash have arrays that i can push into and run a for loop against?
# 1  
Old 03-25-2011
does bash have arrays that i can push into and run a for loop against?

Hi

I have a bash script where i need to push some values into an array and when finished, run a for loop against that array for example


myfile

Code:
sausages|meat
beef| meat
carrot| veg
...
...

Code:
for LINE in `cat myfile`; do
   FOOD=`echo $LINE | cut -d\| -f1`
   TYPE=`echo $LINE | cut -d\| -f2`
    
    case $TYPE in 

    meat)
             <push $FOOD into an array called @MEAT or something>
    ;; 
    veg)
             <push $FOOD into an array called @VEG or something>
   ;;
   esac
done


for element in @MEAT; do
    echo "I really like the meat called $@MEAT"
done


for element in @VEG; do
    echo "I really like the vegetable called $@VEG"
done

I know what I have done here is a syntactically incorrect (and mixes perl and bash all over the place :-))

but is what im doing above possible ? i.e can I push into an array with an ever incrementing element number (that i dont have to define) and then can I run a 'for' loop against the whole array ?

Id also like to perform a count against elements in a bash array as well if thats possible ?

i.e. if count of elements is more than 1 then use plural words etc etc


i know how to do all of this in perl, but is it possible in bash?

any help would be greatly appreciated
# 2  
Old 03-25-2011
Something like:
bash code:
  1. while IFS='|' read FOOD TYPE ; do
  2.     case $TYPE in
  3.     meat)
  4.         MEAT=( "${MEAT[@]}" "$FOOD" )
  5.         # MEAT[${#MEAT[@]}]="$FOOD" would work too (see below)
  6.     ;;
  7.     veg)
  8.         VEG&#91;${#VEG[@]}]="$FOOD"
  9.    ;;
  10.    esac
  11. done <myfile
  12. for element in "${MEAT[@]}"; do
  13.     echo "I really like the meat called $element"
  14. done
  15. for element in "${VEG[@]}"; do
  16.     echo "I really like the vegetable called $element"
  17. done
# 3  
Old 03-25-2011
Or another solution:
Code:
#!/bin/bash
while read ; do
   FOOD=${REPLY%|*}
   TYPE=`echo ${REPLY#*|}`  # use echo to trim leading spaces
 
  case $TYPE in 
     meat)
        MEAT[${#MEAT[@]}]=$FOOD
     ;; 
     veg)
        VEG[${#VEG[@]}]=$FOOD
     ;;
  esac
done < myfile
 
for element in "${MEAT[@]}"; do
    echo "I really like the meat called $element"
done
 
for element in "${VEG[@]}"; do
    echo "I really like the vegetable called $element"
done


Last edited by Chubler_XL; 03-25-2011 at 03:00 PM..
# 4  
Old 03-25-2011
You might also find it useful to check out useless use of backticks. Unlike perl, you can't cram things of unlimited size into a shell variable or argument list, so your programs might start surprising you once the file grows beyond 64KB (or even 4KB on some systems). This is an operating system limit.

Instead, whenever you have
Code:
for LINE in `cat file`

you do
Code:
while read LINE
do
        ....
done < file

so you never need more than a line at a time stored in a variable. This also avoids creating an extra do-nothing process (cat).

You can even do token splitting with read like frans illustrated.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 03-28-2011
thanks everyone for the great responses, really helpful
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple arrays in variable using for loop

Hi, I'm trying to get the number of files inside same kind of folders on each disks and assigning each values in to a variable named with same folder and disk name so that it'll be easy for me to identify each time.But somehow I'm not able to assign those values in that specific name variable... (1 Reply)
Discussion started by: ratheeshp
1 Replies

2. Shell Programming and Scripting

Loop over multiple arrays

Hi All I need really really help with this :- I have two files ( File1 , File 2) both files are output of two different scripts. File1 usually has a list of names ( sometimes 3 names sometimes 5 sometimes more , depends about the output of the script) File2 usually has a list of numbers... (2 Replies)
Discussion started by: samsan
2 Replies

3. Shell Programming and Scripting

Bash for loop with arrays second variable?

I am fairly new to bash and am not sure how to resolve this: I have a series of geographical long/lat points eg. 50/-30 listed on separate lines in a file called junk2. I have input these into an array and am then using that array in a for loop. Towards the end of the loop I create a file called... (4 Replies)
Discussion started by: lily-anne
4 Replies

4. Shell Programming and Scripting

Using arrays in bash using strings to bash built-in true

I have the following code and for some reason when I call the program using /home/tcdata/tatsh/trunk/hstmy/bin/bash/raytrac.bash --cmod=jcdint.cmod I get hasArgument = hasArgument = true Somehow the array element is returning even though I have not chosen the option. ... (41 Replies)
Discussion started by: kristinu
41 Replies

5. Shell Programming and Scripting

Problem with arrays and loop

Hello , im sorry for my english . im trying to create a dynamic menu that will display if the interface is ACTIVE OR STOPPED/FAILED for some reason i cant get it to work properly start_interface_func() { i=0 for interface_chk in 11 71 73 72 12 47 48 49 50 20 23 24 25 46 21 22 27 28... (5 Replies)
Discussion started by: visiown
5 Replies

6. Shell Programming and Scripting

Iterating through two arrays in a single loop

Hey everyone. Is it possible to use two arrays in a loop? Basically what I am trying to do is iterate through the elements in an array, and, based on a condition, use the current position in the array to point to that index in the array. Here's the loop structure I'm looking for: ... (1 Reply)
Discussion started by: msarro
1 Replies

7. Programming

Perl arrays and loop through array

Hi All I need to get <STDIN> from a user. the <STDIN> is a range of number delimited by "," (comma) and can be with range delimited by "-". Example: 1,2,3,4-9,12,15,34-36,70 Now I need to get this from the user and go on each number and "Do something"... but when trying to do this as above... (2 Replies)
Discussion started by: RedGrinGo
2 Replies

8. Shell Programming and Scripting

Push records to array during implicit loop and write to file

NEWBIE ALERT! Hi, I'm 1 month into learning Perl and done reading "Minimal Perl" by Tim Maher (which I enjoyed enoumously). I'm not a programmer by profession but want to use Perl to automate various tasks at my job. I have a problem (obviously) and are looking for your much appreciated help.... (0 Replies)
Discussion started by: jospan
0 Replies

9. Shell Programming and Scripting

Using arrays outside the loop

set -A town_name india pakistan srilanka india set -A town m=0 n=0 while } ] do t1=`echo ${town_name}` town= $t1 echo ${town} n=$((n+1)) m=$((m+1)) done t2=`echo ${town}` echo $t2 i m trying to get the value of town array outside the loop but i m nt getting it.. Could u plz... (5 Replies)
Discussion started by: priyanka3006
5 Replies

10. Shell Programming and Scripting

arrays in bash

hi guys, i have the following script and when i run it i get blank lines on the screen.. i am trying to display the contents of array var.. #!/usr/bin/bash var=`awk 'NR>20&&NR<31' try.sum | awk '{print $4}'` echo "${var}" (1 Reply)
Discussion started by: npatwardhan
1 Replies
Login or Register to Ask a Question