Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Question Problem in While loop in BASH

    Hi guys,

    I have written a big automation script for my company's servers. While writing script i found an issue with while loop and i am not able to find explanation for it, although i have found another way to work around it, but still i wonder what might be the reason which gives the result unexpected, Please take a look on the below piece of Bash code...

    #!/bin/bash
    flag=0
    cat /tmp/instanceid.txt | while read id eip garbage
    do
    if [ "$id" = "i-ab7178c1" ]
    then
    ec2-start-instance -O accesskey -W key "i-ab7178c1"
    flag=1
    break
    fi
    done
    echo "The flag contains value = $flag"

    Its a small part of my code which i wanted to use to automate the starting of instances.

    Assume that the if condition is fine and satisfies the condition once while executing the code and there in no problem the "ec2-start-instance" command, we can expect that the output of the echo as "The flag contains value = 1"; but if u execute the above code, you wont get value of the flag variable as 1 ever it will always print 0 irrespective of if condition.

    Can anybody explain why it is so.

    Thanks in advance.

    Regards,
    Santosh




  2. #2
    Join Date
    Dec 2011
    Posts
    71
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Rep Power
    2
    Indeed! You've found a feature of bash that I stumble over occasionally and annoys me a lot.
    It's something to do with piping into the loop. It seems to make it a separate process or something.

    I solve it by putting the loop in a function. I didn't test this, but it looks like it should work:
    Code:
    #!/bin/bash
    
    KEY=i-ab7178c1
    
    function file_has_key()
    {
        cat $1 | while read id eip garbage
        do
            if [ "$id" = "$KEY" ]; then
                return 1;
            fi
        done
        return 0;
    }
    
    flag=0
    if file_has_key /tmp/instanceid.txt; then
        ec2-start-instance -O accesskey -W key "$KEY"
        flag=1
    fi
    
    echo "The flag contains value = $flag"

  3. #3
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    KenJackson, thanks for your response

    We can work-around this problem in so many ways, one of which you have provided.

    I actually wanted to know the reason why its happening here.
    As you said it may be problem with the pipe, but what is the problem exactly?

 

 

Similar Threads

  1. Reinstallation problem
    By eschamp in forum Getting Started
    Replies: 8
    Last Post: 08-15-2012, 01:06 PM
  2. Problem with cdma modem
    By zuhudfm in forum Linux Networking
    Replies: 0
    Last Post: 05-31-2012, 10:13 AM
  3. Bash alias and its usefulness
    By ehansen in forum Linux-Howtos
    Replies: 2
    Last Post: 04-24-2012, 10:27 AM
  4. BASH Programming
    By Rory Glenn Pascua in forum General Linux
    Replies: 6
    Last Post: 01-20-2012, 11:19 AM
  5. How to Debug Bash Scripts
    By Fred in forum Linux News
    Replies: 0
    Last Post: 11-11-2011, 03:04 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
           








Check out Linux Central for Linux software and other goodies!





» Stats

Members: 3,539
Threads: 3,912
Posts: 9,423
Top Poster: Fred (1,486)
Welcome to our newest member, Ian Marsteller

» Links



Powered by vBadvanced CMPS