LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-25-2017, 05:48 AM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 113

Rep: Reputation: Disabled
Unable to use variable (i) from loop with FOR in a another variable


There are some codes in bash that break my head off and leave me without a clew why is not working .

On this code

Code:
#!/bin/bash
# random variable
var1="49274"

echo "Example variable is : $var1"

#simple loop with 5 steps

for i in 1 2 3 4 5
do

# This command will call the current variable and will search
# if $i from loop is in the var , loop starts in 1 and finishes in 5

test=$(echo "$var1" | grep -o $i)

# If grep did not found the $i value on var then set the variable
# ex$i ($i could be from 1 to 5) some text , else then found that
#number in the var and set the text in 2nd var different

if [[ -z "$test" ]]
then
ex$i="not there"
else
ex$i="is there"
fi
done

# Here the loop as finished and i expect that all the (ex$) variables
# have some text in there
# However it is popping an error

echo "1 = $ex1"
echo "2 = $ex2"
echo "3 = $ex3"
echo "4 = $ex4"
echo "5 = $ex5"
Yes , i know what you are thinking , you are probably thinking that i could set the final "echo" in code inside the loop like this and solved the problem

Code:
#!/bin/bash
# random variable
var1="49274"

echo "Example variable is : $var1"
#simple loop with 5 steps
for i in 1 2 3 4 5
do
# This command will call the current variable and will search
# if $i from loop is in the var , loop starts in 1 and finishes in 5
test=$(echo "$var1" | grep -o $i)

# If grep did not found the $i value on var then set the variable
# ex$i ($i could be from 1 to 5) some text , else then found that
#number in the var and set the text in 2nd var different
if [[ -z "$test" ]]
then
echo "$i= not there"
else
echo "$i=is there"
fi
done
But i need it to be like it is in the first example , setting a different variable .

-------------------------------------------------------------------

Final code fixed :


Code:
#!/bin/bash

var1="49274"

echo "Example variable is : $var1"

for i in 1 2 3 4 5
do

test=$(echo "$var1" | grep -o $i)

if [[ -z "$test" ]]
then
ex[$i]="not there"
else
ex[$i]="is there"
fi
done

echo "1 = ${ex[1]}"
echo "2 = ${ex[2]}"
echo "3 = ${ex[3]}"
echo "4 = ${ex[4]}"
echo "5 = ${ex[5]}"

Last edited by pedropt; 12-29-2017 at 03:12 AM.
 
Old 12-25-2017, 06:01 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,690

Rep: Reputation: 510Reputation: 510Reputation: 510Reputation: 510Reputation: 510Reputation: 510
Use an array (you could use eval but it will add complexity)

Code:
...
ex[$i]="not there"
...

echo "1 = ${ex[1]}"
echo "2 = ${ex[2]}"
echo "3 = ${ex[3]}"
echo "4 = ${ex[4]}"
echo "5 = ${ex[5]}"
 
1 members found this post helpful.
Old 12-25-2017, 06:45 AM   #3
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 113

Original Poster
Rep: Reputation: Disabled
Thank you very much keefaz
With that simple explanation i got rid of more than 50 lines of code in my project .
 
Old 12-25-2017, 07:47 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,690

Rep: Reputation: 510Reputation: 510Reputation: 510Reputation: 510Reputation: 510Reputation: 510
Just a note, you can avoid using grep with bash globing

Code:
if [[ $var1 == *$i* ]]
then
  ex[$i]="is there"
else
  ex[$i]="not there"
fi

# or
[[ $var1 == *$i* ]] && ex[$i]="is there" || ex[$i]="not there"

Last edited by keefaz; 12-25-2017 at 07:49 AM.
 
Old 12-25-2017, 08:28 AM   #5
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 113

Original Poster
Rep: Reputation: Disabled
Thanks , i will test it out later , for now i have to concentrate in a lot of code and logic that must be written .
 
Old 12-25-2017, 12:31 PM   #6
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,885

Rep: Reputation: 568Reputation: 568Reputation: 568Reputation: 568Reputation: 568Reputation: 568
It's not a good practice to have variable named 'test' or named the same as any shell reserved words or commands.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to set a variable for loop pedropt Programming 6 07-24-2017 04:43 PM
How to define a variable In a while loop? fatsheep Programming 16 07-30-2014 02:12 PM
variable incrementation in for loop ++nick++ Linux - General 3 10-20-2011 03:03 PM
incrementing variable in an until loop jeffreybluml Programming 2 05-12-2005 08:45 AM
getting a variable out of a WHILE loop. fhinkle Programming 8 02-22-2005 05:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration