using existing variables as counters in a "for" loop ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using existing variables as counters in a "for" loop ?
# 1  
Old 11-25-2010
using existing variables as counters in a "for" loop ?

Hi all,
I am rather new to shell programing
I am trying to run a loop through variables that already exist,
here is (a part of ) the code, variables param_kz etc already exist (in the same file, i do not run another script file etc) and have values assigned to them (strings, "y" or "n")

Code:
echo $param_kz
#counting the number of perturbations
paramnr=0
for pw in kx ky kz kB kCs
do
echo param_$pw
echo $[param_${pw}]
if [ $[param_${pw}] == "y" ] ; then paramnr=`expr $paramnr + 1`
fi
done
echo $paramnr
echo $param_kz

the echo $param_kz before the loop and after it is just to check if the (already existing) variable is what i set it to earlier in the code.
Seems in the loop it treats the param_${pw} as new variables, since echo $[param_${pw}] returns zeros (even though they are either "y" or "n").
The funny thing is that a similar construction works for a friend on his laptop. So, my question is, is there a way to do this the way i am trying, am i missing something, does the older version of bash somehow creates new variables in loops in this sense, or what ? I tried
Code:
for pw in "kx" "ky" "kz" "kB" "kCs"

, to no avail Smilie
Sure i can do it some other way, but it is annoying when you do not know why a seemingly good idea doesn't want to work ... Smilie
Thanks
Luk
# 2  
Old 11-25-2010
Here's an example to put you on the right direction:
Code:
param_kx="y"
param_ky="n"

paramnr=0

for pw in kx ky
do
  eval "var=\$param_${pw}"
  echo $var
  if [ "$var" = "y" ] then
    paramnr=$(( paramnr + 1 ))
  fi
done

This User Gave Thanks to Franklin52 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. AIX

How to make existing volume group "shared"?

We have a 2 node cluster in which only the primary actually mounts the shared VGs at any specific time. We recently added a volume group to the primary. * The disks in it are visible to both nodes, but the secondary does not know about the new VG. * The new VG is not a "shared volume group" *... (10 Replies)
Discussion started by: ridgetop01
10 Replies

5. Programming

extract the same format from existing excel file using " Spreadsheet::ParseExcel " module

Hi , can any one tell me,"How to extract the same format from existing excel file to new excel file " using Spreadsheet::WriteExcel or Spreadsheet::ParseExcel module ??? Example_pgm: Below program is used to read existing excel file..In this program "my $cell = $_;" line is used to... (0 Replies)
Discussion started by: kavi.mogu
0 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. UNIX for Dummies Questions & Answers

BASH: Interactive "cp" (and "mv") in a loop

Hi. I have a copy-file script (and a move-file script) that I recently tried to make interactive. I tested the former on three files from a text list, and watched to see what would happen. As the cp command was in a while/do/done loop, there was no pause for input: it wrote the file from the... (2 Replies)
Discussion started by: SilversleevesX
2 Replies

8. Shell Programming and Scripting

Question about special variables: "-" and "$_"

both ksh/bash support this 2 special variables, Is there any document for reference? 1) "-" is $OLDPWD 2) "$_" is last argument of previous command. (4 Replies)
Discussion started by: honglus
4 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question