/dev/nullOnly integers will be compared he" />
Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to write the judgment condition when judging if statement in Shell programming. If the judgment variable a is an integer variable, the subsequent statement will be executed.
How to write the judgment condition when judging if statement in Shell programming. If the judgment variable a is an integer variable, the subsequent statement will be executed.

Change if [ "$tt" ~ '/^[1-9][0-9]*$/' ] to

if [ "$tt" -le 0 -o "$tt" -ge 0 ] 2>/dev/null

Only integers will be compared here. If $tt is not an integer, an error message will be generated and no comparison will be made. Then execute the action after else. The error message generated can be discarded using 2>/dev/null.

If it is an integer, compare within []. The -o in the middle means or, which means that if the integer $tt is less than or equal to 0 or greater than or equal to 0, it is true, and the action after then is executed