一、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) /循环
do
useradd $i >>ok.log 2>>error.log /日志追加写入
echo “123” | passwd --stdin user$i &>>2.log
done
#######
ls >1.log 2>&1 = ls &>1 效果等同
#######
mail -s warning root < mail.txt 再将文件发送到手机
监控【CPU】90% 》 邮件
####
top -P 按cpu排序 -M 按内存排序
uptime
free 看内存 小于哪个值用 if判断
echo“CPU超了” | mail -s warning abc@139.com
#######
@管道操作
linux设计思想
small 【优点:快,漏洞少bug】 【缺点:功能少,依赖关系】
用管道可以使出错率减小
who | wc -l 看有多少人连接电脑
[root@svr5 ~]# yum list | grep cluster
####################################################
三、脚本的编写
1.新建文件(.sh)
2.编写内容(#!/bin/bash;#版本号、描述信息;)
3.赋予权限,执行(chmod +x )
执行脚本:有权限的# ./test.sh 相对路径、#/root/test.sh 绝对路径;
无权限的: 1.开子进程的--#bash test.sh #sh test.sh ksh/tcsh/sh #python test.py
2.不开子进程的--#source test.sh # . test.sh (修改系统脚本时,相当于reboot重起可立即生效)
printf “abc” 用python无法解释
which perl/python
#!/bin/bash
o=”chen”
exit
[root@server0 ~]# bash 3.sh
[root@server0 ~]# echo $o //开子进程的方式bash/sh执行此脚本没有任何变化
[root@server0 ~]# source 3.sh
[root@server0 ~]# echo $o //不子进程的方式source 执行此脚本会退出远程
chen
#######
[root@server0 ~]# history | wc -l
58
[root@server0 ~]# . /etc/profile 或 [root@server0 ~]# source /etc/profile
[root@server0 ~]# history | wc -l
10
###############################################################################
四、案例1:配置yum软件源
[root@server0 ~]# vim test.sh
#!/bin/bash
echo -e " 33[31m 准备配置yum源..