LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 04:48 AM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

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 02:12 AM.
 
Old 12-25-2017, 05:01 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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, 05:45 AM   #3
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

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, 06:47 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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 06:49 AM.
 
Old 12-25-2017, 07:28 AM   #5
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

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, 11:31 AM   #6
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

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


Reply



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 03:43 PM
How to define a variable In a while loop? fatsheep Programming 16 07-30-2014 01:12 PM
variable incrementation in for loop ++nick++ Linux - General 3 10-20-2011 02:03 PM
incrementing variable in an until loop jeffreybluml Programming 2 05-12-2005 07:45 AM
getting a variable out of a WHILE loop. fhinkle Programming 8 02-22-2005 04:43 AM

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

All times are GMT -5. The time now is 11:37 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
Open Source Consulting | Domain Registration