I. Introduction to Programming
Early programming:
Drive hardware default can not be used;
The need for communication between different manufacturers instruction hardware devices, drivers we need to "translate;"
More close and hardware development engineers, to learn "assembly language"; and "assembly language" is limited to manufacturers;
C, C ++ is the underlying assembly language;
Now programming:
Based on high-level language, as well as ultra-high-level language, better enable programmers to achieve programming
Programming preface Category:
High-level language, ultra-high-level language translation * need * into computer-readable language (binary instructions)
Interpreted - translated line by line, line by line shell python JAVA
bash +x fileanme.sh
Compiled - a compilation of all the implementation of C language C ++ C # JAVA
Program instructions = data +
Object-oriented - focusing on the data programming language
Process for - focus on instruction
Programming language implementation:
[] Line by line
1, the order of execution
2, the loop executes - * for (convenience) * while (loop condition) until (and while the opposite)
3, choose to perform - a branch if * case *
Example: Adding a user
IF the above mentioned id $ * &> dev / null ; the then
echo " The user already exists "
the else
for i in $ *; do
useradd $ i
echo " 123456 " | passwd --stdin " $ i " &> / dev / null
echo " user $ i added successfully "
DONE
Exit 0
fi
Two, shell scripting language introduced
Advantages: call the command OS (operating system) of the line to all the functions;
Cons: no library calls (to distinguish Python)
shell scripts include:
1, * command (very important) *
2, the variable (global variables, local variables, local variables, parameter passing)
3, logic
shell first words! ! ! (Must be written)
#!/bin/bash -- 定义脚本解释器(普遍认为#为注释行,这里特殊)shebang
#!/user/bin/python
/etc/shells -- 查看当前系统支持的shell程序
echo $SHELL -- 查当前会话的shell程序
/etc/passwd -- 制定了用户默认支持的shell程序(/sbin/nologin)
shell的执行:
1、bash命令的执行脚本
-n 查看shell脚本的逻辑错误(注意:单词写错可不管)
-x 逐行显示执行的脚本程序(方便拍错)
2、授权 chmod +x filename.sh
使用全路径执行脚本,即可
三、变量
环境变量
局部变量declare命令 -- 定义变量的类型
本地变量local在函数中使用
变量类型:
数值型:
1、整型 int
2、浮点型 float
3、布尔值 0 1 (真 假) (True False)
字符串:
1、普通字符及字符串
2、数组
编程语言的分类:
强类型语言 -- 数值必须被定义,才能进行处理或运算
弱类型语言 -- 编程语言可以自动识别变量类型
【*多态* 一个数据具有多个属性,而最后使用的属性取决于和它进行运算的数据】
传参
$? -- 上一条命令执行的状态 0 正确 1-255 错误
$1 $2 ... ${10} -- 命令后面传入脚本的数据,以空格为分隔符
$# -- 统计传参数量
${$@# ???
$* -- 表示所有传参,多有的传参以字符串形式进行输出
$@ -- 表示所有传参,将所有的传参以流标的形式输出
【工作中要对所有的传参进行操作的时候,就要考虑$*和$@】
定义变量的格式:
NAME=VALUE
我们通过命令declare定义变量类型
declare -i 整数 int() str()
declare -a 数组
变量命名:
1、下划线的方式指定变量名称
2、驼峰命名方式
四、Test文本测试,条件判断
在脚本中,我们需要进行 语句分支;说白了,就要做判断
判断就是使用test命令来实现;
使用格式:
1、test [option] file
2、[条件语句]
常见的test选项:
比较选项:
-eq 等于
-ne 不等于
-gt 大于
-ge 大于等于
-lt 小于
-le 小于等于
判断选项:
-e 判断文件是否存在
-f 判断是否为普通文件
-d 判断是否为目录文件
-L 判断是否为链接文件
-r -w -x 判断文件是否具有 读 写 执行的权限
关联选项:
-o 或
-a 与
! 非
字符串判断:
= 判断俩边的字符串是否相同
!= 不等于
-z 判断字符串长度是否为0
-n 和 -z相反,存在为假,不存在为真
五、逻辑运算
与 && :俩边的命令,都为真,结果才是真
或 || :俩边的命令,一遍为真,就是真,同时为假,才是假
非 != :
这个逻辑运算符,前后对接的都是单独存在的命令;
例如: [ $? -eq 0 ] && exit 0 || exit 1
六、算数运算
*let 1 + 1
let a=1+1
echo $a
expr 1*1
expr 1 * 10
*$[$1 + 10]
a=$[2+3]
echo $a
$(($1/$2))
a=$((3/2))
echo $a
+ - * / % **