problems calling out variables in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problems calling out variables in a loop
# 1  
Old 07-31-2009
problems calling out variables in a loop

good afternoon forums.
i have a problem that ive been trying to work out all morning and cant seem to get my head around it.
what i have in my script is individual letters saved in different variables. so if the word unix was saved then 'u' would be stored in the variable 'wvar1' 'n' in 'wvar2' 'i' in 'wvar3' and x in 'wvar4'.
what i want to do is loop through each of these to see wether an inputed letter 'in1' is the same as what is in each variable, one at a time. I have come up with the following script.
Code:
1)for (( i = 0; i <= 4; i++ ))
2)do
3)rdm=$(($rdm + 1))
4)testvar=$(eval echo $wvar$rdm)
5)if [[ $in1 = $testvar ]]
6)then
7)eval xnum$rdm=$in1
8)fi

1-makes the code loop 4 times
3-adds 1 to the variable rdm each time it loops around
4-testvariable changes each time it goes through the loop to be equal to wvar1 the first time it loops, wvar2 for the second time etc...
5-if the inputed character is equal to the saved variable then do the next command
7-creates a new variable which is equal to the inputed character

I hope i explained my problem well enough for some genius to figure out whats wrong with it. I have been on this same problem for a day and a half now...
# 2  
Old 07-31-2009
This might be or not a nice exercise but why don't you just compare the entered string with a string stored in a variable instead of cycling through every letter?
# 3  
Old 07-31-2009
how would i go about doing that then?

if a word is stored in the variable 'word'?
# 4  
Old 07-31-2009
Your loop is not looping 4 times, it will do 5 times.
# 5  
Old 07-31-2009
the number of times it loops wont really affect the outcome that i am getting, which now i have realised i forgot to put into the first post.

when i echo out what is in the xnum variables i get a number 1 in the first one, 2 in the second one and so on.

this is not what i was expecting to get as an output
# 6  
Old 07-31-2009
Where are you changing the xnum...somehow I am unable to understand your problem...sorry.
# 7  
Old 07-31-2009
in line 7 i do this
Code:
7)eval xnum$rdm=$in1

what i want that to do is create a variable xnum1 for the first time the loop hapens, then the second time xnum2.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling an interactive perl script from within a while-read loop

Hi, I have a perl script that prompts for a user to enter a password before doing what it does. This works well if I call it directly from a bash script #!/bin/bash /path/to/perl/script $arg1 $arg2 But, when I try to enclose this within a while read loop, the perl script is called but... (1 Reply)
Discussion started by: prafulnama
1 Replies

2. Shell Programming and Scripting

Calling variables in case

Hey guys, I got a little problem with calling variables in my case This is a simple version of my script: var1=1 var2=2 while do case $1 in "a") var1=$var2; echo "1"; echo "var1: $var1"; echo "var2: $var2";; "b") var2=5; echo "2"; (1 Reply)
Discussion started by: Miki1579
1 Replies

3. Web Development

Permission problems calling Ruby from php script

I'm just getting my feet wet with web development, so hopefully this is a simple thing I'm overlooking, but so far I'm stumped. I have a php script that calls Ruby via exec(). This works fine in my test environment, but when I moved it to my production environment I run into a permissions... (7 Replies)
Discussion started by: dantes990
7 Replies

4. UNIX for Dummies Questions & Answers

calling process and going back to the main loop

hi everyone , i want to read an option and depending on the option call the program .For ex #! /bin/ksh export JAVA_HOME=/home/oracle/jdk1.6.0_20 echo " Please enter mod-modeler, dev - sqldeveloper" read choice if ; then echo ' SQL DEVELOPER IS STARTING NOW ... ' cd... (0 Replies)
Discussion started by: kdev
0 Replies

5. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

6. Shell Programming and Scripting

for loop, calling and editing multiple files inside

hey guys, I'm trying to call and modify multiple files inside the for loop, i can't get it to work... ------------------------ AFILE=/dir/a_file.txt BFILE=/dir/b_file.txt CFILE=/dir/c_file.txt ADESTFILE=/dir/a_dest_file.txt BDESTFILE=/dir/b_dest_file.txt... (6 Replies)
Discussion started by: DeuceLee
6 Replies

7. Shell Programming and Scripting

Is there a better way I could have run this loop. (For loop with two variables)

Sorry for such a dreadful title, but I'm not sure how to be more descriptive. I'm hoping some of the more gurutastic out there can take a look at a solution I came up with to a problem, and advice if there are better ways to have gone about it. To make a long story short around 20K pieces of... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

8. Shell Programming and Scripting

calling sqlplus from within a for loop

i'm not new to programming, but i AM new to unix scripting. here's my deal. this works: #!/bin/ksh echo "HELLO" /oracle_home/bin/sqlplus username/password@MYDB<<EOF SELECT COUNT(*) FROM EMPLOYEES; EOF exit echo "GOODBYE" this doesn't: #!/bin/ksh echo "HELLO" for x in 1 2... (4 Replies)
Discussion started by: akosz
4 Replies

9. Shell Programming and Scripting

loop of killing and calling process

I make two process killing and calling one process I want to do this repeatedly many time between the interval(sleep) What will be the command to do this, can you make as one do use sleep between and run clear the memory(sh sync.sh) I need your advice, the script will be like this killps... (1 Reply)
Discussion started by: 197oo302
1 Replies

10. Programming

Problems calling external C routines from PL/SQL

Hi everybody! I'm not familiar with C programming in Unix, but I'm trying to make work an example to execute external procedures (developed in C) from PL/SQL. The example includes .c and .pc source files, which I have compiled succesfully. After that, links the .o files into .so to declare... (0 Replies)
Discussion started by: lwnorowski
0 Replies
Login or Register to Ask a Question