incrementing the variable name along with the data?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting incrementing the variable name along with the data?
# 1  
Old 12-19-2011
incrementing the variable name along with the data?

Hello folks.

I am trying to increment my variable names to match a counter that is to be used later on...

Basically, i have a for loop that lists directories (for example TEST_OS DVP_OS PROD_OS ) but this loop is not static, it may contain 3 directory once and the next run 5 directories.

I then, want to assign "selection1" to TEST_OS, "selection2" to DVP_OS and "selection3" to PRD_OS. Again, those are not static...

Here is the code i have for now, but the "combining" of variables is not working...Smilie (selection$counter=$i) What i want, is that the variable selection1=TEST_OS and so forth...

Code:
BKPSETS=`ls -altr /etc/workdir/ | grep $OS | awk '{print $9}'`
counter=1

for i in $BKPSETS
do
        echo $counter "-" $i
        selection$counter=$i
        echo $selection

        counter=`expr $counter + 1`
done

read CHOICE

if [[ $CHOICE -ge $COUNTER ]]
then
        echo "No such choice available.  Quitting!"
        exit 1
        else
        echo "You've selected option $CHOICE"
fi

# 2  
Old 12-19-2011
Could you please elaborate more: which OS and shell are you using?
Could you post an example of the directory structure and what should be the output (variable content) based on that structure?
# 3  
Old 12-19-2011
Code:
eval "selection$counter=\$i"

counter=$(( $counter + 1 )) ## no need for an external command

Or
Code:
eval "selection$(( counter += 1 ))=\$i"

# 4  
Old 12-19-2011
The script in Post #1 is not sound in this context.

Please post a specification for what you are trying to achieve - including sample input and sample output - complete with a description of the process.
It is impossible to determine the process based on faulty code and a garbled process specification.

Further to radlouv, please do post what Operating System and version you have and what Shell you prefer.

Please take the time to describe the process such that an 11-year-old would understand.
# 5  
Old 12-21-2011
Sorry folks for assuming it was clear...at least it was to me... Smilie

This is a Linux box, ksh. Technically, i have this output as my $bkpsets variable:

Code:
 TEST_OS DVP_OS PROD_OS

As mentionned, it may contain 3 instances as in this example, but my be 5 the next time around...

From there, i want to be able to set my "Selection" variable to

Code:
 selection1=TEST_OS selection2=DVP_OS selection3=PROD_OS

That way, i can do something else later, based on that selection.
# 6  
Old 12-21-2011
OK,
given you're using the KornShell you could use arrays, there's no need to use eval:

Code:
$ set -A a TEST_OS DVP_OS PROD_OS
$ print "${a[0]}"
TEST_OS
$ print "${a[1]}"                                                                                                
DVP_OS
$ print "${a[@]}"
TEST_OS DVP_OS PROD_OS

# 7  
Old 12-21-2011
Quote:
Originally Posted by cfajohnson
Code:
eval "selection$counter=\$i"

counter=$(( $counter + 1 )) ## no need for an external command

Or
Code:
eval "selection$(( counter += 1 ))=\$i"

Thanks for this code...Can i "echo" this variable to see its content, just to debug for now...Since this is using the eval, is it echoed differently? a simple echo won't do. I get no errors, but no idea what is in the "selection" variable.

Thanks.

---------- Post updated at 02:05 PM ---------- Previous update was at 02:00 PM ----------

Thanks radoulov. Let's see if i can get this to work...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing specific and incrementing lines of text from file via variable

This is part of a larger script where I need to pass only 1 line of a file to the script, based on a variable and not a direct reference. As part of a for loop : # for((line=0;line<50;line++)); do # awk ‘NR==$line' PhraseList.txt; done ... (5 Replies)
Discussion started by: Seth
5 Replies

2. Shell Programming and Scripting

Help with incrementing data in some field

Hi I have the below set of lines , i need to duplicate these lines 1000 times, also eevrytime when it is incremented , it should increment the one in Blue color. 130400030000010000200001 130400030000010000200002 140050030000010000200005A eg: 130400030000010000200001... (5 Replies)
Discussion started by: santhoshks
5 Replies

3. Shell Programming and Scripting

Converting variable space width data into CSV data in bash

Hi All, I was wondering how I can convert each line in an input file where fields are separated by variable width spaces into a CSV file. Below is the scenario what I am looking for. My Input data in inputfile.txt 19 15657 15685 Sr2dReader 107.88 105.51... (4 Replies)
Discussion started by: vharsha
4 Replies

4. UNIX for Dummies Questions & Answers

Incrementing variable in for

Hi, want to increment a variable in a for loop like this: for (( c=$total-1; c>=0; c-- )) do if ; then maximo=$valores fi done But it gives the error: No such file or directory How can i do this only incrementing the c variable? Thanks (8 Replies)
Discussion started by: limadario
8 Replies

5. UNIX for Dummies Questions & Answers

Incrementing Variable Names

Hi, I am using BASH. I have encountered a situation where the following is necessary (but I am not sure how to do it): #Define multiple arrays, whose names only differ by a number: ARRAY_1=(1 2 3) ARRAY_2=(4 5 6) ARRAY_3=(7 8 9) #Define ARRAY_AMOUNT, the number of arrays. In this case... (1 Reply)
Discussion started by: msb65
1 Replies

6. Shell Programming and Scripting

Facing problem in incrementing the variable

When I did, echo $SHELL in cmd prompt of putty, its displaying /bin/sh And in my shell script., I hav started with., #!/bin/sh and i=1; while ; do . . . i=$; (9 Replies)
Discussion started by: av_vinay
9 Replies

7. Homework & Coursework Questions

Incrementing Variable resets outside of while loop

1. The problem statement, all variables and given/known data: Variable is resetting to 0 after incrementing in while loop My bit of scripting displays the current users logged in the machine. Then it reads in a specific username and displays the processes for that user. The portion that I... (3 Replies)
Discussion started by: ratzlaff
3 Replies

8. Shell Programming and Scripting

Incrementing a variable is not happening

Hi All, Iam trying to increment a variable Following is the code #!/usr/bin/ksh i=1; i='expr $i+1'; echo $i; Output: expr $i+1 not able to understand why its happening in that way i was expecting result as 2... if the above method is worng .. can you help how i can get... (3 Replies)
Discussion started by: kiranlalka
3 Replies

9. Linux

Incrementing the date stored in the variable

Hi all, I have a variable with date as 20080831 . Now I want to increment it as 20080901 and so on.Is there any command for this. Please help me. thanks rameez (1 Reply)
Discussion started by: rameezrajas
1 Replies

10. UNIX for Advanced & Expert Users

how to read the data from an excel sheet and use those data as variable in the unix c

I have 3 columns in an excel sheet. c1 c2 c3 EIP_ACCOUNT SMALL_TS_01 select A.* from acc; All the above 3 col shoud be passed a variable in the unix code. 1.How to read an excel file 2.How to pass these data as variable to the unic script (1 Reply)
Discussion started by: Anne Grace
1 Replies
Login or Register to Ask a Question