Shell Script Learning Day fifth-arithmetic operators

Source: Internet
Author: User
Tags arithmetic arithmetic operators ibase


I would like to have a person who is as ordinary as you
Like a refreshing breeze in the mountains
such as the warm light of the old City
From early morning to night
From the wild to the study
As long as you're the last
Just fine.

--Zhang Jiajia "Passing Through your World" one, chars= ' seq-s "100 ': from 1 to 100, with a space as a separator.

Three ways to get string lengths:

 echo ${#chars}    #取长度   291
 echo $chars |wc-m   #292
 echo $ (expr length "$chars")
Second, check the time of the command run:

Time for I in $ (seq 11111);d o/a.out;d one;

Here I put a delay of 1 seconds in the program.
Iii. Common Numerical Operations: (()) Let expr BC $[] 1, (()) Operator:

Used to perform a simple integer operation (not a decimal or a string, the BC command can be used for decimal calculations), with the exception of inline double quotes without escaping.

The operators are basically the same as the C language:

Example:

Two * is a power:
((a=1+2**3-4%3))//Internal defined variables
b=$ ((1+2**3-4%3))

Compare:
echo $ ((3>2))//output: 1 (true)

arithmetic progression sum: n * (A1+an)
echo $ ((100* (1+100)/2))

Examples of shell scripts for various (()) Operations:

\#。 /bin/bash
a=6
b=2
echo "A-B = $ (($a-  $b))"
echo "A+b = $ (($a +  $b))"
echo "A*b = $ (($  A * $b)) "echo" A/
B = $ (($a  /$b)) "echo"
A**b = $ (($a * *  $b)) "
echo" a%b = $ ($a%  $b ))"

Command-line arguments:

Change a=6 and b=2 to A=$1, b=$2:

The simplest arithmetic command:

echo $ (($1$2$3))

2. Let

Let and (()) are basically as inefficient and less used

Example:

i=2 let
i=i+8  ((i = i+8))
output:

subtract let into definition:
 i=i+8  
echo $i
output: i+8
3. Expr:

C. expr ( there must be a space before and after the operator )

Expr 2 + 2   
expr 2 \* 2  

expr $[2+3]     //Bracket expression method
expr $[2*3]

a=3
i= ' expr $a + 1 '

Example One:
Determine the file extension if it does not match the return 0 . The match successfully returns the file name character length.

Expr "test.txt": ". *\.pub"

Example Two:
determines whether the number entered is an integer :
The correct addition is an integer ($? Gets the return value of the last input):

    Read-p-T 7 "Please input:"  a   //-t 7  7 seconds does not input the time-out
    expr $a + 0  &>/dev/null   //Discard output and errors are not reported on the screen wrong
    [$?-eq 0] && echo int | | echo chars

&> equivalent to >/dev/null 2>&1
The way this is written. This command means that both the standard output and the error output are redirected to/dev/null, which means that all information (error, standard output) is discarded.

Add while Tree loop
Do

Done


The guarantee can be continuously judged without exiting. 4. BC Operations:

BC is a computational language of arbitrary precision, note is a language, it provides a number of grammatical structure, such as conditional judgment, loop, etc., can be said to be very powerful, = in practice rarely found the need for this purpose of the occasion. Another use is for the conversion of the binary.
The expr we described above supports integer operations, but there is nothing to do with floating-point arithmetic, and expr cannot perform exponential operations, and all of them are no longer spoken.

Parameters:

-I forced interactive mode;

-l using the BC built-in library, BC has a number of mathematical libraries, triangular calculation and so very practical;

-Q no longer outputs redundant information such as version when entering BC Interactive mode.

Special variables:

The ibase,obase is used for binary conversions. IBase is the input, the obase is the output of the system, the default is decimal, the scale decimal reserved digits, the default is 0 bits.

First, use the command bc-l-Q to enter interactive mode.

$BC-L-Q

ibase=2                    /* The input mechanism for the specified binary conversion is binary, the output is assumed to be decimal *
/1010

4^2                           /* Exponential operation, note: The exponent cannot be a floating-point number */
16

4*a (1)/                        * Calculates the π value, a () is a function: arctan ()
3.14159265358979323844

quit/                            * exit */

Daily Programming Exercises
To send a message to a user continuously:
You can use write usrname, establish a connection, and then control the input on the keyboard.

/dev/pts is the directory where the console device files are created after a remote login (TELNET,SSH, etc.). So you need to know the number in his PTS directory first.

While true
 does
        sleep 1
        echo "Nihao" >/DEV/PTS/9
 Done     
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.