Linux numeric calculation, linux numeric

Source: Internet
Author: User
Tags control characters

Linux numeric calculation, linux numeric

Recommended method 3. more intuitive

In linux shell language, some variables store numbers, but when involved in numerical calculation, the error is reported. Why?

A terrible idea ...... Let's sigh.

Is there any character in 98 that is not a number?
You guessed it.

The crux of the problem has been found,
Free = 'top-n 1 | grep "Cpu (s):" | awk-F', ''{print $4} '| awk-F' [.] ''{print $1 }''
Run echo $ free> test, and then run vim test. There is a space in front of the number, and several invisible control characters in front of the space.
Here I am
[[M ^ [(B ^ [[39; 49m ^ [[m ^ [(B 97, then go through a space and change it
[[M ^ [(B ^ [39; 49m ^ [m ^ [(B97.
I have been busy for a long time. Suddenly, as soon as I want to output $ free to a file, I can see other characters in wood.

Recommended Modification Scheme:
# Use the following method to delete all spaces
[Root @ localhost ~] # Free = 'echo-n $ free | sed's // g '';
Change to free = 'echo-n $ free | awk '{print $2 }''

Filter spaces. Instead, extract numbers directly ~
I was also a newbie. I wanted to use regular expressions to get numbers ...... I found this works ......

No score ...... Give the best answer ......
It turns out that there are points ......

Q: How do I perform numerical operations in linux? For example, if I want to assign $ sum to the values of tab1 [$ I] and tab [$2 ],

You are a programming language and have nothing to do with the operating system. Download a compiler corresponding to your programming language.

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: 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.