This article mainly introduces the "shell judge string is a number", mainly related to the shell to determine whether the string is a number of content, for the shell to determine whether the number of figures interested students can refer to.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the |
; > #!/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 shell's test flag, 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 of the line # # method 2, which can, but not be implemented by bash, 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 Nu Mber. "#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 $" + "&A Mp;>/dev/null if [$-eq 0];then Echo ' is number ' else echo ' not number ' fi |