"Pit" of automatic implicit type conversion in Shell, AWK

Source: Internet
Author: User

1. Questions:

in a variety of programming languages, the weak type of language is quite a lot, on the one hand this "dynamic type" is very convenient to use, while on the other hand, "pit" you do not discuss ~ Common SQL, Shell, AWK will encounter various hidden "implicit type conversion", the following list some Shell, Awk Automatic implicit type conversion case to prevent the pit from falling.

    • Note the shell, awk variable is an empty string , the variable is null undefined , and the initial value of the implicit conversion problem:
# dictionary sort comparison under shell [email protected] 10:59:23/opt/script >[[((a > 0)]] && Echo 1 1[email prote  CTED] 10:59:34/opt/script >[[((> 0)] && Echo 1 # Double brackets do not do arithmetic comparisons, but rather a dictionary comparison of strings, equivalent to [[]][[((0 = = 0)]] && Echo 1 1[email protected] 13:04:27/opt/script >[[((>))] && echo 1 [email&n Bsp;protected] 13:04:34/opt/script ># Shell string comparison does not convert, strictly according to the literal dictionary comparison [email protected] 11:51:41/opt/script > [[A = = "]] && echo 1[email protected] 14:00:53/opt/script >[[a >" "] && Echo 1 1[EMAIL&N Bsp;protected] 14:00:59/opt/script >[[a > 0]] && Echo 1 # Shell numeric type implicit conversions: variable undefined will be converted to 0[email prote CTED] 11:53:56/opt/script >[[a-eq 0]] && Echo 1 1[email protected] 11:54:11/opt/script >[[A1- EQ 1]] && echo 1 [email protected] 11:55:11/opt/script >[[1a-eq 1]] && echo 1-bash: [[: 1a: Value too great for base (erRor token is "1a") # awk implicit type only converts 1a, does not convert a1[email protected] 11:55:15/opt/script >echo|awk ' {print 1a = 1} ' # awk An external variable undefined is set to an empty string, and an empty string is not implicitly converted to a number [email protected] 14:12:57/opt/script >echo|awk-va= ' {print a = = 0} ' 0[EMAIL&NB Sp;protected] 14:18:08/opt/script >echo|awk ' {print "" = = 0} ' 0[email protected] 14:13:01/opt/script >echo|a Wk-va= ' {print a = = '} ' # awk built-in variable undefined default is implicitly converted to the number 0 or an empty string [email protected] 14:13:12/opt/script >echo|awk ' {prin t a = = 0} ' 1[email protected] 14:13:48/opt/script >echo|awk ' {print a = = ""} ' # awk built-in variable when comparing, a side with double quotes, the end result As a string comparison (undefined variable or auto-implicit conversion) [email protected] 14:23:43/opt/script >echo|awk ' {print 0b = = ' xx '} ' 0[email  Protected] 14:23:47/opt/script >echo|awk ' {print 0b = = xx} ' 1[email protected] 14:36:57/opt/script >echo|a WK ' {print ' 00 "= = 0} ' # explicitly identifies a string that is no longer implicitly converted to the implicit conversion in the 0# awk: undefined variables are automatically converted implicitly regardless of whether the final result is a numeric comparison [email protected] 14:27:49/opt /script >echo|awk ' {print 0b= = "0"} ' 1echo|awk ' {y=0b; print y = = "0"} ' 1echo|awk ' {print 0b = = 0} ' 1echo|awk ' {y= "0b"; print y = = "0"} ' 0ech O|awk ' {y= "0b"; print +y = = "0"} ' 1[email protected] 23:49:55 ~ >echo|awk ' {y= "0b"; Print +y = "xx"} ' 0[EMAIL&NB Sp;protected] 23:49:59 ~ >echo|awk ' {y= "0b"; print (+y > "xx")} ' 0[email protected] 23:50:14 ~ >echo|awk ' {y = "0b"; Print +y < "xx"} ' 1[email protected] 14:41:16/opt/script >echo|awk ' {print "xx" a = = "xx"} ' # awk external variables participate in the comparison, Unlike the implicit conversion characteristics of built-in variables, a number that contains a non-numeric string is directly treated as a string. # that is to say awk external variables do not have the properties of built-in variables for non-digital implicitly conversions [email protected] 16:16:27/opt/script >a=0b; echo|awk-vm= "$a" ' {print m; print m = = 0; print int (m) = = 0} ' 0b01[email protected] 16:16:39/opt/script >a=00;  echo|awk-vm= "$a" ' {print m; print m = = 0; print int (m) = = 0} ' 0011[email protected] 16:16:59/opt/script >a=00; echo|awk-vm= "$a" ' {print m; print m = = "0"; print int (m) = = "0"} ' 0001[email protected] 16:20:11/opt/script >a= 00; Echo|awk-vm= "$a" ' {print m; print m = = "XX"; print int (m) = = 0} ' 0011[email protected] 00:33:24 ~ >a=0b; echo|awk-vm= "$a" ' {b=0;print 0b; print 0b = = 0; print int (m) = = 0} ' 0001# to avoid the ambiguity and uncertainty of the case, or to cast it honestly with +, int ~[ema Il protected] 00:43:55 ~ >echo|awk ' {print int ("1/*")} ' 1[email protected] 00:47:31 ~ >echo|awk ' {print Int ("*/1")} ' 0[email protected] 00:47:38 ~ >echo|awk ' {print + "*/1"} ' 0[email protected] 00:47:47 ~ > Echo|awk ' {print + ' 1/* '} ' 1[email protected] 00:47:53 ~ >
2. Conclusion: (1) The Shell's automatic implicit type conversion is rather weak, and awk is relatively fault-tolerant .
(2) in case, if the automatic implicit type conversion by Shell or awk is quite unreliable, it is extremely error-prone .
(3) to get a definite result, or an honest cast, such as awk using +, int and so on.

"Pit" of automatic implicit type conversion in Shell, AWK

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.