How the shell determines if the variable entered by the user from the keyboard is a number

Source: Internet
Author: User

There is no direct command in the bash shell to determine whether a variable is a pure number, then we are using an indirect method:

Problem Description:

Read-t 30-p "Please a number:" NUM

Use the Read command to accept the user from the keyboard input a variable, stored in the variable num, sometimes the user blindly input a pass, or do not input, this will affect the execution of the script, Gu judge beforehand, better is also necessary, then how to judge?

Method One:

To further process num variables:

String= ' echo ${num}|sed ' ss^.*[0-9] $SS ' give ${num to the SED command to process, replace any number at the beginning of any number with an empty, and then deal with the result of the substitution. That

${string} is empty then ${num} is a pure number, otherwise it is not a pure number ...


Example Demo:

#!/bin/bash

Read-t 10-p "Please input a number:" NUM


String= ' echo ${num} |sed ' ss^[0-9].* $SS '


[[-Z ${string}]] && echo true | | echo False

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/87/C5/wKioL1fhYoOwCFZNAAAuXx9n4n4043.png-wh_500x0-wm_3 -wmp_4-s_1142399211.png "title=" Qq20160921002223.png "alt=" Wkiol1fhyoowcfznaaauxx9n4n4043.png-wh_50 "/>

EXECUTE as follows:

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/87/C9/wKiom1fhYuKDyLQgAAA_H7z0d3k896.png-wh_500x0-wm_3 -wmp_4-s_2356161499.png "title=" 111.png "alt=" Wkiom1fhyukdylqgaaa_h7z0d3k896.png-wh_50 "/>


Method Two: Also recommended is a

Match ${num} to digital mode i.e. =~^[0-9]+$

Example Demo:


#!/bin/bash

Read-t 10-p "Please input a number:" NUM


[[${num} =~ ^[0-9]+$]] && echo true | | echo False


650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/87/C5/wKioL1fhZD6hYdYFAAAjqjTJyWw462.png-wh_500x0-wm_3 -wmp_4-s_3196592928.png "title=" 33.png "alt=" Wkiol1fhzd6hydyfaaajqjtjyww462.png-wh_50 "/>


Execution Result:


650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/87/C9/wKiom1fhZFWzjkGaAABUBB2rEVM756.png-wh_500x0-wm_3 -wmp_4-s_1426919299.png "title=" 22.png "alt=" Wkiom1fhzfwzjkgaaabubb2revm756.png-wh_50 "/>






This article from the "Linux-related technology" blog, declined to reprint!

How the shell determines if the variable entered by the user from the keyboard is a number

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.