Programming principle:
1, Introduction to Programming
early language:
Drive hardware default can not be used;
the need for command communication between different hardware device manufacturers, we need to drive to "translate";
more close and hardware development engineers, to learn "assembly language"; and "assembly language" is limited by the manufacturer; C C ++ language 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;
classification of programming languages:
high-level language, ultra-high-level language to be translated into computer-readable language (binary instructions)
Interpreter - progressive translation, the JAVA Python line execution the shell
the bash the -X-filename.sh
compiled - a compiler, performs all the C language C ++ C # JAVA
program instructions = + Data
Object-oriented - focusing on the data programming language
Process for - focus on instruction
implementation programming language:
[] line by line
1, the order of execution
2 repeatedly executed - * for (traversing) * while (loop condition) an until (and while the opposite)
3, choose to perform - a branch if * case *
2, shell scripting language introduced
advantages: Call os (operating system) command line to achieve all functions;
Disadvantages: No library calls (to distinguish Python)
shell scripts include: 1 * command * () 2, variable (global variables, local variables, local variables, parameter passing) 3, logic
shell first sentence! ! ! (Must be written)
#! / bin / bash - defined script interpreter (# is generally considered a comment line, where special) the shebang
#! / user / bin / python
/ etc / shells - to view the current program supported by the system shell
echo $ SHELL - shell program to check the current session of the
/ etc / passwd - developed to support the user's default shell program (/ sbin / nologin)
shell execution:
1, bash command to execute the script
-n view the shell script program logic error (Note: the word may be wrong no matter)
-x progressive script execution (easy troubleshooting)
2, authorize chmod u + x filename.sh
use the full path execute the script, you can
3, variable
环境变量
局部变量 declare 命令 -- 定义变量类型
本地变量 local 在函数中使用
变量类型:
数值型:
1、整形 int
2、浮点型 float
3、布尔值 0 1 (假真)
字符串:
1、普通字符及字符串
2、数组
编程语言的分类:
强类型语言 -- 数值必须被定义,才能进行处理或运算
弱类型语言 -- 编程语言可以自动识别变量类型
【*多态* 一个数据具有多个属性,而最后使用的属性取决于和他进行运算的数据】
传参
$? -- 上一条命令的执行状态 0 正确 1-255 错误
$1 $2 ... ${10} -- 命令后面传入脚本的数据,以空格为分隔符
$# -- 传统传参数量 ${$#}
$* -- 表示所有的传参 ,所有的传参以字符串形式进行输出
$@ -- 表示所有传参,将所有的传参以列表的形式进行输出
【工作中要对所有的传参进行操作的时候,就要考虑$*和$@】
定义变量格式:
NAME = VALUE (一个等于叫赋值,两个等号叫判断)
我们通过命令declare 定义变量类型
declare -i a=10 int() str()
declare -a 数组
变量命令:
1、下划线的方式指定变量名称
2、驼峰命名方式
4、test文件测试、判断条件
在脚本中,我们需要进行 语句分支;说白了,就是要做判断
判断就是使用test命令来实现;
使用格式:
1、test [option] file
2、[条件语句]
【老师,什么时候加[ ]什么时候不加[ ]?】
常见的test选项:
比较选项:
-eq 等于
-ne 不等于
-gt 大于
-ge 大于等于
-lt 小于
-le 小于等于
判断选项:
-f 判断是否为普通文件
-d 判断是否为目录文件
-L 判断是否为链接文件
-r -w -x 判断是否具有权限
关联选项:
-o 或
-a 与
! 非
字符串判断:
= 判断两边的字符串相等
!= 不等于
-z 判断字符串是否存在
-n 和 -z 相反,存在为假,不存在为真
5、逻辑运算
与 && :两边的命令,都为真,结果才为真
或 || :两边的命令,一边为真,就是真,同时为假,才是假
非 !=
这个逻辑运算符,前后对接的都是单独存在的命令;
[$? -eq 0] && exit 0 || exit 1
6、算术运算
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
+ - * / % **
陌生人加油吧:
人生最怕的几个字就是“试试”、“等等”、“看看”“想想”最大的失败不是跌倒,而是从来不敢奔跑,没有勇气付出行动,去实现自己的梦想,如果没有去行动,永远不会发现,自己也可以创造奇迹!