Sponsored Content
Full Discussion: Loops & Variable
Top Forums UNIX for Dummies Questions & Answers Loops & Variable Post 302948446 by bakunin on Monday 29th of June 2015 06:26:07 PM
Old 06-29-2015
Quote:
Originally Posted by Xterra
Now, what if I would like to use a double nested?
Code:
awk -v"REP=${TMPArray$y[$e]}" [...]

Such an expansion will not work, regardless of array content or files to process. The reason is that all the variables are expanded at the same time when the shell tries to digest an input line.

Consider the following example:

Code:
a="/some/where"
ls -l $a

What the shell does is: it first notices that "$a" is a variable and expands that. In the example "a" is given some content before but if this would be not the case "$a" would just expand to the empty string. Then it replaces the variable by its content:

Code:
ls -l /some/where

only then this resulting line is executed.

Now, if several variables are in a single commandline they are all expanded at the same time. This means, that any construct where one variables expansion is necessary to expand another variable will fail:

Code:
${TMPArray$y[$e]}

Here it would be necessary to first expand "$y" to determine the name of the array first and only then look up array element "$e" in this array.

Actually there is a way to achieve this: eval This command does nothing itself, it just tells the shell to start over the parsing process again. For instance:

Code:
a='$b'
b="hello world"

echo $a

will not bear the desired result of first expanding "$a" to "$b" and then expand "$b" to "hello world" for the reasons given above. But:

Code:
a='$b'
b="hello world"

eval echo $a

will do the trick.

Fortunately you do not need eval (which one should try to avoid the same way one avoids dynamite: it is a potent but dangerous device and deadly if not handled with utmost care). I do not know about bash (which you seem to prefer scripting in for reasons i do not entirely understand) but in Korn Shell (ksh), which is available for all systems i know there are multidimensional arrays:

Code:
x=( ( 11 12 13 ) ( 21 22 23 ) ( 31 32 33 ) )
for i in {0..2} ; do
     for j in {0..2} ; do
          echo ${x[$i][$j]}
     done
done
11
12
13
21
22
23
31
32
33

I hope this helps.

bakunin
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

korn shell "loops & arrays"

Hi, I am trying to write a script which will loop until a certain action has been performed. I have two files i would like to compares. For example: file1 has a list of user ids (about 900) from the company's e-mail server. file2 has a list of user ids (about 50 or so) from... (7 Replies)
Discussion started by: muzica
7 Replies

2. Shell Programming and Scripting

search & replace in variable

Can I use search & replace in any variable? Suppose I have one variable named var1 which holds value "abcabc" I need to search 'a' in var1 and want to replace with 'x' like 'xbcxbc'. Is it possible? Can you provide me an example? Malay (3 Replies)
Discussion started by: malaymaru
3 Replies

3. Shell Programming and Scripting

scripting headache... loops & variables

Surely there's an easier way to do this, lets see if anyone knows! I am new to scripting so go easy on me! I have the following script and at the moment it doesn't work and I believe the problem is that I am using a while loop within a while loop. When I run the script using sh -x I can see... (6 Replies)
Discussion started by: StevePace
6 Replies

4. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

5. UNIX for Dummies Questions & Answers

Passing KSH variable to AWK with two loops

Hi, I need some help to figure out why an outer for loop KSH variable does not decode in AWK but inner for loop does. Below is my code, If I hard code variable 'SUBSEQ' in AWK it works but if I try to pass the SUBSEQ from KSH, it does not and when I pass the variable 'NAM' from KSH it works: I... (1 Reply)
Discussion started by: chowdhut
1 Replies

6. Shell Programming and Scripting

sed -i '7 c\$variable' file ....(?!@#$%^&*!)

I have tried everything I can think of to get sed to change line N of a file to the contents of a variable. I have Googled the Internet, and I find lots of people telling how to use variables with the "Substitute" command, but no one telling how to use variables with the "Change" command. I... (4 Replies)
Discussion started by: Mr.Lauren
4 Replies

7. Shell Programming and Scripting

Re-assign variable's value through which FOR LOOP loops

Hi, I've a requirement where I want to re-assign the value in the variable through which FOR LOOP loops. For e.g. Snippet of code --------------- for i in $var do echo $i >> $tempFile var=`echo $another_var | awk -F" " '{print $1}'` done I am re-assigning var so... (2 Replies)
Discussion started by: dips_ag
2 Replies

8. UNIX for Dummies Questions & Answers

Bash loops and variable scope

Hi All, I've been researching this problem and I am pretty sure that the issue is related to the while loop and the piping. There are plenty of other threads about this issue that recommend removing the pipe and using redirection. However, I haven't been able to get it working using the ssh and... (1 Reply)
Discussion started by: 1skydive
1 Replies

9. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

10. Shell Programming and Scripting

Bash: How to use read with conditions & loops

Hello, Below I try to control that the input is good an IP : #!/bin/bash cp /home/scripts/choice_interfaces.txt /home/scripts/interfaces.txt chmod 644 /home/scripts/interfaces.txt echo -e "Please enter the network informations into the /etc/network/interfaces file, complete them below... (9 Replies)
Discussion started by: Arnaudh78
9 Replies
All times are GMT -4. The time now is 04:13 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy