Learning - Why does this bit of code fail


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Learning - Why does this bit of code fail
# 1  
Old 12-17-2010
Learning - Why does this bit of code fail

In bash:

Code:
for i in 1 2 3 4 5
do
    tmp_${i}=$i
    echo $i
done

It gives error on executing tmp_1=1, and so on...

Why? If I executed this in prompt, not from script, it shouldn't make a subshell should it?

Moderator's Comments:
Mod Comment please use "code" tags! thanks...

Last edited by DukeNuke2; 12-17-2010 at 08:52 PM..
# 2  
Old 12-17-2010
Use eval
Code:
 
eval "tmp_${i}=$i"


Last edited by anurag.singh; 12-17-2010 at 08:02 PM..
# 3  
Old 12-17-2010
Quote:
Originally Posted by StuartH
In bash:

for i in 1 2 3 4 5
do
tmp_${i}=$i
echo $i
done

It gives error on executing tmp_1=1, and so on...

Why?
It only evaluates variables and assignments once. So once it's done the ${i} substitution, it won't take a step back, start over, and see that you meant it to assign to the variable tmp_1. If you want it to rethink like that, you have to do so explicitly, with eval.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Memmove fail on 4 Gb+ raster under RHEL 64 bit

I'm rotating huge image rasters (1+ GB) using a 3-shear algorithm. I rotate 'in-place' after reallocating the raster. A necessary first step is reorganizing the image in the new raster dimensions that will exist after the rotation. RGBA pixels byte packed into uint32. The following code runs... (6 Replies)
Discussion started by: imagtek
6 Replies

2. Programming

64 bit code debugging using gdb and ddd

Hello I have built our application on AIX 7.1 as a 64 bit application. My queries are as follows: Can a 32bit gdb (v7.6) and ddd (data display debugger - v3.3.12), debug a 64bit executable ? If I have a small 64bit a.exe executable that seems to work. If I have a more complicated executable... (4 Replies)
Discussion started by: biju64
4 Replies

3. What is on Your Mind?

Code learning problems

Now I suffer from my programming learning, feeling it is difficult, ask friends how I'm going to learn the code, I am doing penetration now, occasionally need to programming but suffer from themselves, to ask I hope to find a teacher :):) (2 Replies)
Discussion started by: planesb
2 Replies

4. Programming

C code 1ULL and bit calculation

I found this block of C code to create combinations of A, T, C & G characters (DNA bases). Can anybody explain this code for me, especially with 1ULL<< and ? 16 for (x = 0; x < 1ULL << (2 * k); ++x) 17 { 18 for (i = 0, y = x; i < k; ++i, y >>= 2) 19 putchar ("ACGT"); 20 }... (3 Replies)
Discussion started by: yifangt
3 Replies

5. UNIX for Dummies Questions & Answers

Trying to understand a complex bit of code

Hi, To re-introduce myself, I'm a router guy trying to learn some scripting from the examples in my work place... In a ksh script, one of the script guys wrote the following and I am trying to understand it. I'm hoping someone can explain it to me. The script flow enters a case structure.... (5 Replies)
Discussion started by: Marc G
5 Replies

6. Shell Programming and Scripting

sort -t option causing code to fail need ASCII character

Hello, When I run this UNIX code without the -t option it gives me the desired results. The code keeps the record with the greatest datetime based on the key columns. I sort it first then sort it again with the -u option, that's it. I need to have a variable to specify an ASCII character... (2 Replies)
Discussion started by: script_op2a
2 Replies

7. Shell Programming and Scripting

Need to return fail or pass from shell script on the basis of pl/sql code execution

Hi guys, I am quite new in shell scripting. I am tring to promote some oracle jobs into control-M. In control-M, I am calling a script which establishes a connection with database and execute some procedures. Now I want if that PL/sql Block got failed script should return failure to... (2 Replies)
Discussion started by: alok1301
2 Replies

8. Solaris

Porting C++ 32-bit code on 64-bit Solaris

Hi, I am trying to convert 32-bit code to 64-bit. I have defined function int main() { int* l; size_t len1; fun(len1); return 0; } void fun(int* ptr) { cout<<"\nsizeof(ptr)"<<sizeof(ptr); } However while compiling getting error as : Error: Formal argument ptr... (2 Replies)
Discussion started by: amit_27
2 Replies

9. UNIX for Advanced & Expert Users

Migrating perl code from 32 to 64 bit OS

Hello all, I am migrating perl code from 32-bit Solaris 2.5 to 64-bit Solaris 9. On which part of code should I concentrate? And please suggest if there is any checklist for the same. Thanks in advance, Devesh. (3 Replies)
Discussion started by: devesh
3 Replies

10. Programming

32-bit code with gcc

I want to create a 32 bit shared library. I am using following commands 1. gcc -c test1.c 2. ld -b test1.o -o test1.sl But this creates 64 bit shared library. Unix version of the server on which I am working is HP-UX 11.11 (1 Reply)
Discussion started by: amol.chavan
1 Replies
Login or Register to Ask a Question