|
14、bash脚本编程之一,变量和变量类型
2021年07月13日 |
|
14.1 shell编程:脚本编程基础 编译器,解释器:将汇编语言或者是高级语言转换为机器语言的翻译设备或软件 编程语言:机器语言、汇编语言、高级语言(符合人类的思考模式,而且能通过编译器转换为机器语言) 14.1.1 编程语言分类 1、高级语言分类 (1)静态语言:编译型语言 强类型(变量类型) 事先转换成可执行格式(不需要解释器就能执行) C、C++、JAVA、C# (2)动态语言:解释型语言, on the fly 弱类型语言 边解释边执行(必须要解释器才能正常执行) PHP、SHELL、python、perl 2、根据执行情况分类 面向过程(着眼点再解决问题本身):Shell, C 面向对象(着眼点在于一个一个的对象): JAVA, Python, perl, C++ 变量:内存空间,命名,可以不断地更新内容 内存:编址的存储单元 进程:将数据临时放于内存当中 1+100: 1+1000000 1 14.1.2 bash: 1、保存类型: (1)字符 (2)数值 (a)整型 (b)浮点型: 11.23, 1.123*10^1, 0.1123*10^2 2013/10/10, 存为字符串格式需要64bit 99999: 存为数字格式需要24bit, (3)真、假 整型,8bit,保存256的话会发生溢出,导致部分程序运行不正常,称之为是缓冲区溢出 逻辑运算 逻辑:1+1>2 逻辑运算:与、或、非、异或 1: 真 0: 假 与 1 & 0 = 0 0 & 1 = 0 0 & 0 = 0 1 & 1 = 1 或: 非: ! 真 = 假 ! 假 = 真 异或 操作数相同为假,不同则为真 14.2 变量及变量操作 shell: 弱类型编程语言 14.2.1 变量类型: 强:变量在使用前,必须事先声明,甚至还需要初始化; 弱:变量用时声明,甚至不区分类型;(一般默认为字符串) 变量类型:事先确定数据的存储格式和长度 变量赋值:VAR_NAME=VALUE 1、bash变量类型: 环境变量:作用域为当前shell进程及其子进程; export VARNAME=VALUE VARNAME=VALUE export VARNAME “导出” 本地变量(局部变量):set VARNAME=VALUE: 作用域为整个bash进程;(set可省略) 引用变量:${VARNAME} (会造成混淆时使用{}),{}有时可省略 局部变量:local VARNAME=VALUE:作用域为当前代码段; 位置变量:$1, $2, … 特殊变量(系统变量):用于保存一些特殊数据 $?: 上一个命令的执行状态返回值; 程序执行,可能有两类返回值: (1)程序执行结果 (2)程序状态返回代码(0-255) 0: 正确执行 1-255:错误执行,1,2,127系统预留;其他的返回值可以自己定义 [root@Daniel-R480 ~]# clear [root@Daniel-R480 ~]# echo $? 0 [root@Daniel-R480 ~]# ll total 16 -rw——- 1 root root 6921 Jan 29 2019 anaconda-ks.cfg -rw-r–r– 1 root root 0 Jun 7 10:55 cut -rw-r–r– 1 root root 0 Jun 7 09:47 hellosa -rwx—— 1 apache root 513 Jun 4 11:52 inittab -rw——- 1 root root 6577 Jan 29 2019 original-ks.cfg -rw-r–r– 1 root root 15 Jun 7 10:53 passwd.txt -rw-r–r– 1 root root 91 Jun 7 13:43 test2.txt -rw-r–r– 1 root root 12 Jun 7 14:43 test3.txt -rw-r–r– 1 root root 46 Jun 7 11:49 test.txt [root@Daniel-R480 ~]# echo $? 0 [root@Daniel-R480 ~]# ld ld: no input files [root@Daniel-R480 ~]# echo $? 1 [root@Daniel-R480 ~]# [root@Daniel-R480 ~]# id student &> /dev/null [root@Daniel-R480 ~]# echo $? 1 dev/null: 软件设备(模拟的不存在的设备), bit bucket,数据黑洞(会吞掉所有接受到的数据) echo $?,可用来判断用户是否存在 输出重定向: >,>>,2>,2>>,&> 撤消变量: unset VARNAME 查看当shell中变量: set 查看当前shell中的环境变量: printenv env export [root@Daniel-R480 ~]# ANIMALS=PIG [root@Daniel-R480 ~]# ANIMALS=$ANIMALS:GOAT [root@Daniel-R480 ~]# echo $ANIMALS PIG:GOAT [root@Daniel-R480 ~]# ANIMALS=$ANIMALS:SHEEP [root@Daniel-R480 ~]# echo $ANIMALS PIG:GOAT:SHEEP shell默认不进行数据的运算 14.3 BASH脚本 脚本:命令的堆砌,按实际需要,结合命令流程控制机制实现的源程序 1、shebang: 魔数(每个脚本的第一行) #!/bin/bash(#!+脚本的解释路径) # 注释行,不执行(正文里面的#作用) [root@Daniel-R480 ~]# vim first.sh [root@Daniel-R480 ~]# [root@Daniel-R480 ~]# file /bin/ls /bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=ceaf496f3aec08afced234f4f36330d3d13a657b, stripped [root@Daniel-R480 ~]# file first.sh first.sh: ASCII text [root@Daniel-R480 ~]# vim first.sh [root@Daniel-R480 ~]# file first.sh first.sh: Bourne-Again shell script, ASCII text executable [root@Daniel-R480 ~]# ll first.sh -rw-r–r– 1 root root 35 Jun 7 17:44 first.sh [root@Daniel-R480 ~]# chmod +x first.sh [root@Daniel-R480 ~]# ./first.sh # # /etc/fstab # Created by anaconda on Mon Jan 28 20:51:49 2019 # # Accessible filesystems, by reference, are maintained under ‘/dev/disk’ # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=f41e390f-835b-4223-a9bb-9b45984ddf8d / xfs defaults 0 0 adm crash empty gopher lib lock mail opt run tmp yp cache db games kerberos local log nis preserve spool var.out [root@Daniel-R480 ~]# [root@Daniel-R480 ~]# chmod -x first.sh [root@Daniel-R480 ~]# ll total 16 -rw——- 1 root root 6921 Jan 29 2019 anaconda-ks.cfg -rw-r–r– 1 root root 0 Jun 7 10:55 cut -rw-r–r– 1 root root 35 Jun 7 17:44 first.sh -rw-r–r– 1 root root 0 Jun 7 09:47 hellosa -rwx—— 1 apache root 513 Jun 4 11:52 inittab -rw——- 1 root root 6577 Jan 29 2019 original-ks.cfg -rw-r–r– 1 root root 15 Jun 7 10:53 passwd.txt -rw-r–r– 1 root root 91 Jun 7 13:43 test2.txt -rw-r–r– 1 root root 12 Jun 7 14:43 test3.txt -rw-r–r– 1 root root 46 Jun 7 11:49 test.txt [root@Daniel-R480 ~]# bash first.sh # # /etc/fstab # Created by anaconda on Mon Jan 28 20:51:49 2019 # # Accessible filesystems, by reference, are maintained under ‘/dev/disk’ # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=f41e390f-835b-4223-a9bb-9b45984ddf8d / xfs defaults 0 0 adm crash empty gopher lib lock mail opt run tmp yp cache db games kerberos local log nis preserve spool var.out [root@Daniel-R480 ~]# 2、脚本和环境关系 脚本在执行时会启动一个子shell进程; 命令行中启动的脚本会继承当前shell环境变量; 系统自动执行的脚本(非命令行启动)就需要自我定义需要各环境变量; 练习: 写一个脚本,完成以下任务 1、添加5个用户, user1,…, user5 2、每个用户的密码同用户名,而且要求,添加密码完成后不显示passwd命令的执行结果信息; 3、每个用户添加完成后,都要显示用户某某已经成功添加; #!/bin/bash # useradd user1 echo !$ | passwd –stdin &>/dev/null echo “Add user1 successfully!” useradd user2 echo !$ | passwd –stdin &>/dev/null echo “Add user2 successfully!” useradd user3 echo !$ | passwd –stdin &>/dev/null echo “Add user3 successfully!” useradd user4 echo !$ | passwd –stdin &>/dev/null echo “Add user4 successfully!” useradd user5 echo !$ | passwd –stdin &>/dev/null echo “Add user5 successfully!” [root@Daniel-R480 ~]# ./aduser.sh Add user1 successfully! Add user2 successfully! Add user3 successfully! Add user4 successfully! Add user5 successfully! [root@Daniel-R480 ~]# tail -5 /etc/passwd user1:x:4008:4008::/home/user1:/bin/bash user2:x:4009:4009::/home/user2:/bin/bash user3:x:4010:4010::/home/user3:/bin/bash user4:x:4011:4011::/home/user4:/bin/bash user5:x:4012:4012::/home/user5:/bin/bash [root@Daniel-R480 ~]# |