6 ways to determine if a string is a number in the shell share the _linux shell

Source: Internet
Author: User

This article mainly introduces the "shell judge string is a number", mainly related to the shell to determine whether the string is a digital content, for the shell to determine whether the number of figures interested students can refer to.

#!/bin/bash # # method 1 A=1234;echo "$a" | [-N ' ' sed-n '/^[0-9][0-9]*$/p '] && echo string A is numbers the first-n is the test flag of the shell, to the back string "' Sed-n '/^[0-9][0-9]*$/
   P ' "To test, if not NULL, the result is true.
  SED defaults to display all input line information, the SED "-n" option is to let sed not display, but only to show what we need: that is, the following expression matches the row, this is done by adding the "P" command in the expression.  
/^[0-9][0-9]*$/his meaning is to match at least one digit line # # method 2, yes, but not bash implementation, is using GREP's regular #if grep ' ^[[:d igit:]]*$ ' <<< '; then 
# echo ' is number. ' 
#else # echo ' No. ' #fi # # Method 3 #if ["-GT 0] 2>/dev/null; then # echo" is number. " 
#else # echo ' No. ' #fi # # method 4,case #case "in # [1-9][0-9]*] # echo" is number. " 
#    ;; 
#  *)  
#    ;; #esac # # Method 5,awk #echo $1| awk ' {print ($0~/^[-]? ( [0-9]) +[.]? ([0-9]) +$/)? " Number ":" String "} ' # # method 5,awk #if [-N ' $ (echo $1| 
Sed-n "/^[0-9]\+$/p") "];then # echo" is number. #else # echo ' No. ' #fi # # method 6,expr Expr $ "+" &>/dev/null if [$?-eq 0];then echo "is Numbe R "Else echo" not Number "fi  

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.