how to use array variables in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to use array variables in shell
# 1  
Old 10-26-2012
how to use array variables in shell

Hi, everyone.

I wrote a code like this
Code:
for f in HB021*
  do 
   program
done

for f in HB034*
   do 
     program
done

for f in HB056*
    do 
       program
done
.
.
.
.
.
.


I know that a loop with array variables is much easier to do this. I searched online, but I can't achieve it.

HTML Code:
vari[0]=021
vari[1]=034
vari[2]=056
How can I use this array variable in the filename?

Thanks in advance!
# 2  
Old 10-26-2012
This thread might help.
# 3  
Old 10-27-2012
Quote:
Originally Posted by bipinajith
This thread might help.
Code:
#!/bin/bash

num[0]=021
num[1]=034
num[2]=056

for ((i=1;i<=5;i++))
 do
    for f in HB*
      do
         if [[$f == HB${num[i]*}]]; then
            echo $f
         else
           break
         fi
       done

done

Could you please help to check this code?
# 4  
Old 10-27-2012
Try this instead-

Code:
#!/bin/bash

declare -a num
num=( 021 034 056 )

for ((i=0;i<=2;i++))
do
    for f in HB*
      do
         fname="HB${num[i]}"
         if [[ "$f" == "$fname" ]]; then
            echo $f
         else
           break
         fi
       done
done

# 5  
Old 10-27-2012
Quote:
Originally Posted by bipinajith
Try this instead-

Code:
#!/bin/bash

Code:
#!/bin/bash

declare -a num
num=( 086 088 143 291 292)

for ((i=1;i<=4;i++))
 do
    for f in HB*
      do
         fname="HB${num[i]}*"
         if [[ "$f" == "$fname" ]]; then
            echo $f
         else
           echo "wrong"
         fi
       done

done

I need a * after HB${num[i]}, cause the file names are like this "HB086-AA-A8.txt"

but
Code:
fname="HB${num[i]}*"

seems wrong.

---------- Post updated at 07:15 PM ---------- Previous update was at 12:09 AM ----------

Code:
#!/bin/bash

declare -a num
num=( 086 088 143 291 292)

for ((i=1;i<=4;i++))
 do
    for f in HB*
      do
         fname="HB${num[i]}"
         if [[ "$f" == "$fname"* ]]; then
            echo $f
         else
           echo "wrong"
         fi
       done

done

# 6  
Old 10-28-2012
We're talking bash now. Assigning the array either way is fine; num=( 086 088 143 291 292) may be a bit more efficient. The ${...} construct will allow you to access the array's features and members:
Code:
echo ${#num[@]}, ${num[@]}
5, 086 088 143 291 292

#: element count; @: all elements; 0 .. 4 individual element.
In order to access individual files in a directory, you need to decollate the patterns, like e.g.
Code:
for f in ${num[@]}
   do for g in HB$f*
      do ls $g
         done
     done 
HB086
HB0861
HB0862
HB0863
HB088
HB0881
HB0882
HB0883
HB143
HB1431
...

f will run through all elements of the num array, while g will address every single file in that particular directory who's name is starting with "HB", having one of the num elements as part of its name, and then any postfix existing that dir ("", 1, 2, 3 in this example).

You could of course run the numerical for loop like
Code:
for ((i=1;i<${#num[@]};i++))

to make sure you are addressing every single element of num.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop; how to construct a array with variables

Hi everybody!! Here is the thing; I have a trouble in this simple situation, I'm trying to write an array with all the arguments of a command. I mean, if I have: ./mycommand.sh aa bb cc dd I need to take an array like this: myarray=(aa bb cc dd) So I use a simple for loop like this: for... (4 Replies)
Discussion started by: andresgom
4 Replies

2. Shell Programming and Scripting

'*' vs. '@' in Korn Shell Array Variables

In order to use the shellcurses functions described at: Shell Curses function library I am learning about ksh, which has arrays. My trusty Kochan & Wood book says that for any Korn Shell array AR : ${AR } expands to all the defined array elements, and ${#AR } expands to the number... (3 Replies)
Discussion started by: Clovis_Sangrail
3 Replies

3. Shell Programming and Scripting

array variables

Hi, There are total 100 files in the backup directory with the name format as below: prod_bkp_140611_13_30_05.txt prod_bkp_140611_14_30_05.txt prod_bkp_140611_15_30_05.txt prod_bkp_140611_16_30_05.txt How to use array variables (in perl) to get list of names of the above files ? (13 Replies)
Discussion started by: milink
13 Replies

4. Shell Programming and Scripting

comparing variables in awk array

Hi, I am trying to sort and display the below(like) input using awk command: Input: ------ 0;A 4;A 5;A 33;A 45;A 0;B 4;B 5;B 33;B 45;B Output (desired): (5 Replies)
Discussion started by: pvamsikr
5 Replies

5. UNIX for Dummies Questions & Answers

trying to store variables in an array

im looping through an array setting three variables each time (one of the variables gives me the position in the array and is incremented each loop) im trying to then set the variables to that position in the array without much luck. any ideas? anArray=`$VAR1+$VAR2+"("$pos")"` (1 Reply)
Discussion started by: magnia
1 Replies

6. Shell Programming and Scripting

How to store contents of a command in array of variables in shell script?

I want to store contents of command dir in array of variables For eg: dir contents are command d2 demovi~ file inven java new untitled folder d1 demovi er1 filename inven~ myfiles ubuntu desktop xmms ----------------------------------- I... (3 Replies)
Discussion started by: netresearch
3 Replies

7. Shell Programming and Scripting

How to use variables/array in grep command

Hi, I have a reqmt as i have some values in array and I want to search each value in a file by grep command. Here goes my scripting: #!/bin/ksh set -A ArrayA CENTER LEFT RIGHT echo "ArrayA contains: ${ArrayAİ*¨}" grep -e "${ArrayAİ*¨}" filename.txt The above grep is working for... (4 Replies)
Discussion started by: prashant43
4 Replies

8. Shell Programming and Scripting

storing variables in array.Please help

Hi All, I need some help with arrays. I need to take input from the user for hostname, username and password until he enters .(dot) or any other character and store the values in the variable array. I would further connect to the hostname using username and passwd and copy files from server to... (7 Replies)
Discussion started by: nua7
7 Replies

9. Shell Programming and Scripting

Getting variables into a array.

Hi , Im trying to monitor 2 instancesof a process on our solaris server and trying to do a notification if the returned process size is greater than 500M. Since there are two variables returned I want to make use of arrays to check each and every variable that is stored. the issue that im facing... (2 Replies)
Discussion started by: vivsiv
2 Replies

10. Shell Programming and Scripting

Array variables

hi, how to store array values in unix? and how to access array values? can somebody help? kavitha (2 Replies)
Discussion started by: kavitha
2 Replies
Login or Register to Ask a Question