0

I have a function with a while loop that should run untill it one of the string will be a substring of my second string. The thing is that the while loop is infinity, and i double checked my condition and it shouldn't go inside the while loop with the paramteres that i've entered. Here is my 2 functions:

## check if the client exist
## if exists return 1 else return 0
function isClientExist () {
        clientToCheck=irabinowitz_tlv-cc-lx64_806
        checkClient=$(p4 client -o -t $clientToCheck 2>&1)
        tempStr="doesn\'t exist"
        if [[ $checkClient != *"$tempStr"* ]]; then
                echo The client exist
                flag=1
        else
                echo the client doesnt exist
                clientToCreate=$clientToCheck
                flag=0
        fi
        return $flag
}
## Fixing the client name by appeding  _number to the client name
function fixClientName () {
        echo fixing the client name...
        numToAppend=1
        tempClientToCheck=$clientToCheck
        echo the temp client to check is: $tempClientToCheck
        clientToCheck+=_$numToAppend
        echo  the client to check is: $clientToCheck
        echo try number $numToAppend
        sleep 20
        while [[ $checkClient != *"$tempStr"* ]]; do

        #       let "numToAppend+1"
                ((++numToAppend))
                clientToCheck=$tempClientToCheck
                echo the client to check in the loop before appending  is: $clientToCheck
                clientToCheck+=_$numToAppend
                echo the client to check in the loop after appending is: $clientToCheck
                echo try number $numToAppend
                sleep 20

        done
        clientToCreate=$clientToCheck
        echo the client to create is $clientToCreate
}

#main
isClientExist
if [ $? -eq 1 ]; then
        fixClientName
fi
10
  • You have several problems here: for one, let "numToAppend+1" doesn't do what you think it does: it only returns success. Instead, you probably want ((++numToAppend)). Also, your line clientToCheck+=_$numToAppend will keep on appending: so before the while loop you have clientToCheck that expands to irabinowitz_tlv-cc-lx64_806_1, then, in the while loop you'll have irabinowitz_tlv-cc-lx64_806_1_2, irabinowitz_tlv-cc-lx64_806_1_2_3, etc. (assuming numToAppend gets incremented properly). Your design is also really weird. Commented Feb 1, 2015 at 16:03
  • Can you suggest another design ? Commented Feb 1, 2015 at 16:06
  • First fix the errors I mentioned and see if it helps. Commented Feb 1, 2015 at 16:07
  • Also in fixClientName you're never calling isClientExist. Commented Feb 1, 2015 at 16:08
  • 1
    tempStr="doesn\'t exist" Commented Feb 1, 2015 at 16:46

1 Answer 1

1

You should not backslash-escape the ' in:

tempStr="doesn\'t exist"

That will never match the string you expect, so [[ $checkClient != *"$tempStr"* ]]; will always succeed.

It won't match, because inside a double-quoted string, \' is, literally, \'. So the backslash would have to be in the message for the match to succeed.

Use one of the following:

tempStr="doesn't exist"
tempStr=doesn\'t\ exist
Sign up to request clarification or add additional context in comments.

4 Comments

@user3502786: You should learn how to use function parameters ($1, $2, ...) and local variables. And also, remember that if statement; tests whether statement succeeds or fails, so there is no need to play games with [[ and $? if you just want to know if a command succeeded or failed. But all of that belongs on codereview.stackexchange.com
@user3502786 if it “works” it's because isClientExist succeeds and the if branch is never executed. In a case where isClientExist fails, you'll run into an infinite loop since, as I said in a comment, your function fixClientName never updates checkClient, so you'll still get an infinite loop in that case.
I could place the fixClientName function inside the isClientExist function
The while loop still not working, it's never exisitng from the program, any idea how to solve it ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.