LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 10-25-2013, 02:50 AM   #1
bkcreddy17
Member
 
Registered: Feb 2008
Location: India-Hyderabad
Distribution: RHEL and Fedora
Posts: 171

Rep: Reputation: 15
Unhappy 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

Last edited by bkcreddy17; 10-25-2013 at 02:53 AM.
 
Old 10-25-2013, 02:52 AM   #2
bkcreddy17
Member
 
Registered: Feb 2008
Location: India-Hyderabad
Distribution: RHEL and Fedora
Posts: 171

Original Poster
Rep: Reputation: 15
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..
 
Old 10-25-2013, 02:52 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,686

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
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.

Last edited by pan64; 10-25-2013 at 02:58 AM.
 
Old 10-25-2013, 02:57 AM   #4
bkcreddy17
Member
 
Registered: Feb 2008
Location: India-Hyderabad
Distribution: RHEL and Fedora
Posts: 171

Original Poster
Rep: Reputation: 15
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}

Last edited by onebuck; 10-25-2013 at 08:02 AM. Reason: add vbcode tags
 
Old 10-25-2013, 02:59 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,686

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
sorry, I edited my previous post, added explanation
 
Old 10-25-2013, 03:03 AM   #6
bkcreddy17
Member
 
Registered: Feb 2008
Location: India-Hyderabad
Distribution: RHEL and Fedora
Posts: 171

Original Poster
Rep: Reputation: 15
not sure why it is picking up as '{1..3}'. any idea?
 
Old 10-25-2013, 03:05 AM   #7
bkcreddy17
Member
 
Registered: Feb 2008
Location: India-Hyderabad
Distribution: RHEL and Fedora
Posts: 171

Original Poster
Rep: Reputation: 15
pan,

how can i make, $COUNT evaluated "in time" ?
 
Old 10-25-2013, 03:16 AM   #8
bkcreddy17
Member
 
Registered: Feb 2008
Location: India-Hyderabad
Distribution: RHEL and Fedora
Posts: 171

Original Poster
Rep: Reputation: 15
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.

Last edited by onebuck; 10-25-2013 at 08:03 AM. Reason: add vbcode tags
 
Old 10-25-2013, 03:24 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,686

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
so you found the way. (if you really want to say thanks just press YES)
 
Old 10-25-2013, 08:04 AM   #10
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,922
Blog Entries: 44

Rep: Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158
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.
 
  


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
[SOLVED] for loop not working dth4h Linux - Software 3 05-20-2012 03:37 PM
[SOLVED] sed not working for loop say_hi_ravi Programming 3 04-05-2012 07:03 AM
how to prove working of RS-232 full modem working PC to PC and LOOP BACK getasif Programming 2 08-20-2011 09:58 AM
[SOLVED] bash loop not working as expected... replica9000 Linux - Software 10 04-24-2011 09:08 AM
Bash for loop not working cryptinitedemon Linux - General 5 04-24-2010 10:44 AM

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

All times are GMT -5. The time now is 06:45 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