0

Hello~ I've been trying to crack my head solving this for half a day.

INPUT=lookup.txt
IFS=":"
while read vTitle vAuthor vPrice vStock vSold
do
    tempString="$vTitle:$vAuthor:$vPrice:$vStock:$vSold"
    #echo "$tempString"
    while [ "$iTitle" == "$vTitle" ]; do
        ./testcase.sh

    done 

done < $INPUT

Testcase.sh contains a script for a sub-menu. When I run this script, my sub-menu keeps repeating, as in a recursive loop. Any idea how to solve this issue? EDIT# Sorry guys $iTitle contains user-input. And a sample of testcase.sh as follows:

#! /bin/bash
menu=""
until [ "$menu" = "f" ]; do
echo "  a.) Update title"
echo "  b.) Update author"
echo "  c.) Update price"
echo "  d.) Update quantity available"
echo "  e.) Update quantity sold"
echo "  f.) Back to main menu"
echo " Make selection"
read menu
case $menu in
a)
echo "a" ;;
b)
echo "b" ;;
c)
echo "c" ;;
d)
echo "d" ;;
e)
echo "e" ;;
f)
break ;;
*) ;;
esac
done

I realised my question isn't clear. I've got a user input $iTitle the first code excerpt checks lookup.txt for the same $vTitle. When successful I want the program to run the testcase.sh. Currently it does run, but testcase.sh runs recursively.

4
  • What is the value of $iTitle? I do not see it defined anywhere in the sample code... Commented Feb 1, 2014 at 16:01
  • What does testcase.sh look like? Commented Feb 1, 2014 at 16:02
  • please update your question to include what is inside testcase.sh (or small example that simulates the problem). If it's code that uses the shell's select keyword, you can try sourcing the file instead, . ./testcase.sh, but if it has a shebang header, you may need to remove that. Lots of other stuff comes to mind. Lets see what's in ./testcase.sh first ;-) . good luck. Commented Feb 1, 2014 at 16:02
  • you mean infinite loop don't you? A recursive function may conceptually call itself for infinity, but in a computer, fn_a->fn_a->fn_a->INF will run out of computer resources pretty quickly and crash. An infinite loop as the result of while condition always being true means the same block is exectued over and over again, but with building up a "stack" of calls and doesn't keep consuming more and more computer resources; just your time ;-). Good luck. Commented Feb 1, 2014 at 16:26

1 Answer 1

1
while [ "$iTitle" == "$vTitle" ]; do
        ./testcase.sh
done

In this block statement, neither $iTitle nor $vTitle changes. and thats why the loop doesn't end. May be you want to replace it with

if [ "$iTitle" == "$vTitle" ]; then
            ./testcase.sh
fi

And also, could not see a value for$iTitle in the code.

Sign up to request clarification or add additional context in comments.

2 Comments

:) $iTitle is a user input. So in other words I don't want either $iTitle/$vTitle to change i want to compare them. But while comparing them I hit a recursive loop.
the comparison is done in while loop, which loops until the expression becomes false, in this case, say if both the values match, then the loop never ends.

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.