Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically name a bash array 1

Status
Not open for further replies.

alan147

Technical User
Nov 15, 2002
128
GB
Hello

I wrote a script that reads the file names in a directory and puts them in to one of two arrays based on part of the filename which is a mailbox ID.

For example:

INSURER1_SEND_ARRAY=(`ls -al | awk '/^-/' | awk '{print $9}' | grep ABCD |cut -f1 -d.`)

INSURER2_SEND_ARRAY=(`ls -al | awk '/^-/' | awk '{print $9}' | grep EFGH |cut -f1 -d.`)

This works fine. Now we have 10 insurers I want to make the script scaleable.

To do this I have a text file that contains the Insurer's name and the Mailbox ID. From this I could create the array name:

for INSURER in `cat INSURE_LIST.TXT | cut -d "," -f1`
do
MAILBOX_ID=`cat $DATA/INSURER_LIST.TXT | grep $INSURER | cut -d "," -f2`
ARRAY_NAME=$INSURER"_SEND_ARRAY"
$ARRAY_NAME"=(`ls -al | awk '/^-/' | awk '{print $9}' | grep $MAILBOX_ID | cut -d. -f1`)
done

Relpacing INSURER1_SEND_ARRAY with $ARRAY_NAME gives me a syntax error unexpected token.
Removing the brackets around the command allows me to run the script but it returns this:

aaaaa_SEND_ARRAY=ABCD_MTAD05_MTAD000971-019: command not found

Does anyone know what I'm doing wrong?

Thanks

Alan

 


Can you post some sample data and what would be the expected results?
Also, perhaps there is an alternative to fulfill your requirements, so post these requirements.
As the saying goes: "There are many ways to skin a cat"
[tongue]




----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
I'd use the eval builin.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

If you could let us know how are you using these arrays (perhaps a code snippet), we could come up with a better alternative.
[3eyes]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
In its original form the script checks a directory for message files, if there are any it then creates an two arrays one for each insurer to hold a list of files to be sent to each insurer.

The files are transmitted and are removed from the original location and put into another directory.

The script compares the contents of the arrays with the files that have been sent.

The snippet below is part of the original script that has been working for the past 4 years.

# Check the contents of the Send directory

cd $EDI_LOCN/Send

# We'll find out if there any file to send first. If there are we will continue, if not put an entry in the log file and then quit.

# Count the number of files in the send directory

FILE_COUNT=(`ls -al | awk '/^-/' | awk '{print $9}' | wc -l`)

if [ $FILE_COUNT -gt 0 ]
then
# one array is for the Aaaaa files the other is for the Bbbbb files. We strip off the .env

# to make comparing the files in the Send directory easier later.

AAAAA_SEND_ARRAY=(`ls -al | awk '/^-/' | awk '{print $9}' | grep 1234 |cut -f1 -d.`)

BBBBB_SEND_ARRAY=(`ls -al | awk '/^-/' | awk '{print $9}' | grep 5678 |cut -f1 -d.`)

AAAAA_COUNT=${#AAAAA_SEND_ARRAY[@]}

BBBBB_COUNT=${#BBBBB_SEND_ARRAY[@]}

echo "There are $AAAAA_COUNT Aaaaa envelope file(s) in the Send directory." >>$REP_LOCN/EDI-TRANSMISSION-LOG-$TODAY.log

echo " " >>$REP_LOCN/EDI-TRANSMISSION-LOG-$TODAY.log

echo "There are $BBBBB_COUNT Bbbbb envelope file(s) in the Send directory." >>$REP_LOCN/EDI-TRANSMISSION-LOG-$TODAY.log

echo " ">>$REP_LOCN/EDI-TRANSMISSION-LOG-$TODAY.log

else

echo "There are no files in the send directory, check the EDI task has run." >>$REP_LOCN/EDI-TRANSMISSION-LOG-$TODAY.log

exit

fi



Now that we have more insurers I want to make the script easier to maintain by creating the arrays by reading the contents of a text file.

This is a typical list of fie in the send directory:

-rw-rw-rw- 1 xxx yyy 1746 Nov 27 15:10 1234_RCON52_RCON001394-001.env
-rw-rw-rw- 1 xxx yyy 2802 Nov 29 11:54 1234_RCON52_RCON001400-002.env
-rw-rw-rw- 1 xxx yyy 35697 Nov 29 22:00 1234_RCON52_RCON001401-024.env
-rw-rw-rw- 1 xxx yyy 1638 Nov 30 11:54 1234_RCON52_RCON001404-001.env
-rw-rw-rw- 1 xxx yyy 1637 Nov 30 23:32 1234_RCON52_RCON001406-001.env
-rw-rw-rw- 1 xxx yyy 10401 Dec 13 19:00 5678_RCON52_RCON001432-007.env
-rw-rw-rw- 1 xxx yyy 7338 Dec 14 12:18 5678_RCON52_RCON001434-005.env
-rw-rw-rw- 1 xxx yyy 1724 Dec 15 15:23 5678_RCON52_RCON001439-001.env
-rw-rw-rw- 1 xxx yyy 1464 Dec 16 14:34 5678_RCON52_RCON001444-001.env

Alan
 



Using eval will not work:
Code:
==> cat ./k0
## k0 Script
i=0
IFS=','
while read INSURER MAILBOX_ID
do
   eval ARRAY_NAME=$INSURER"_SEND_ARRAY"
   echo $ARRAY_NAME
   eval $ARRAY_NAME=\(`ls -al | awk '/^-/{print $9}' | grep $MAILBOX_ID | cut -d. -f1|tr "\n" " "`\)
  ((i+=1))
done < insure_list.txt
((n=i-1))
i=0
echo "+--- i=$i, n=$n"
while read INSURER MAILBOX_ID
do
  eval ARRAY_NAME=$INSURER"_SEND_ARRAY"
  eval ARRAY_COUNT=$INSURER"_COUNT"
  echo "+--- $i $ARRAY_COUNT --------------"
  eval $ARRAY_COUNT=${$ARRAY_NAME[@]}
  print $ARRAY_COUNT
  ((i+=1))
done < insure_list.txt
echo "+---------------------"

==> ./k0
INSURER1_SEND_ARRAY
INSURER2_SEND_ARRAY
+--- i=0, n=1
+--- 0 INSURER1_COUNT --------------
./k0: line 19: $ARRAY_COUNT=${$ARRAY_NAME[@]}: bad substitution
+---------------------
[thumbsdown]



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 


But...here is an alternative:
Code:
==> cat k1
## Alternatives
i=0
IFS=','
while read INSURER MAILBOX_ID
do
  send_array[$i]="$INSURER:`ls -al|awk '/^-/{print $9}'|grep $MAILBOX_ID|cut -d. -f1|tr '\n' ' '`"
  ((i+=1))
done < insure_list.txt
IFS=' '
((n=i-1))
i=0
echo "+--- i=$i, n=$n"
#
while [ $i -le $n ]
do
  echo "+--- $i --------------"
  echo "INSURER=`echo ${send_array[$i]}|cut -d':' -f1`"
  SEND_LIST=(`echo ${send_array[$i]}|cut -d':' -f2-`)
  echo "SEND_LIST=$SEND_LIST"
  ARRAY_COUNT=${#SEND_LIST[@]}
  echo "ARRAY_COUNT=$ARRAY_COUNT"
  ((i+=1))
done
echo "+---------------------"
[thumbsup2]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 

Fixed a typo:
Code:
==> cat k1
## Alternatives
i=0
IFS=','
while read INSURER MAILBOX_ID
do
  send_array[$i]="$INSURER:`ls -al|awk '/^-/{print $9}'|grep $MAILBOX_ID|cut -d. -f1|tr '\n' ' '`"
  ((i+=1))
done < insure_list.txt
IFS=' '
((n=i-1))
i=0
echo "+--- i=$i, n=$n"
#
while [ $i -le $n ]
do
  echo "+--- $i --------------"
  INSURER="`echo ${send_array[$i]}|cut -d':' -f1`"
  echo "INSURER=$INSURER"
  SEND_LIST=(`echo ${send_array[$i]}|cut -d':' -f2-`)
  echo "${INSURER}_SEND_LIST=${SEND_LIST[*]}"
  ARRAY_COUNT=${#SEND_LIST[@]}
  echo "${INSURER}_COUNT=$ARRAY_COUNT"
  ((i+=1))
done
echo "+---------------------"
[wink]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Thanks for the help. Could you explain briefly what is going on, I think I understand but I want to make sure.

Alan
 


a) First while loop loads the main array:

For each insurer it creates an element in the send_array[ 0..N ] where 0 to n are a incremental number; 0-insurer1 .. N-insurerN
The content of each element is the insurer separated from the mailbox array by a semi-colon :)), like:

Code:
{insurer}:{mailbox array}

b) Second while loop is an example of how to use the array:

This extracts the insurer from the array element i:
Code:
INSURER="`echo ${send_array[$i]}|cut -d':' -f1`"
This extracts the mailbox array into a "send list" array:
Code:
SEND_LIST=(`echo ${send_array[$i]}|cut -d':' -f2-`)

HTH.
[shadeshappy]









----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top