Shell脚本编程:
编程语言的分类:根据运行方式
编译运行:源代码—>编译器(编译)-- > 程序文件
C语言: 解释运行:源代码—>运行时启动解释器,由解释器边解释边运行;
根据器编程过程中功能的实现是调用库函数调用外部的程序文件;
shell脚本编程: 利用系统上的命令及编程组件进行编程; 完整编程: 利用库或飙车组件进行编程; 编程模型:过程式编程语言,面向对象的编程语言; 程序=指令+数据 过程式:以指令为中心来组织代码,数据是服务于代码; 顺序执行,现在执行,循环执行 对象式:以数据为中心来组织代码,围绕数据来组织于代码; 类 实例化对象
shell脚本编程:过程式编程,解释运行,依赖于外部程序文件运行;
如何写 shell脚本:
脚本文件的第一行,顶格:给出 shebang,解释器路径,用于指明解释执行
当前脚本的解释器程序文件
常见的解释器:
#!/bin/bash
#!/usr/bin/python
#!/usr/bin/perl
第一个脚本:
~]# cat myfirst.sh
#!/bin/bash useradd user3 echo "user3" | passwd --stdin user3
mktemp -d /tmp/test.XXXX
运行结果:
~]# bash myfirst.sh
uid=1019(user3) gid=1019(user3) 组=1019(user3)
更改用户 user3 的密码 。
passwd:所有的身份验证令牌已经成功更新。
/tmp/test.woDX
脚本: export JAVA_HOME=/usr ~]# nano /etc/profile.d/java.sh 重读配置变量文件 ~]# . /etc/profile.d/java.sh ~]# export declare -x JAVA_HOME="/usr"
第二个脚本:
~]# cat test.sh #!/bin/bash echo "show some under /etc" ls -d /etc/[pP]* echo echo "Traslate lower to upper" ls -d /var/* | tr 'a-z' 'A-Z' echo echo "create a temp file" mktemp /tmp/myfile.XXXX ~]# bash test.sh show some under /etc /etc/pam.d /etc/pnm2ppa.conf /etc/postfix /etc/printcap /etc/protocols /etc/passwd create a temp file /tmp/myfile.xF5M