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
let "numToAppend+1"doesn't do what you think it does: it only returns success. Instead, you probably want((++numToAppend)). Also, your lineclientToCheck+=_$numToAppendwill keep on appending: so before thewhileloop you haveclientToCheckthat expands toirabinowitz_tlv-cc-lx64_806_1, then, in thewhileloop you'll haveirabinowitz_tlv-cc-lx64_806_1_2,irabinowitz_tlv-cc-lx64_806_1_2_3, etc. (assumingnumToAppendgets incremented properly). Your design is also really weird.fixClientNameyou're never callingisClientExist.tempStr="doesn\'t exist"