Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the numerical operations in shell programming?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly talks about "what are the numerical operations of shell programming?" interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the numerical operations of shell programming?"

Basic numerical operations in Shell programming, which include:

Addition, subtraction, multiplication, division, exponentiation, modulus, etc., between values (including integers and floating point numbers)

Generate a random number in a specified range

Generate a sequence of numbers in a specified range

Shell itself can do integer operations, more complex operations to achieve through external commands, such as expr,bc,awk and so on. In addition, a random number from 0 to 32767 can be generated through the RANDOM environment variable, and some external tools, such as awk, can generate a random number through the rand () function. The seq command can be used to generate a sequence. Each of them is introduced below.

Example of integer operation: add 1 to a number

This experiment is run in the interactive mode of Linux virtual machine.

$echo $i2 $echo $i1 | awk'{printf $1cards 2}'3

Description: after expr, there is a space separation between $igraine and 1. If you perform multiplication, you need to escape the operator, otherwise Shell interprets the multiplication sign as a wildcard, resulting in a syntax error; the $1 and $2 after awk refer to $I and 1, respectively, the 1st and 2nd numbers from left to right.

Use Shell's built-in commands to view the types of commands as follows:

$type typetype is a shell builtin$ type letlet is a shell builtin$ type exprexpr is hashed (/ usr/bin/expr) $type bcbc is hashed (/ usr/bin/bc) $type awkawk is / usr/bin/awk

As you can see from the above demonstration: let is the Shell built-in command, and the others are external commands, all in the / usr/bin directory. Expr and bc have been loaded in the hash table in memory because they have just been used.

Description: if you want to view the help of different commands, for Shell built-in commands such as let and type, you can view related help through a built-in command help of Shell, while some external commands can view help through an external command of Shell, such as built-in command: help let, external command: man expr, etc.

Script example: add from 1 to a number #! / bin/bash# calc.shi=0;while [$I-lt 10000] do ((iTunes +)) doneecho $I

Description: here through while [conditional expression]; do. Done loop to implement. -lt is the less than sign.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report