Assignment with variable assigment


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assignment with variable assigment
# 1  
Old 12-21-2013
Assignment with variable assigment

Hello All,

I'm trying to assign integer values to variables using substitution in both the value and variable's name, i.e.,

Code:
number$x=$x

where x is equal to one in the first assignment, two in the second assignment, and so on with x being incremented each time.

However, when I do the first assignment

Code:
number$x=$x

the assignment works but I get

number1=1 not found.

what's going on?
Thank you in advance.
Smilie

Last edited by bartus11; 12-21-2013 at 07:35 PM.. Reason: Please use code tags
# 2  
Old 12-21-2013
Try:
Code:
eval number$x=$x

# 3  
Old 12-22-2013
Rule: if you compose a string, and you want to run it, and it is other than a command name,then you need eval.

Last edited by MadeInGermany; 12-22-2013 at 04:23 AM.. Reason: precised
# 4  
Old 12-22-2013
What shell are you using? Does it have arrays:
Code:
number[x]=$x
echo "${number[x]}"

# 5  
Old 12-22-2013
Yes, I see that the eval command works.
However, the assignment also does work without eval.
But, I get that "not found" error.
What's going on there?
# 6  
Old 12-22-2013
Without eval the shell is trying to execute the command number1=1 which apparently is not present on your system so it says "not found"
# 7  
Old 12-22-2013
Obviously.
But why is the shell trying to execute a command?
Why is not performing assignment?
So, I'll ask once again, what's going on here?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable Assignment

Hi I am facing a problem. export local_folder=/opt/app/ cd /opt/app/abc/ abcversion="abc*" (abcga5 is inside /opt/app/abc/) echo $abcversion (it echoes the correct version as abcga5 ) Now when I reuse the value of abcversion for a below path: export... (6 Replies)
Discussion started by: ankur328
6 Replies

2. Shell Programming and Scripting

Same Variable Assignment

Hi I have a strange problem: In my shell script I am performing a copy task: . prop.txt cp -r $dir/ $dir/archive $dir is fetched from a property file (prop.txt) which stores its value dir=/opt/data Now the problem is another dir1 comes into picture. I only want to add... (1 Reply)
Discussion started by: ankur328
1 Replies

3. Shell Programming and Scripting

Use one loop get the variable assignment

for var in {1..3} do lspci -nn |awk -v i=$var '/8086:10a7/{split($1,a,""); print "0x"a}' done it will output 0x01 0x00 0x1 I want to let bus=0x01 slot=0x00 fun=0x1, How to modify this loop? I know it can be get by bus=`lspci -nn |awk... (4 Replies)
Discussion started by: yanglei_fage
4 Replies

4. Shell Programming and Scripting

variable assigment not works in shell script

Hi, The following assigment is not working within shell script but is working from command line. Could anybody advise why? OS - solaris 8 APPL=`grep "$Application" ldapapps |awk '{print $1}'` echo $APPL (5 Replies)
Discussion started by: urello
5 Replies

5. Shell Programming and Scripting

variable assignment problem

hi everyone pls pls help in the query below it's urgent pls -bash-3.00$ abc=deepak -bash-3.00$ a=1 -bash-3.00$ def_${a}=$abc -bash: def_1=deepak: command not found y iti is giving error ? (3 Replies)
Discussion started by: aishsimplesweet
3 Replies

6. Shell Programming and Scripting

Help with variable assignment

Hi, In AIX I have a variable with , (coma) separated values assigned to it like shown below var1=apple,boy,chris i want to convert this to var1='apple','boy','chris' the number of values assigned to var1 might change and it could be from 1 to n any suggestions please? (3 Replies)
Discussion started by: rahul9909
3 Replies

7. Shell Programming and Scripting

Dynamic variable assignment

Hi all, I’m very new to UNIX programming. I have a question on dynamic variable 1. I’m having delimited file (only one row). First of all, I want to count number of columns based on delimiter. Then I want to create number of variables equal to number of fields. Say number of... (5 Replies)
Discussion started by: mkarthykeyan
5 Replies

8. Shell Programming and Scripting

Indirect variable assignment

Hi I have variable A_B=alpha also var1="A" var2="B" I want to retrieve the value alpha using var1 and var2 , somthing like echo ${${var1}_${var2}} that works. Obviously this is receiving syntax error (6 Replies)
Discussion started by: sumir
6 Replies

9. Shell Programming and Scripting

help needed in variable assignment

Hi all, I am having some value in feedrow in my script. And want to assign first three character in to DTYPE. I am using following syntax. DTYPE=`$FEEDROW|cut -c 1-3` But it's not assigning this valuse into DTYPE. Please suggest. Its really uegent. Regards, Gander_ss (2 Replies)
Discussion started by: gander_ss
2 Replies

10. UNIX for Dummies Questions & Answers

@ in a variable assignment

Hello Everybody, Does anyone know what the @ symbol means in a csh script, if used with a variable assignment as below @ line = 1 why not just use.... set line=1 Many thanks rkap (1 Reply)
Discussion started by: rkap
1 Replies
Login or Register to Ask a Question