Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Detailed explanation and examples of declare in linux
Detailed explanation and examples of declare in linux
statement

Core grammar:

-I declare it as an integer.

-The declaration is not an array.

-f statement doesn't work.

-r statement is not read-only.

The above are commonly used, of course, others can be obtained through man declaration.

By default, if we want to get a variable of numeric integer type for the declaration string of an object, we need to declare it this time.

Let's look at a situation:

Price = 10

num=3

Echo $ price * quantity

-

10*3

If the declaration is declare, then it is as follows.

inter 12 @ inter 12:/tmp $ declare-I price = 10;

inter 12 @ inter 12:/tmp $ declare-I num = 3;

inter 12 @ inter 12:/tmp $ declare-I total = $ price * $ num

inter 12 @ inter 12:/tmp $ echo $ total

30

In fact, there is another way to deal with declare, just look at the code.

1 #! /bin/bash

2 # test statement and $ (())

three

four

5 user statement () {

six

7 statement-i num =1;

8 statement -i price = 5;

9 declaration-my total =$num*$price

10 echo' declares that the style is and the result is' $ total'

1 1 }

12

13

14 userDouble(){

15

16 number d= 1

17 price_d=5

18 total_d=$ ((quantity _d * price _ d))

19 echo "double price for users is" $total_d "

20 }

2 1

22

23 user statement

24 user double

-

inter 12 @ inter 12:~/my shell/shell/declare $。 /test

The declaration style is that the result is 5.

The double price for users is 5.

$((num_d*price_d)): means to perform the calculation and return the result. If it is ((num_d*price_d)), the calculation will not return the result. Please pay attention here!

Interactive command reading

This is also a very common command, according to the different input of end users, execute different script commands!

1 #! /bin/bash

2

3 echo'-This is the first line-'

4 echo' 1. When you select 1, echo heihei

5 Echo' 2. Echo haha when you choose 2'

6 echo -n' Select Yes'

7 reading choice

eight

9 if[$ selection = " 1 "]; then

10 echo' hehe'

1 1 elif[$ selection = " 2 "]; then

12 echo' haha'

13 others

14 echoes "Invalid option"

15 fi