views:

21

answers:

2

hi dear friends

I have the following (part of big ksh script)

 while [[ $? -ne 0 ]]
  do

    print -n "ENTER VALID HOST IP"
     read IP
     CHECK_IP $IP   # CHECK_IP is afuntion that check if IP is valid and return 0 or 1
     done

as we see in the while the $? is always equal to 0 so we cant ask for the IP

the target of the loop is to ask IP address and activate CHECK_IP function in order to return 0 or 1

0 for valid IP

1 for BAD IP

but as I said in the begging I never asked about IP because $? always 0 (after "while" )

so my question

How to enable default 1 to $? so I will start the loop , or other suggestion?

lidai

+1  A: 

Run false first.

Ignacio Vazquez-Abrams
great solution whaaao very coooooooooooool
lidia
A: 

$? is the exit status of previous executed command. One cannot guarantee that $? always return 0.

Your code above says that 'while some error occurs, repeat'. In other word, the $? -ne 0 will guard/ensure your loop to stop when some command executed successfully.

I don't know what exactly your case is. But i guess you have a bunch of IPs where many of them are incorrect. The loop itself will check these IPs and will exit when an IP is correct.

The biggest question is whether your code will ever be able to enter the loop. If this is the case, the use of while-do itself is incorrect. Try do-while instead (i don't know whether this is available in your shell or not). By using do-while, you will be sure that your code will enter the loop at least 1 time.

jancrot