shell 脚本编程
day 01
~shell:逻辑+系统管理命令
一、shell与bash区别
shell 命令解释器:众多解释器的总括ksh、sh、tcsh、csh、bash
bash 命令解释器: 属于某一个人,或属于某家公司开发出来的产品,众多解释器中的一个,具体产品
~存放位置 #cat /etc/shells
/usr/bin/bash快捷键 = /bin/bash
~用户解释器:useradd -s /bin/bash 默认
#usermod -s /bin/tcsh 永久改解释器
#tcsh 回车 临时改解释器
#exit 退出该解释器
~装解释器 yum -y insall kssh 不支持快捷键,没有上下左右键
#cat /etc/shells
#ksh 回车 #ls #ls – color
################################
二、bash特点
运维工作,人工—自动化
脚本运行-cpu,内存;用户白天访问,脚本晚上运行较好
@快捷键 ctrl +L +D +W +E命令的行尾 +A 命令的行首
@tab键:命令、路径、文件、选项-默认不支持-要装bash-completion包
公司最小化mini安装默认不支持tab键-要装bash-completion包
公司不允许随便装包改密、装包需先申请–gcc先源码包编译、安装再卸载
@命令历史:方便上下键,默认1000条,50-100条
/etc/profile HISTSIZE=50,history -c清空历史记录
真实机的PXE的安装后脚本Post-scripts、虚拟机的模版里先改命令历史
PXE pre-Scripts
PXE post-Scripts
@别名:#alias 查看默认别名 ; \cp 去掉别名 ; i:interacter交互式;alias 查看=‘ls –color’ 支持中文;
环境变量: /etc/profile 全局文件,对所有用户有效;~/.bash_profile用户文件 ,指定用户有效。
/etc/bashrc放的是 函数、别名
@输入输出:标准输出1> 不写默认、错误输出2、&错误正确都输出
[root@svr5 ~]# mail –s Error root < /etc/passwd
@重定向
#ls anaconda-ks.cfg ttt > 1.log 2> 2.log /分开存放
#vim a.sh
#!/bin/bash
for i in 1 2 33 a g /看个数 循环五次
for i in {1..100} (cat a.txt) /<span style="font-family:'宋体';">循环</span></p><p>do</p><p> useradd (cat a.txt) /<span style="font-family:'宋体';">循环</span></p><p>do</p><p> useradd i >>ok.log 2>>error.log /日志追加写入
echo “123” | passwd –stdin useri &<span style="color:rgb(255,51,51);">>></span>2.log</p><p>done</p><p>#######</p><p><strong><span style="color:rgb(63,63,63);">ls >1.log 2>&1 = ls &>1 效果等同</span></strong></p><p>#######</p><p><strong><span style="color:rgb(63,63,63);">mail -s warning root < mail.txt 再将文件发送到手机</span></strong></p><p><strong><span style="color:rgb(63,63,63);">监控【CPU】90% 》 邮件</span></strong></p><p><strong><span style="color:rgb(63,63,63);">####</span></strong></p><p><strong><span style="color:rgb(63,63,63);">top -P 按cpu排序 -M 按内存排序</span></strong></p><p>uptime</p><p>free <span style="font-family:'宋体';">看内存 小于哪个值用 </span><span style="font-family:Calibri;">if</span><span style="font-family:'宋体';">判断</span></p><p>echo“CPU<span style="font-family:'宋体';">超了</span><span style="font-family:Calibri;">” | mail -s warning abc@139.com</span></p><p><strong><span style="color:rgb(63,63,63);">#######</span></strong></p><p><strong><span style="color:rgb(63,63,63);">@管道操作 </span></strong></p><p><strong><span style="color:rgb(63,63,63);">linux设计思想</span></strong></p><p><strong><span style="color:rgb(63,63,63);">small 【优点:快,漏洞少bug】 【缺点:功能少,依赖关系】</span></strong></p><p><strong><span style="color:rgb(63,63,63);">用管道可以使出错率减小</span></strong></p><p><span style="color:#ff0000;">who | wc -l</span> <span style="font-family:'宋体';">看有多少人连接电脑</span></p><p>[root@svr5 ~]# yum list | grep cluster</p><p><strong><span style="color:rgb(63,63,63);">####################################################</span></strong></p><p><strong><span style="color:rgb(63,63,63);">三、脚本的编写</span></strong></p><p><strong><span style="color:rgb(63,63,63);">1.新建文件(.sh)</span></strong></p><p><strong><span style="color:rgb(63,63,63);">2.编写内容(#!/bin/bash;#版本号、描述信息;)</span></strong></p><p><strong><span style="color:rgb(63,63,63);">3.赋予权限,执行(chmod +x )</span></strong></p><p>执行脚本:有权限的<span style="font-family:Calibri;"># ./test.sh </span><span style="font-family:'宋体';">相对路径、</span><span style="font-family:Calibri;">#/root/test.sh </span><span style="font-family:'宋体';">绝对路径;</span></p><p> 无权限的<span style="font-family:Calibri;">: 1.</span><span style="font-family:'宋体';">开子进程的</span><span style="font-family:Calibri;">--#bash test.sh #sh test.sh ksh/tcsh/sh #python test.py </span></p><p> <strong><span style="color:rgb(63,63,63);">2.不开子进程的--#source test.sh # . test.sh (修改系统脚本时,相当于reboot重起可立即生效)</span></strong></p><p>printf “abc” <span style="font-family:'宋体';">用</span><span style="font-family:Calibri;">python</span><span style="font-family:'宋体';">无法解释</span></p><p>which perl/python</p><p>#!/bin/bash</p><p>o=”chen” </p><p>exit </p><p>[root@server0 ~]# bash 3.sh </p><p>[root@server0 ~]# echo i &<span style="color:rgb(255,51,51);">>></span>2.log</p><p>done</p><p>#######</p><p><strong><span style="color:rgb(63,63,63);">ls >1.log 2>&1 = ls &>1 效果等同</span></strong></p><p>#######</p><p><strong><span style="color:rgb(63,63,63);">mail -s warning root < mail.txt 再将文件发送到手机</span></strong></p><p><strong><span style="color:rgb(63,63,63);">监控【CPU】90% 》 邮件</span></strong></p><p><strong><span style="color:rgb(63,63,63);">####</span></strong></p><p><strong><span style="color:rgb(63,63,63);">top -P 按cpu排序 -M 按内存排序</span></strong></p><p>uptime</p><p>free <span style="font-family:'宋体';">看内存 小于哪个值用 </span><span style="font-family:Calibri;">if</span><span style="font-family:'宋体';">判断</span></p><p>echo“CPU<span style="font-family:'宋体';">超了</span><span style="font-family:Calibri;">” | mail -s warning abc@139.com</span></p><p><strong><span style="color:rgb(63,63,63);">#######</span></strong></p><p><strong><span style="color:rgb(63,63,63);">@管道操作 </span></strong></p><p><strong><span style="color:rgb(63,63,63);">linux设计思想</span></strong></p><p><strong><span style="color:rgb(63,63,63);">small 【优点:快,漏洞少bug】 【缺点:功能少,依赖关系】</span></strong></p><p><strong><span style="color:rgb(63,63,63);">用管道可以使出错率减小</span></strong></p><p><span style="color:#ff0000;">who | wc -l</span> <span style="font-family:'宋体';">看有多少人连接电脑</span></p><p>[root@svr5 ~]# yum list | grep cluster</p><p><strong><span style="color:rgb(63,63,63);">####################################################</span></strong></p><p><strong><span style="color:rgb(63,63,63);">三、脚本的编写</span></strong></p><p><strong><span style="color:rgb(63,63,63);">1.新建文件(.sh)</span></strong></p><p><strong><span style="color:rgb(63,63,63);">2.编写内容(#!/bin/bash;#版本号、描述信息;)</span></strong></p><p><strong><span style="color:rgb(63,63,63);">3.赋予权限,执行(chmod +x )</span></strong></p><p>执行脚本:有权限的<span style="font-family:Calibri;"># ./test.sh </span><span style="font-family:'宋体';">相对路径、</span><span style="font-family:Calibri;">#/root/test.sh </span><span style="font-family:'宋体';">绝对路径;</span></p><p> 无权限的<span style="font-family:Calibri;">: 1.</span><span style="font-family:'宋体';">开子进程的</span><span style="font-family:Calibri;">--#bash test.sh #sh test.sh ksh/tcsh/sh #python test.py </span></p><p> <strong><span style="color:rgb(63,63,63);">2.不开子进程的--#source test.sh # . test.sh (修改系统脚本时,相当于reboot重起可立即生效)</span></strong></p><p>printf “abc” <span style="font-family:'宋体';">用</span><span style="font-family:Calibri;">python</span><span style="font-family:'宋体';">无法解释</span></p><p>which perl/python</p><p>#!/bin/bash</p><p>o=”chen” </p><p>exit </p><p>[root@server0 ~]# bash 3.sh </p><p>[root@server0 ~]# echo o //开子进程的方式bash/sh执行此脚本没有任何变化
[root@server0 ~]# source 3.sh
[root@server0 ~]# echo o //<span style="font-family:'宋体';">不子进程的方式</span><span style="font-family:Calibri;">source </span><span style="font-family:'宋体';">执行此脚本会退出远程</span></p><p>chen</p><p>#######</p><p>[root@server0 ~]# history | wc -l</p><p>58</p><p>[root@server0 ~]# . /etc/profile <span style="font-family:'宋体';">或 </span><span style="font-family:Calibri;">[root@server0 ~]# source /etc/profile</span></p><p>[root@server0 ~]# history | wc -l</p><p>10</p><p>###############################################################################</p><p><strong><span style="color:rgb(63,63,63);">四、案例1:配置yum软件源</span></strong></p><p>[root@server0 ~]# vim test.sh</p><p>#!/bin/bash</p><p>echo -e "\033[31m <span style="font-family:'宋体';">准备配置</span><span style="font-family:Calibri;">yum</span><span style="font-family:'宋体';">源</span><span style="font-family:Calibri;">.. \033[0m" /</span><span style="font-family:'宋体';">红色</span></p><p>sleep 1 </p><p>rm -rf /etc/yum.repos.d/*</p><p>echo " [dvd] </p><p>name=dvd </p><p>baseurl=<a href="http://content.example.com/rhel7.0/x86_64/dvd" rel="nofollow" target="_blank"><u><span style="color:rgb(0,0,128);">http://content.example.com/rhel7.0/x86_64/dvd</span></u></a> </p><p>gpgcheck=0 </p><p>enabled=1" > /etc/yum.repos.d/dvd.repo</p><p>yum clean all &>/dev/null</p><p>echo “yum<span style="font-family:'宋体';">源配置完</span>成<span style="font-family:Calibri;">”</span></p><p>echo -e "\033[35m 可用的软件有: \033[0m" /紫色</p><p>yum repolist | grep “repolist”</p><p><span style="color:rgb(63,63,63);">######</span></p><p><strong><span style="color:rgb(63,63,63);">案例2:编写脚本,安装软件,自动启动,开机自起</span></strong></p><p>yum -y install vsftpd systemctl restart vsftpd systemctl enable vsftpd </p><p>yum -y install vsftpd &>/dev/null</p><p>systemctl restart vsftpd</p><p>systemctl enable vsftpd &>/dev/null</p><p>echo -e "\033[33m vsftpd<span style="font-family:'宋体';">服务启动完成,请访问</span><span style="font-family:Calibri;">ftp</span><span style="font-family:'宋体';">:</span><span style="font-family:Calibri;">//172.25.0.11 </span><span style="font-family:'宋体';">验证 </span><span style="font-family:Calibri;">\033[0m" </span></p><p>firefox ftp://172.25.0.11</p><p><strong><span style="color:rgb(63,63,63);">####################################################</span></strong></p><p><strong><span style="color:rgb(63,63,63);">五、变量</span></strong></p><p><strong><span style="color:rgb(63,63,63);">(数字,字母,下划线)</span></strong></p><p><strong><span style="color:rgb(63,63,63);">自定义变量 </span></strong>1.<span style="font-family:'宋体';">定义:变量名</span><span style="font-family:Calibri;">=</span><span style="font-family:'宋体';">变量值 (<strong><span style="font-size:16px;color:#ff0000;">中间不能有空格、特殊符号、数字开头</span></strong>)</span></p><p> 2.<span style="font-family:'宋体';">调用</span><span style="font-family:Calibri;">echo o //<span style="font-family:'宋体';">不子进程的方式</span><span style="font-family:Calibri;">source </span><span style="font-family:'宋体';">执行此脚本会退出远程</span></p><p>chen</p><p>#######</p><p>[root@server0 ~]# history | wc -l</p><p>58</p><p>[root@server0 ~]# . /etc/profile <span style="font-family:'宋体';">或 </span><span style="font-family:Calibri;">[root@server0 ~]# source /etc/profile</span></p><p>[root@server0 ~]# history | wc -l</p><p>10</p><p>###############################################################################</p><p><strong><span style="color:rgb(63,63,63);">四、案例1:配置yum软件源</span></strong></p><p>[root@server0 ~]# vim test.sh</p><p>#!/bin/bash</p><p>echo -e "\033[31m <span style="font-family:'宋体';">准备配置</span><span style="font-family:Calibri;">yum</span><span style="font-family:'宋体';">源</span><span style="font-family:Calibri;">.. \033[0m" /</span><span style="font-family:'宋体';">红色</span></p><p>sleep 1 </p><p>rm -rf /etc/yum.repos.d/*</p><p>echo " [dvd] </p><p>name=dvd </p><p>baseurl=<a href="http://content.example.com/rhel7.0/x86_64/dvd" rel="nofollow" target="_blank"><u><span style="color:rgb(0,0,128);">http://content.example.com/rhel7.0/x86_64/dvd</span></u></a> </p><p>gpgcheck=0 </p><p>enabled=1" > /etc/yum.repos.d/dvd.repo</p><p>yum clean all &>/dev/null</p><p>echo “yum<span style="font-family:'宋体';">源配置完</span>成<span style="font-family:Calibri;">”</span></p><p>echo -e "\033[35m 可用的软件有: \033[0m" /紫色</p><p>yum repolist | grep “repolist”</p><p><span style="color:rgb(63,63,63);">######</span></p><p><strong><span style="color:rgb(63,63,63);">案例2:编写脚本,安装软件,自动启动,开机自起</span></strong></p><p>yum -y install vsftpd systemctl restart vsftpd systemctl enable vsftpd </p><p>yum -y install vsftpd &>/dev/null</p><p>systemctl restart vsftpd</p><p>systemctl enable vsftpd &>/dev/null</p><p>echo -e "\033[33m vsftpd<span style="font-family:'宋体';">服务启动完成,请访问</span><span style="font-family:Calibri;">ftp</span><span style="font-family:'宋体';">:</span><span style="font-family:Calibri;">//172.25.0.11 </span><span style="font-family:'宋体';">验证 </span><span style="font-family:Calibri;">\033[0m" </span></p><p>firefox ftp://172.25.0.11</p><p><strong><span style="color:rgb(63,63,63);">####################################################</span></strong></p><p><strong><span style="color:rgb(63,63,63);">五、变量</span></strong></p><p><strong><span style="color:rgb(63,63,63);">(数字,字母,下划线)</span></strong></p><p><strong><span style="color:rgb(63,63,63);">自定义变量 </span></strong>1.<span style="font-family:'宋体';">定义:变量名</span><span style="font-family:Calibri;">=</span><span style="font-family:'宋体';">变量值 (<strong><span style="font-size:16px;color:#ff0000;">中间不能有空格、特殊符号、数字开头</span></strong>)</span></p><p> 2.<span style="font-family:'宋体';">调用</span><span style="font-family:Calibri;">echo 变量名 #a=12 #b=13 #echo a a b #echo 0 当前所在的进程或脚本名
echo 1 <span style="font-family:'宋体';">第</span><span style="font-family:Calibri;">1</span><span style="font-family:'宋体';">个参数; </span><span style="font-family:Calibri;">echo 1 <span style="font-family:'宋体';">第</span><span style="font-family:Calibri;">1</span><span style="font-family:'宋体';">个参数; </span><span style="font-family:Calibri;">echo 2 第2个参数;echo 3 </span><span style="font-family:'宋体';">第</span><span style="font-family:Calibri;">3</span><span style="font-family:'宋体';">个参数</span></p><p>echo 3 </span><span style="font-family:'宋体';">第</span><span style="font-family:Calibri;">3</span><span style="font-family:'宋体';">个参数</span></p><p>echo * 所有位置变量的值
echo # <span style="font-family:'宋体';">已加载的位置变量的个数</span></p><p>echo # <span style="font-family:'宋体';">已加载的位置变量的个数</span></p><p>echo <span style="font-family:'宋体';">当前运行进程的</span><span style="font-family:Calibri;">PID</span><span style="font-family:'宋体';">号 </span><span style="font-family:Calibri;">#ps aux | grep PID</span></p><p>echo <span style="font-family:'宋体';">当前运行进程的</span><span style="font-family:Calibri;">PID</span><span style="font-family:'宋体';">号 </span><span style="font-family:Calibri;">#ps aux | grep PID</span></p><p>echo ? 命令执行后的返回状态,0表示正常,1或其他值表示异常
######
[root@server0 ~]# vim /bin/myuser
#!/bin/bash
useradd 1</p><p>echo " 1</p><p>echo " 2” | passwd –stdin 1</p><p>[root@server0 ~]# chmod +x /bin/myuser</p><p>[root@server0 ~]# myuser lili 123 /<span style="font-family:'宋体';">将</span><span style="font-family:Calibri;">myuser</span><span style="font-family:'宋体';">变成一个命令 </span></p><p>[root@server0 ~]#/bin/myuser lili 123</p><p>##########</p><p>ping -c 2 192.168.4.254</p><p>##########################################################################</p><p><strong><span style="color:rgb(63,63,63);">六、引号</span></strong></p><p><strong><span style="color:#ff0000;">双引号“ ”:引用整体 </span><span style="color:rgb(63,63,63);"> “a b” != a b; “ab” == ab</span></strong></p><p><strong><span style="color:#ff0000;">单引号' ':引用整体,屏蔽特殊符号命令</span><span style="color:rgb(63,63,63);">等 a=11 “ 1</p><p>[root@server0 ~]# chmod +x /bin/myuser</p><p>[root@server0 ~]# myuser lili 123 /<span style="font-family:'宋体';">将</span><span style="font-family:Calibri;">myuser</span><span style="font-family:'宋体';">变成一个命令 </span></p><p>[root@server0 ~]#/bin/myuser lili 123</p><p>##########</p><p>ping -c 2 192.168.4.254</p><p>##########################################################################</p><p><strong><span style="color:rgb(63,63,63);">六、引号</span></strong></p><p><strong><span style="color:#ff0000;">双引号“ ”:引用整体 </span><span style="color:rgb(63,63,63);"> “a b” != a b; “ab” == ab</span></strong></p><p><strong><span style="color:#ff0000;">单引号' ':引用整体,屏蔽特殊符号命令</span><span style="color:rgb(63,63,63);">等 a=11 “ a” != ’a'</span></strong></p><p><strong><span style="color:#ff0000;">反引号` `:等于 a'</span></strong></p><p><strong><span style="color:#ff0000;">反引号` `:等于 () ,引用命令,将该命令的执行结果作为参数
例:每周五使用tar备份一个/var/log
vim /root/test.sh
time=`+%Y%m%d%H%M%S`
tar -czPf /root/log.time</span>.tar.gz /var/log <a target="_blank"></a>&>/dev/null</p><p> 01 03 * * 5 /root/test.sh &>/dev/null</p><p><strong><span style="color:rgb(128,0,0);">案例补充:</span></strong></p><p><strong><span style="color:rgb(128,0,0);background:rgb(255,255,255);">1 #service vsftpd status</span></strong></p><p><strong><span style="color:rgb(128,0,0);background:rgb(255,255,255);"> [root@server0 ~]# chen="chen gui zhen"</span></strong></p><p><strong><span style="color:rgb(128,0,0);background:rgb(255,255,255);">2 [root@server0 ~]# set | grep chen</span></strong></p><p>chen='chen gui zhen'</p><p><strong><span style="color:rgb(128,0,0);">3 当前用户的环境变量USER-用户名、LOGNAME-登录名、HOME-宿主目录、SHELL-登录Shell、HOSTNAME-主机名、TERM-终端类型:</span></strong></p><p>[root@server0 ~]# echo time</span>.tar.gz /var/log <a target="_blank"></a>&>/dev/null</p><p> 01 03 * * 5 /root/test.sh &>/dev/null</p><p><strong><span style="color:rgb(128,0,0);">案例补充:</span></strong></p><p><strong><span style="color:rgb(128,0,0);background:rgb(255,255,255);">1 #service vsftpd status</span></strong></p><p><strong><span style="color:rgb(128,0,0);background:rgb(255,255,255);"> [root@server0 ~]# chen="chen gui zhen"</span></strong></p><p><strong><span style="color:rgb(128,0,0);background:rgb(255,255,255);">2 [root@server0 ~]# set | grep chen</span></strong></p><p>chen='chen gui zhen'</p><p><strong><span style="color:rgb(128,0,0);">3 当前用户的环境变量USER-用户名、LOGNAME-登录名、HOME-宿主目录、SHELL-登录Shell、HOSTNAME-主机名、TERM-终端类型:</span></strong></p><p>[root@server0 ~]# echo USER LOGNAME LOGNAME HOME SHELL SHELL HOSTNAME TERM TERM UID PATH ..</p><p>root root /root /bin/bash server0.example.com xterm-256color</p><p> </p><p>4 环境变量PS1表示Shell环境的一级提示符,即命令行提示符(\u 用户名、\h 主机名、\W 工作目录、\$ 权限标识):[\u@\h \W]\$</p><p>环境变量PS2表示二级提示符,出现在强制换行、at任务编辑等场合:# echo PATH ..</p><p>root root /root /bin/bash server0.example.com xterm-256color</p><p> </p><p>4 环境变量PS1表示Shell环境的一级提示符,即命令行提示符(\u 用户名、\h 主机名、\W 工作目录、\$ 权限标识):[\u@\h \W]\$</p><p>环境变量PS2表示二级提示符,出现在强制换行、at任务编辑等场合:# echo PS2
使用env可查看所有环境变量:# env
使用set可查看所有变量(包括env能看到的环境变量):# set
全局变量:操作系统的变量
PATH:命令搜索路径(目录),为ls、cat、find等提供路径搜索 ,对所有操作系统都很重要 </p><p>PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin</p><p>源码装的软件都不会放在<a target="_blank">PATH</a>路径下,需要自己做个链接,或加到PATH路径下PATH= PATH:命令搜索路径(目录),为ls、cat、find等提供路径搜索 ,对所有操作系统都很重要 </p><p>PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin</p><p>源码装的软件都不会放在<a target="_blank">PATH</a>路径下,需要自己做个链接,或加到PATH路径下PATH= PATH:/root/
export i=5 或 x=8;export x
局部变量:自己写的变量
i=5 脚本执行完就失效