Kevin_306

练习笔记-SHELL脚本命令算术运算(一) 20210222

shell脚本相关命令:
set命令和脚本安全:

 

 Options:

 

1.shell相关练习:

比如去掉和恢复花括号扩展功能:

[root@centos8 ~]# set +B(#去掉了大括号扩展功能)
[root@centos8 ~]# echo {1..10}
{1..10}
[root@centos8 ~]# echo $-
himHs
[root@centos8 ~]# set -B(#恢复了花括号扩展功能)
[root@centos8 ~]# echo $-
himBHs
[root@centos8 ~]# echo {1..10}
1 2 3 4 5 6 7 8 9 10

2.shell相关练习:set设置

 

 

 

3.shell相关练习

   脚本的安全选项 set -u

 1 [15:03:17 root@centos8 data]#ls
 2 2.1122  d.ab  d.abc3  d.abc32  d.abc3223  d.abc32231111
 3 [15:03:18 root@centos8 data]#ll -a
 4 total 0
 5 drwxr-xr-x.  2 root root  99 Feb 22 15:03 .
 6 dr-xr-xr-x. 18 root root 236 Jan 11 16:32 ..
 7 -rw-r--r--.  1 root root   0 Feb 22 15:03 2.1122
 8 -rw-r--r--.  1 root root   0 Feb 22 15:03 d.ab
 9 -rw-r--r--.  1 root root   0 Feb 22 15:03 d.abc3
10 -rw-r--r--.  1 root root   0 Feb 22 15:03 d.abc32
11 -rw-r--r--.  1 root root   0 Feb 22 15:03 d.abc3223
12 -rw-r--r--.  1 root root   0 Feb 22 15:03 d.abc32231111
13 [15:03:22 root@centos8 data]#cd ~
14 [15:03:26 root@centos8 ~]#cp hello.sh /data hello1.sh
15 cp: target 'hello1.sh' is not a directory
16 [15:03:50 root@centos8 ~]#cp hello.sh /data/hello1.sh
17 [15:03:57 root@centos8 ~]#cd /data
18 [15:04:03 root@centos8 data]#ls
19 2.1122  d.ab  d.abc3  d.abc32  d.abc3223  d.abc32231111  hello1.sh
20 [15:04:04 root@centos8 data]#ll -a
21 total 4
22 drwxr-xr-x.  2 root root 116 Feb 22 15:03 .
23 dr-xr-xr-x. 18 root root 236 Jan 11 16:32 ..
24 -rw-r--r--.  1 root root   0 Feb 22 15:03 2.1122
25 -rw-r--r--.  1 root root   0 Feb 22 15:03 d.ab
26 -rw-r--r--.  1 root root   0 Feb 22 15:03 d.abc3
27 -rw-r--r--.  1 root root   0 Feb 22 15:03 d.abc32
28 -rw-r--r--.  1 root root   0 Feb 22 15:03 d.abc3223
29 -rw-r--r--.  1 root root   0 Feb 22 15:03 d.abc32231111
30 -rw-r--r--.  1 root root 479 Feb 22 15:03 hello1.sh
31 [15:04:08 root@centos8 data]#bash hello1.sh
32 [15:04:21 root@centos8 data]#ll -a
33 total 0
34 drwxr-xr-x.  2 root root   6 Feb 22 15:04 .
35 dr-xr-xr-x. 18 root root 236 Jan 11 16:32 ..
36 [15:04:25 root@centos8 data]#cd ~
37 [15:05:59 root@centos8 ~]#ls
38 anaconda-ks.cfg  hello.sh  initial-setup-ks.cfg
39 [15:06:00 root@centos8 ~]#

 

脚本的内容:

4.shell相关脚本的安全选项 

[15:16:31 root@centos8 ~]#set -u

[15:18:31 root@centos8 ~]#set -o nounset 

以上2个命令功能一样

 

 恢复使用 #set +u

 

     总结:set -u     设置该选项后,当脚本在执行过程中尝试使用未定义过的变量时,报错并退出运行整个脚本(默认会把该变量的值当作空来处理),这个感觉也非常有用,有些时候可能在脚本中变量很多很长,疏忽了把某个变量的名字写错了,这时候运行脚本不会报任何错误,但结果却不是你想要的,排查起来很是头疼,使用这个选项就完美的解决了这个问题。

------------------------------------------------------------------------------------------------------

 shell相关脚本算术运算:

Shell允许在某些情况下对算术表达式进行求值,比如:let和declare 内置命令,(( ))复合命令和算术扩 展。求值以固定宽度的整数进行,不检查溢出,尽管除以0 被困并标记为错误。运算符及其优先级,关 联性和值与C语言相同。以下运算符列表分组为等优先级运算符级别。级别按降序排列优先。

计算方法:

(1) let var=算术表达式 (使用较多,let支持比较丰富的功能)

(2) ((var=算术表达式)) 和上面等价 (使用较多)

(3) var=$[算术表达式]

(4) var=$((算术表达式))

(5) var=$(expr arg1 arg2 arg3 ...)

(6) declare -i var = 数值

(7) echo '算术表达式' | bc

1.shell相关运算 

2.shell相关运算(乘法)

 3.shell相关运算

 4.shell增强型赋值

增强型赋值:
格式:
+= i+=10 相当于 i=i+10
-= i-=j   相当于 i=i-j
*=
/=
%=
++ i++,++i   相当于 i=i+1
-- i--,--i   相当于 i=i-1

[16:57:08 root@centos8 ~]#let i=10*2
[17:06:32 root@centos8 ~]#echo $i
20
[17:06:39 root@centos8 ~]#((j=i+10))
[17:07:11 root@centos8 ~]#echo $j
30

 

 

 

posted on 2021-02-22 13:04  熊猫小虾  阅读(96)  评论(0编辑  收藏  举报

导航