If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > script help needed

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-06-04, 15:47
chatguy2020 chatguy2020 is offline
Registered User
 
Join Date: May 2003
Posts: 58
script help needed

I want to initialize variables in a loop as shown below (commented). It gives errors Ex: COUNT1=1: not found. Similarily the statement below it gives errors: 1=COUNT1: not found. etc...

If I say for example,

COUNT1=`echo "$ALLCOUNTS" | cut -d" " -f1`
COUNT2=`echo "$ALLCOUNTS" | cut -d" " -f2` the variables initialize well.

Can somebody please tell why?

Code:
#
i=1 
while [ $i -lt $COUNT_DESIRED ]
do
    #COUNT$i=`echo "$ALLCOUNTS" | cut -d" " -f$i`
    [ -z $COUNT$i ] && COUNT$i=11
    ${allcounts[$i-1]}=COUNT$i
    i=`expr $i + 1`
done
Reply With Quote
  #2 (permalink)  
Old 04-06-04, 17:01
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
You can do something like this :
Code:
i=1 
while [ $i -le $COUNT_DESIRED ]
do
    #COUNT$i=`echo "$ALLCOUNTS" | cut -d" " -f$i`
    eval Count=\$COUNT$i
    allcounts[$((i-1))]=${Count:-11}
    (( i += 1 ))
done
__________________
Jean-Pierre.
Reply With Quote
  #3 (permalink)  
Old 04-09-04, 11:10
chatguy2020 chatguy2020 is offline
Registered User
 
Join Date: May 2003
Posts: 58
Aigles, Thanks for the reply.

i=1
while [ $i -le 5 ]
do
COUNT$i=$i
(( i+= 1 ))
done

Here my idea is just to initialize 5 variables dynamically. The above approach won't work. I tried using

i=1
while [ $i -le 5 ]
do
((COUNT$i=$i))
(( i+= 1 ))
done

it won't work either. Any ideas? Thanks.
Reply With Quote
  #4 (permalink)  
Old 04-09-04, 13:03
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
First approach
Code:
i=1
while [ $i -le 5 ]
do
   eval COUNT$i=$i
   (( i+= 1 ))
done
The second approach works fine for me, what is your problem ?
execute with -x option to verify variable creation.

Note: The two solutions works only with bash and ksh due to '(( ))' syntax usage.
(( i+= 1)) can be replaced by i=`expr $i + 1`
__________________
Jean-Pierre.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On