UNIX - Until loop condition expects integer from a string? - unix

So, I have some code which is supposed to check whether a pre-check sequence has completed before I begin patching a server. I'm doing this by using an until loop and as the condition, I have a flag set to true. When the flag is set to false that means that the pre-checks are done and I can now proceed out of the until loop.
The problem is I get stuck in an infinite loop and I get an error message saying [: true: integer expression expected
I continually call a URL to conduct my checks, and it returns a file which I save. An example extract from that file would be:
inProgress":true,"status":"IN_PROGRESS","preCheckMessages":[],"precheckResultItems":[]}
The part I need to extract from this is the value from the inProgress field (which could be 'true' or 'false'). I then use this value to compare to my flag. So I extract the value from the inProgress field.
The section of code causing me an issue is as follows:
inProgress="true"
until [ "$inProgress" -eq "false" ]
do
long_URL_goes_here > jobIdCheck.txt
inProgress=$(grep -o 'inProgress.*$' jobIdCheck.txt)
word="inProgress\":"
tempVar=${inProgress##${word}}
inProgress=${tempVar%%,*}
echo inProgress value = ${inProgress}
done
echo "Pre-checks complete. Ready to apply patch"
This is where I hit the infinite loop with the error message saying [: true: integer expression expected. (I never see the "Pre-checks complete message")
But I don't understand why it would be expecting an integer? Surely the string comparison should suffice?
Is the fact that the value true in the inProgress field ISN'T encapsulated in double quotes important?
Any help will be greatly received.

Related

Run Keyword If has invalid syntax

Why is the following Robot statement complaining about 'Convert To Integer' keyword is invalid syntax? Thanks
Run Keyword If Convert To Integer ${packets_2} <= Convert To Integer ${packets_1}
... FAIL ${\n}[FAILED] Packets 2 not greater than packets 1.
... ${\n}packets_time1: ${packets_1} ${\n}packets_time2: ${packets_2}
You cannot call a keywords as the condition for Run Keyword If. The first argument is expected to be a python expression. Since you're trying to do a comparison of an integer, you can do that directly in the expression like so:
Run keyword if int('${packets_2}') <= int('${packets_1})
... FAIL \n[FAILED] Packets 2 not greater than packets 1
The other problem is that you are supplying two other arguments: ${\n}packets_time1: ${packets_1} and ${\n}packets_time2: ${packets_2}. It's not clear what you think those are for. I'm guessing you want them as part of the error message. If that's the case, it will have to be all on one line or else robot will think they are extra arguments to the FAIL keyword.
Run keyword if int('${packets_2}') <= int('${packets_1})
... FAIL \n[FAILED] Packets 2 not greater than packets 1\n$packets_time1: ${packets_1}\npackets_time2: ${packets_2}

KEYWORD_SET in IDL

I am new to IDL and find the KEYWORD_SET difficult to grasp. I understand that it is a go no go switch. I think its the knocking on and off part that I am having difficulty with. I have written a small program to master this as such
Pro get_this_done, keyword1 = keyword1
WW=[3,6,8]
PRINT,'WW'
print,WW
y= WW*3
IF KEYWORD_Set(keyword1) Then BEGIN
print,'y'
print,y
ENDIF
Return
END
WW prints but print, y is restricted by the keyword. How do I knock off the keyword to allow y to print.
Silly little question, but if somebody can indulge me, it would be great.
After compiling the routine, type something like
get_this_done,KEYWORD1=1b
where the b after the one sets the numeric value to a BYTE type integer (also equivalent to TRUE). That should cause the y-variable to be printed to the screen.
The KEYWORD_SET function will return a TRUE for lots of different types of inputs that are basically either defined or not zero. The IF loop executes when the argument is TRUE.
Keywords are simply passed as arguments to the function:
get_this_done, KEYWORD1='whatever'
or also
get_this_done, /KEYWORD1
which will give KEYWORD1 the INT value of 1 inside the function. Inside the function KEYWORD_SET will return 1 (TRUE) when the keyword was passed any kind of value - no matter whether it makes sense or not.
Thus as a side note to the question: It often is advisable to NOT use KEYWORD_SET, but instead resort to a type query:
IF SIZE(variable, /TNAME) EQ 'UNDEFINED' THEN $
variable = 'default value'
It has the advantage that you can actually check for the correct type of the keyword and handle unexpected or even different variable types:
IF SIZE(variable, /TNAME) NE 'LONG' THEN BEGIN
IF SIZE(variable, /TNAME) EQ 'STRING' THEN $
PRINT, "We need a number here... sure that the cast to LONG works?"
variable = LONG(variable)
ENDIF

How to Assign a variable after running Run keyword If - else ?

I am trying to assign a variable - body ,depending on the status of another variable phonenumber_id. If the phonenumber_id is NULL, body gets assigned False.
But it doesnt seem to be working. It works only if he phonenumber_id is not NULL.
${body}= Run keyword if '${phonenumber_id}'!='NULL' Set variable TRUE
... Else Set Variable FALSE
Not sure what i am doing wrong.
The keyword Set Variable If will set a variable based on a given condition
${body}= Set Variable If '${phonenumer_id} != 'NULL'
... ${True} ${False}
You got it almost right - just mistyped the ELSE - it must be in capital letters, to be considered a part of the Run Keyword If. So in your particular case, it should've been:
${body}= Run keyword if '${phonenumber_id}'!='NULL' Set variable TRUE
... ELSE Set Variable FALSE
For the simple case of just setting a new constant value though, #ILostMySpoon answer is good enough - and more "readable".
In general, for someone stumbling on this post, the Run Keyword If combined with ELSE Set Variable is a very powerful construct to set/change a variable - based on the fact that it not only runs a keyword(s) conditionally, but also propagates its return values back to the stack.
Consider this example:
${var}= Run Keyword If ${bool condition} Do Some Action Returning A Value
... ELSE Set Variable ${var}
In it {var} will be set to the return value of Do Some Action Returning A Value only if ${bool condition} evaluates to true, and will keep its old value otherwise.
Another artifical but less abstract example:
${value}= Run Keyword If ${should be int} Convert To Integer ${value}
... ELSE IF ${should be float} Convert To Number ${value}
... ELSE Set Variable ${value}

Classic ASP - Returning a value from a function

I have a function in Classic ASP which receives a value and in the function it runs through various CASE statements. What I want to do is return the value of that function into a variable that I can use.
For example
Function whichNumber(intNumber)
SELECT CASE intNumber
CASE 1
whichNumber = "Yes"
CASE 2
whichNumber = "No"
END SELECT
END Function
When I call that function in the webpage via:
whichNumber(intNumberToFunction)
I am expecting then the variable "whichNumber" to be either "Yes" or "No". So the next line in the code I use:
strNumberText = whichNumber
However that results in the error:
Wrong number of arguments or invalid property assignment: 'whichNumber'
If I go back and in the function put the following at the end of the function:
Response.Write whichNumber
It correctly writes either "Yes" or "No" to the screen. Is there anyway to return the value of the function to a variable to use outside of the function?
Hopefully this makes sense! Reading around the web it seems it might not be possible to return a value from a function like this?
Do you not need to write
strNumberText=whichNumber(intNumberToFunction)
The error message is telling you that you have called the function without passing the parameter

How do I properly perform modulus operations in Batch?

I'm trying to write a batch file that performs operations depending on the result of a modulus operation performed on a set variable. However, I can't seem to get it quite right.
To first of all test my syntax for the mathematical operation, I've been trying to get a simpler script to produce desired results.
:START
SETLOCAL
SET /P Input-Num="Input Number: "
SET /A Input-Num=%Input-Num% %% 2
ECHO %Input-Num%
ENDLOCAL
PAUSE
:END
If I input 5, the expected output is 1. However, instead I get a message saying Missing operator. and then it outputs 5.
What am I doing wrong here?
Using SET /P is your problem, as 5 is no longer treated as a numerical value. Your example as above works as expected

Resources