Learning Linux (9) - Shell operator

table of Contents

Overview and declare command

1, declare the variable type declaration

2, the variable is declared as numeric

3, declare array variables

4, the environment variable declaration

5, read-only attribute to declare variables

The method of numerical calculation

Method 1: Use declare -i declared

Method 2: expr or let numerical computation tool

Method 3: "$ ((expression))" or "$ [expression]"

Variable test


 

Video tutorial lesson but learning Mu: https://www.imooc.com/learn/336

This section summarizes the study and made notes.

 

Overview and declare command

Disadvantage shell variables: weak type variable, the default type is a string, calculates a lot of trouble.

1, declare the variable type declaration

declare [+/-][选项] 变量名
	选项:
		-:给变量设定类型属性
		+:取消变量的类型属性
		-a:将变量声明为数组型
		-i:将变量声明为整数型(integer)
		-x:将变量声明为环境变量
		-r:将变量声明为只读变量
		-p:显示指定变量的被声明的类型

2, the variable is declared as numeric

Example 1: declare -i cc = $ aa + $ bb command performs an addition operation.

aa=11
bb=22
#给变量aa和bb赋值
declare -i cc=$aa+$bb
#声明变量cc的类型是整数型,它的值是aa和bb的和

3, declare array variables

Example 1: declare -a nums [3] = " Drifting" , declare the array.

#定义数组
nums[0]="唐僧"
nums[1]="孙悟空"
nums[2]="猪八戒"
declare -a nums[3]="沙僧"

#查看数组
# ${nums}打印的是数组的第一个元素的值
echo ${nums}
# ${nums[2]}打印的数组指定下标的值
echo ${nums[2]}
# ${nums[*]}打印的数组的所有元素
echo ${nums[*]}

Example 2: declare -a command is not used to make, can declare the array.

4, the environment variable declaration

export actually declare command call.

Example 1: declare -x test = 123 commands , env used to view the environment variables.

declare -x test=123
	和export作用相似,但其实就是declare命令的作用

5, read-only attribute to declare variables

Once a variable is set if the read-only attribute, it can not be operated.

declare -r test
	test是指变量名,给test赋予只读属性,但是请注意只读属性会让变量不能修改不能删除,甚至不能取消只读属性。

Example 1: declare -r test command , set the variable read-only test.

 

The method of numerical calculation

Method 1: Use declare -i declared

a=1
b=2
declare -i c=$a+$b
echo $c

Method 2: expr or let numerical computation tool

a=1
b=2
c=$(expr $a + $b)
#c的值是a和b的和,注意“+”号两侧必须有空格
echo $c

Method 3: "$ ((expression))" or "$ [expression]"

a=1
b=2
c1=$(($a+$b))
echo $c1
c2=$[$a+$b]
echo $c2

Example 1: Operation

echo $(((11+4)*5/2))

echo $((14%3))

echo $((1&&0))
#逻辑与运算只有相与的两边都是1,与的结果才是1,否则与的结果是0

 

Variable test

Variables used in the test script optimization.

1 case: x = $ {y- test new value}, y variable does not exist

y=3
# 删除变量y
unset y
# x=${y-2},2是测试的一个新值
x=${y-2}
# 进行测试,因为变量y不存在,所以x=新值
echo $x

Example 2: x = $ {y- test new value}, y variable is null

y=""
# x=${y-2},2是测试的一个新值
x=${y-2}
# 进行测试,因为变量y为空值,所以x=空值
echo $x

Example 3: Testing x = $ {y- new value}, y has a value of variable

y=3
# x=${y-2},2是测试的一个新值
x=${y-2}
# 进行测试,因为变量y有设置值,所以x=$y
echo $x

 

 

 

 

 

Published 500 original articles · won praise 77 · views 160 000 +

Guess you like

Origin blog.csdn.net/cnds123321/article/details/105024827