LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   for loop is not working (https://www.linuxquestions.org/questions/programming-9/for-loop-is-not-working-4175482090/)

bkcreddy17 10-25-2013 02:50 AM

for loop is not working
 
Hi,

I am not sure why my for loop is not executing.

Code:

#!/bin/bash

URL="google.com facebook.com flipkart.com"
SERVICE="mail chat buy"

VAR1=( $URL )
echo ${#VAR1[@]}
VAR2=( $SERVICE )
echo ${#VAR2[@]}

if [ ${#VAR1[@]} -eq ${#VAR2[@]} ]
then
        COUNT=${#VAR1[@]}
        echo $COUNT

        for j in {1..$COUNT}
        do
                echo $j
        done

fi


bkcreddy17 10-25-2013 02:52 AM

I am getting the below output. I never faced this type of problem.

$ sh a.sh
3
3
3
{1..3}

$ for i in {1..10};do echo $i;done
1
2
3
4
5
6
7
8
9
10

same works normally..

pan64 10-25-2013 02:52 AM

I'm really sure the for loop is working as it was planned to work (by design). Probably it will not do what you have tried to implement....
use set -xv at the beginning of the script (as second line) and you will see what's happening.

also add the second loop (posted by bkcreddy17): for i in {1..10};do echo $i;done

and you will see:
$COUNT was not evaluated "in time", the for cycle got only one value, the string '{1..$COUNT}' and it was evaluated only during the execution of the echo command.

bkcreddy17 10-25-2013 02:57 AM

Code:

]$ sh a.sh
URL="google.com facebook.com flipcart.com"
+ URL='google.com facebook.com flipcart.com'
SERVICE="mail chat buy"
+ SERVICE='mail chat buy'

VAR1=( $URL )
+ VAR1=($URL)
echo ${#VAR1[@]}
+ echo 3
3
VAR2=( $SERVICE )
+ VAR2=($SERVICE)
echo ${#VAR2[@]}
+ echo 3
3

if [ ${#VAR1[@]} -eq ${#VAR2[@]} ]
then
        COUNT=${#VAR1[@]}
        echo $COUNT

        for j in {1..$COUNT}
        do
                echo $j
        done

fi
+ '[' 3 -eq 3 ']'
+ COUNT=3
+ echo 3
3
+ for j in '{1..$COUNT}'
+ echo '{1..3}'
{1..3}


pan64 10-25-2013 02:59 AM

sorry, I edited my previous post, added explanation

bkcreddy17 10-25-2013 03:03 AM

not sure why it is picking up as '{1..3}'. any idea?

bkcreddy17 10-25-2013 03:05 AM

pan,

how can i make, $COUNT evaluated "in time" ?

bkcreddy17 10-25-2013 03:16 AM

Thanks. I changed for loop pattern.

Code:

#!/bin/bash
URL="google.com facebook.com flipcart.com"
SERVICE="mail chat buy"

VAR1=( $URL )
echo ${#VAR1[@]}
VAR2=( $SERVICE )
echo ${#VAR2[@]}

if [ ${#VAR1[@]} -eq ${#VAR2[@]} ]
then
        COUNT=${#VAR1[@]}
        echo $COUNT
        for (( j=1 ; j<=$COUNT ; j++ ));do
                echo $j
        done

fi



---------- Post added 25th Oct 2013 at 01:46 PM ----------

This started working.

pan64 10-25-2013 03:24 AM

so you found the way. (if you really want to say thanks just press YES)

onebuck 10-25-2013 08:04 AM

Member Response
 
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.


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