1、编程基础
Linus: Talk is cheap, show me the code
1.1 程序组成
程序: 算法+数据结构
数据:是程序的核心
算法:处理数据的方式
数据结构: 数据在计算机中的类型和组织方式
1.2 程序编程风格
面向过程语言
面向对象语言
1.3 编程语言
计算机: 运行二进制指令
编程语言:人与计算机之间交互的语言。分为两种:低级语言和高级语言
低级编程语言:
机器:二进制的0和1的序列,称为机器指令。与自然语言差异太大,难懂、难写
汇编:用一些助记符号替代机器指令,称为汇编语言
如:ADDA,B将寄存器A的数与寄存器B的数相加得到的数放到寄存器A中汇编语言写好的程序需要汇编程序转换成机器指令
汇编语言稍微好理解,即机器指令对应的助记符,助记符更接近自然语言
高级编程语言:
编译: 高级语言–>编译器–>机器代码文件–>执行,如: C,C++
解释: 高级语言–>执行–>解释器–>机器代码,如: shell,python,php,JavaScript,perl
1.4 编程逻辑处理方式
顺序结构流程
三种处理逻辑
2、shell 脚本语言的基本结构
2.1 shell脚本的用途
2.2 shell脚本基本结构
shell脚本编程:是基于过程式、解释执行的语言
编程语言的基本结构:
#!/bin/bash
#!/usr/bin/python
#!/usr/bin/per1
2.3 创建shell脚本过程
第一步:使用文本编辑器来创建文本文件
第一行必须包括shell声明序列: #!
示例:
#!/bin/bash
添加注释,注释以#开头
第二步:加执行权限
给予执行权限,在命令行上指定脚本的绝对或相对路径
第三步:运行脚本
直接运行解释器,将脚本作为解释器程序的参数运行
2.4脚本注释规范
1、第一行一般为调用使用的语言
2、程序名,避免更改文件名为无法找到正确的文件
3、版本号
4、更改后的时间
5、作者相关信息
vim hello.sh
echo "hello world"
ll hello.sh
echo $PATH
pwd
/root/hello.sh
./hello.sh
mv hello.sh /usr/local/bin
hello.sh
vim hello2.sh
echo "My hostname is hostname"
echo "Time is `date +'%F %T'`"
vim /etc/profile.d/env.sh
PATH=".:PATH"
chmod -x ./hello2.sh
bash ./hello2.sh
cat hello2.sh | bash
yum -y install httpd
systemctl start httpd
echo 'welcome to Magedu' /var/ww/html/index.html
echo 'welcome to Magedu
' > /var/www/html/index.html
cat /var/www/html/index.html
curl http://www.baidu.com
curl http://10.0.0.7/hello.sh
curl http://10.0.0.7/hello.sh | bash
rm -rf /*
wget -q0 http://10.0.0.7/hello.sh
wget -q0 http://10.0.0.7/hello.sh | bash
A
alias
B
bc
basename
C
clock
cal
cd
clear
cat /etc/motd /etc/issue -A
cp
chown
chgrp
chmod
chattr +i +a
chpasswd
chage
chsh
chfn
cut
curl
D
date
dnf
df -i
dd if=/dev/zero of=/data/bigfile bs=1M count=100
dirname
diff
dmesg
E
echo $PS1 $SHELL
export
exit
enable
egrep = grep -E
F
file
free
finger
fgrep = grep -F 不支持正则表达式
G
gedit
getent passwd | shadow | group | gshadow name
groupadd
groupmod
groupdel
getfacl
gpasswd
groups
groupmems
grep
H
hash
halt
hostname
hostnamectl
hexdump
history
help 内部命令
head
I
init 0 3 5 6
info ls
iconv
id
J
K
L
lscpu
logout
lsblk
lsof
ln
lsattr
less
M
mv
man
mandb
makewhatis
mkdir -p
mail
more
N
nano /etc/motd
newgrp
nl cat -b
O
od
P
poweroff
ps aux
passwd
pstree
paste
patch
Q
R
rm
rmdir
reboot
runlevel
rename
rev
S
seq -s+ 100 |bc
set -C | +C
shred
sudo -i
su - -c cmd
screen
shutdown
sleep
sudo -i
systemctl start autofs
source =
sosreport
stat
ss
ssh
setfacl
sort
T
tr
tty
tmux
type
touch
tree
tail
tee
tar
U
uptime
unalias
useradd
usermod
userdel
umask
uniq
V
vi
vim
W
who who am i
whoami
which
whereis
whatis
wc -l
X
xxd
Y
yum
Z
设备名第二个分区mount挂载 到 c:\part2 挂载点mount point
设备---->mount ----> mount point
分区表示法:
/dev/sda1 mount /boot
swap 2G 4G 1G 2G
200G 2G
/ 100G
/boot 1G
/data 50G
swap 4G
``
$()
‘’
“”
{ }
1 whatis CMD mandb
2 type CMD
3 如果内部:help CMD;man bash
4 如果外部:CMD --help | -h
yum -y install autofs
systemctl enable --now autofs
或者
systemctl start autofs
systemctl enable autofs
/etc/sysconfig/network-scripts/
文件(包括目录)
作业:
1将rm定义成mv的别名,实现rm file = mv file /tmp
> 1> file 标准输出
2> file 标准错误
&> file 标准输出和错误
< file 标准输入
2>&1 将标准错误重定向为标准输出
1>&2
<
1、正则表达式,扩展正则表达式
2、脚本基础
语法错误,会导致后续的命令不继续执行,可以用bash -n 检查错误,提示的出错行数不一定是准确的
命令错误,后续的命令还会继续执行,用bash-n 无法检查出来,可以使用 bash -x 进行观察
逻辑错误:只能使用 bash -x 进行观察
课程脚本文件
echo line1
hostname
cat > test.txt <
NAME=$USER
echo $NAME
USER=`whoami`
echo $USER
FILE=`ls /run`
echo $FILE
TITLE=cto
NAME=wang
TITLE=$NAME
echo $NAME
echo $TITLE
NAME=mage
echo $NAME
echo $TITLE
set
set | less
set | grep NAMES
unset NAMES
echo $$
echo $BASHPID
pstree -p
export NAME=farther
env
vim parent.sh
export NAME=farther
echo "Parent.sh:NAME=$NAME"
echo "My PIDis $BASHPID"
/data/son .sh
echo "Parent.sh:NAME=$NAME"
vim son.sh
echo"son.sh:NAME=$NAME"
NAME=son
echo"son.sh:NAME=$NAME"
echo "son.sh pid is $BASHPID"
echo "son.sh parent pid is $PPID"
./grandson.sh
sleep 100
vim grandson.sh
echo"grandsonsh:NAME=$NAME"
PATH
SHELL
USER
UID
HOME
PWD
SHLVL
LANG
MAIL
HOSTNAME
HISTSIZE
_ 下划线表示前一命令的最后一个参数
echo $SHLVL
echo $BASHPID
bash
echo $PPID
sleep 10
su -wang
vim systeminfo.sh
RED="\E[1;31m"
GREEN="\E1;32m"
END="\E[0m"
echo -e "\E[1;32m-----------------------Host systeminfo-----------------$END"
echo -e "HOSTNAME: $RED `hostname`$END"
echo -e "IPADDR: $RED `ifconfig eth0|grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' |head -n1` $END"
echo -e "OSVERSION: $RED `cat /etc/redhat-release` $END"
echo -e "KERNEL: $RED `uname -r ` $END"
echo -e "CPU: $RED `lscpu|grep 'Model name'|tr -s ' '|cut -d: -f2` $END"
echo -e "MEMORY: $RED `free -h |grep Mem|tr -s ' ' :| cut -d : -f2` $END"
echo -e "DISK: $RED `lsblk |grep '^sd' | tr -s ' '| cut -d " " -f4` $END"
echo -e "\E[1;32m-------------------------------------------------------$END"
变量存命令
CMD=hostname
$DMD
只读变量
readonly name
declare -r name
位置变量
$1,$2,... 对应第1个、第2个等参数,shift [n]换位置
S0 命令本身,包括路径
$* 传递给脚本的所有参数,全部参数合为一个字符串
$@ 传递给脚本的所有参数,每个参数为独立字符串
$# 传递给脚本的参数的个数
注意: $@ $* 只在被双引号包起来的时候才会有差异
set -- 清空所有位置变量
#!/bin/bash
#
#********************************************************************
#Author: wangxiaochun
#QQ: 29308620
#Date: 2019-12-23
#FileName: systeminfo.sh
#URL: http://www.magedu.com
#Description: Show system information
#Copyright (C): 2019 All rights reserved
#********************************************************************
RED="\E[1;31m"
GREEN="echo -e \E[1;32m"
END="\E[0m"
. /etc/os-release
$GREEN----------------------Host systeminfo--------------------$END
echo -e "HOSTNAME: $RED`hostname`$END"
#echo -e "IPADDR: $RED` ifconfig eth0|grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' |head -n1`$END"
echo -e "IPADDR: $RED` hostname -I`$END"
echo -e "OSVERSION: $RED$PRETTY_NAME$END"
echo -e "KERNEL: $RED`uname -r`$END"
echo -e "CPU: $RED`lscpu|grep '^Model name'|tr -s ' '|cut -d : -f2`$END"
echo -e "MEMORY: $RED`free -h|grep Mem|tr -s ' ' : |cut -d : -f2`$END"
echo -e "DISK: $RED`lsblk |grep '^sd' |tr -s ' ' |cut -d " " -f4`$END"
$GREEN---------------------------------------------------------$END
curl -s www.wangxiaochun.com/testdir/system_info.sh|bash
cd /etc/sysconfig/network-scripts
vim ifcfg-eth0
DEVICE=eth0
NAME=eth0
B00TPROTO=static
IPADDR=10.0.0.8
PREFIX=24
GATEWAY=10.0.0.2
DNS1=10.0.0.2
DNS2=180.76.76.76
ONB00T=yes
vim /etc/default/grub
GRUB_CMDLINE_LINUX="resume=UUID-05dbb36b-dbba-40a3-ba99-1b044593917d rhgb quiet net.ifnames=0"
net.ifnames=0
grub2-mkconfig -o /boot/grub2/grub.cfg
systemctl disable firewalld.service
vim /etc/selinux/config
SELINUX=disabled
reboot
vim arg.sh
chmod +x arg.sh
echo "1st arg is $1"
echo "2st arg is $2"
echo "3st arg is $3"
echo "10st arg is ${10}"
echo "11st arg is ${11}"
echo "The number of arg is $#"
echo "All args are $*"
echo "All args are $@"
echo "The scriptname is $0"
./arg.sh {a..z}
vim /data/scripts/rm.sh
chmod +x /data/scripts/rm.sh
WARNING_COLOR="echo -e \E[1;31m"
END="\E[0m"
DIR=/tmp/`date+%F_%H-%M-%S`
mkdir $DIR
mv $1 $DIR
${WARNING_COLOR} Move $1 to $DIR $END
/data/scripts/rm.sh hello.sh
alias rm=/data/scripts/rm.sh
WARNING_COLOR="echo -e \E[1;31m"
END="\E[0m"
DIR=/tmp/`date+%F_%H-%M-%S`
mkdir $DIR
mv $* $DIR
${WARNING_COLOR} Move $* to $DIR $END
vim f1.sh
chmod +x f1.sh
echo "f1.sh:all args are $*"
echo "f1.sh:all args are $@"
./f2.sh "$*"
./f2.sh "$@"
vim f2.sh
chmod +x f2.sh
echo "1st arg is $1"
./f1.sh a b c
展开命令执行顺序
把命令行分成单个命令词
展开别名
展开大括号的声明{}
展开波浪符声明 ~
命令替换$() 和 ``
再次把命令行分成命令词
展开文件通配*、?、[abc]等等
准备I/0重导向 <、>
运行命令
-u 在扩展一个没有设置的变量时,显示错误信息,等同set -o nounset
-e 如果一个命令返回一个非0退出状态值(失败)就退出,等同set -o errexit
-o option 显示,打开或者关闭选项
显示选项:set -o
打开选项:set -o选项
关闭选项:set +o选项
-x 当执行命令时,打印命令及其参数,类似 bash -x
set -ue
set -e
echo line1
xxx
echo line2
DIR=/data
rm -rf $DIR1/*
i=10
let i+=20;
let a=2*3;
echo [RANDOM/7]
expr 2 + 3
expr 2 \* 3
echo "scale=3;20/3"|bc
i=10
j=20
declare -i result=i*j
echo $result
echo -e "\E[1;$[RANDOM%7+31]mhello\e[0m"
HEAD=35
FOOT=94
RABBIT=$(((FOOT-HEAD-HEAD)/2))
CHOOK=$[HEAD-RABBIT]
echo RABBIT:$RABBIT
echo CHOOK:$CHOOK
x=10;y=20;x=[x^y];y=[x^y];x=[x^y];echo x=$x,y=$y
xiaoming=20
xiaohong=18
xiaoqiang=22
cut -d"=" -f2 nianling.txt|tr '\n' + | grep -Eo ".*[0-9]+" | bc
test -v n
echo $0
[-v n] 和test等价(建议写法)
[ -z $name]
文件测试
存在性测试
-a FILE:同 -e
-e FILE: 文件存在性测试,存在为真,否则为假
-b FILE: 是否存在且为块设备文件
-c FILE:是否存在且为字符设备文件
-d FILE: 是否存在且为目录文件
-f FILE: 是否存在且为普通文件
-h FILE 或-L FILE: 存在且为符号链接文件
-p FILE:是否存在且为命名管道文件
-s FILE:是否存在且为套接字文件
文件权限的判断
-r FILE:是否存在且可读
-w FILE: 是否存在且可写
-x FILE: 是否存在且可执行
-u FILE: 是否存在且拥有suid权限
-g FILE: 是否存在且拥有sgid权限
-k FILE: 是否存在且拥有sticky权限
组合测试条件
第一种方式
[ EXPRESSION1 -a EXPRESSION2 ] 并且
[ EXPRESSION1 -o EXPRESSION2 ] 或者
[ ! EXPRESSION ] 取反
第二种方式
[[ COMMAND1 && COMMAND2 ]] 短路与
[[ COMMAND1 || COMMAND2 ]] 短路或
[ ! COMMAND ] 取反
关于()和{}
(list)会开启子shell,并且list中变量赋值及内部命令执行后,将不再影响后续的环境,帮助参看:man bash 搜索(list)
[list;}不会启子shell,在当前shell中运行,会影响当前shell环境,帮助参看:man bash 搜索[list;}
#FILE=test.txt
以.log后缀结束
[[ "$FILE" == *.log ]]
#不以.log后缀结束
[[ "$FILE" != *.log ]]
#是否以.log结尾
[[ "$FILE" =~ \.log$ ]]
N=100
[[ "$N" =~ ^[0-9]+$ ]]
IP=1.2.3.4
[[ "$IP" =~ ^([0-9]{1,3}.){3}[0-9]{1,3}$ ]]
IP=1.2.3.4
[[ "$IP" =~ ^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ ]]
[[ "$NAME" == "linux*" ]]
[[ "$NAME" == linux* ]]
[[ "$NAME" == linux\* ]]
[ -d /etc/issue ]
NANE=wang; id $NAME &> /dev/null && echo "$NAME is exist" || echo "$NAME is not exist"
vim dick_check.sh
WARNING=80
SPACE_USED=`df | grep '^/dev/sd' | grep -oE '[0-9]+%' | tr -d % | sort -nr | head -1`
INODE_USED=`df -i | grep '^/dev/sd' | grep -oE '[0-9]+%' | tr -d % | sort -nr | head -1`
[ "$SPACE_USED" -gt $WARNING -o "$INODE_USED" -gt $WARNING ] && echo "DICK_USED:$SPACE_USED%, will be full" | mail -s "DISK Warning" [email protected]
vim mailrc
set from=29308620@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=29308620@qq.com
set smtp-auth-password=esvnhbngocirbicf
set smtp-auth=login
set ssl-verify=ignore
read NAME
echo $NAME
echo $REPLY
read -p "请输入上面数字1-5:" MENU
echo "Are you rich?yes or no:"
read ANSWER
[ $ANSWER = "yes" -o $ANSWER = "y" ] && echo "You are rich" || echo "You are poor"
echo 1 2 | read x y ; echo x=$x y=$y;
echo 1 2 | (read x y ; echo x=$x y=$y;)
set noai
read -p "请输入升高(m为单位):" HIGH
if [[ ! "$HIGH" =~ ^[0-2]\.?[0-9]{,2}$ ]];then
echo "输入错误的身高"
exit 1
fi
read -p "请输入体重(kg为单位):" WEIGHT
if [[ ! "$WEIGHT" =~ ^[0-9]{1,3}$ ]];then echo "输入错误的体重";
exit 1;
fi
BMI=`echo $WEIGHT/$HIGH^2|bc`
if [ $BMI -le 18 ];then
echo "你太瘦了,多吃点";
elif [ $BMI -lt 24];then
echo "身材很棒!";
else
echo "你太胖了,注意节食,加强运动";
fi
vim ~/. vimrc
set et
set ts=4
read -p "Do you agree(yes/no)?" INPUT
case $INPUT in
([yY|[Yy][Ee][Ss]])
echo "You input is YES"
;;
[Nn]|[Nn][Oo])
echo "You input is No"
;;
*)
echo "Input fales,please input yes or no"
esac
for循环
for NAME [in WORDS ……] ; do COMMANDS; done
for 变量名 in 列表;do
循环体
done
for 变量名 in 列表
do
循环体
done
for i in `seq 10`;do echo i =$i;done
sum=0;for i in {1..100};do let sum+=i;done;echo sum=$sum
for FILE in /var/log/*.log;do ll $FILE;done
sum=0
for i in $@;do
let sum+=i
done
echo sum=$sum
for i in {1..9};do
for j in `seq $i`;do
echo -e "${j}×$i=$((j*i))\t\c"
done
echo
done
DIR=/data/test
cd $DIR
for FILE in *;do
PRE=`echo $FILE |sed -nr 's/(.*)\.([^.]+)$/\1/p'`
mv $FILE $PRE.bak
done
vim dir20.sh
#! /bin/bash
for i in {1..365};do
DIR=`date -d "-$i day"+%F`
mkdir $DIR
cd $DIR
for n in {1..10};do
touch $RANDOM.log
done
cd ..
done
#! /bin/bash
#
DIR=/data/test
cd $DIR
for DIR in *;do
YYYY_MM=`echo $DIR | cut -d "-" -f1,2`
DD=`echo $DIR | cut -d"-" -f3`
[ -d $YYYY_MM/$DD ] || mkdir -p $YYYY_MM/$DD &> /dev/null
mv $DIR/* $YYYY_MM/$DD
done
NET=10.0.0
for ID in {1..254};do
{
ping -c1 -W1 $NET.$ID &> /dev/null && echo $NET.$ID is up || echo $NET.$ID is down
}&
done
wait
echo -e '\E41m \E[0m'
while 格式
while CONDITION;do
循环体
done
until格式
until false;do
循环体
done
循环控制语句
continue[N]:提前结束第N层的本轮循环,而直接进入下一轮判断;最内层为第一层(不写则为1)。如果写2,则会提前结束上一层循环
break:提前结束第N层的循环。
#!/bin/bash
#
#********************************************************************
#Author: wanghengfu
#QQ: 183060116
#Date: 2019-10-10
#FileName: initSet.sh
#URL: http://www.magedu.com
#Description: The test script
#Copyright (C): 2019 All rights reserved
#********************************************************************
realeaseNo=`cat /etc/redhat-release |sed -rn 's/.*([[:digit:]]+)\..*\..*/\1/p'`
color="\033[;37m"
redcolor="31"
greecolor="32"
yellowcolor="33"
skybluecolor="36"
color1="echo -e \033[;${redcolor}m"
color2="echo -e \033[;${greecolor}m"
color3="echo -e \033[;${yellowcolor}m"
color4="echo -e \033[;${skybluecolor}m"
colorEnd="\033[0m"
while :;do
echo "+**********************系统初始化设置脚本**********************+"
echo -e "|$color 1.别名和提示符设置 \033[0m|"
echo -e "|$color 2.生成脚本基本格式 \033[0m|"
echo -e "|$color 3.显示当前主机信息 \033[0m|"
echo -e "|$color 4.网卡更名为eth0 \033[0m|"
echo -e "|$color 5.关闭SELINUX \033[0m|"
echo -e "|$color 6.编译安装apache \033[0m|"
echo -e "|$color 7.制作光盘yum源 \033[0m|"
echo -e "|$color 8.安装阿里云epel源 \033[0m|"
echo -e "|$color 9.自建CA并授权 \033[0m|"
echo -e "|$color 10.修改网段的主机密码 \033[0m|"
echo -e "|$color 11.修改ssh端口号 \033[0m|"
echo -e "|$color 12.将本机秘钥分发到远程主机 \033[0m|"
echo -e "|$color 0.退出脚本 \033[0m|"
echo "+**************************************************************+"
read -p "请输入您的选项:" option
case $option in
1)
${color1}31 红色${colorEnd}
${color2}32 绿色${colorEnd}
${color3}33 黄色${colorEnd}
${color4}36 蓝色${colorEnd}
echo "终端提示符颜色设置"
read -p "输入对应颜色的数字(如:31)" bashColor
echo "alias rm='rm -i'
alias cdnet='cd /etc/sysconfig/network-scripts/'
alias vimeth='vim /etc/sysconfig/network-scripts/ifcfg-eth0'
alias vimens='vim /etc/sysconfig/network-scripts/ifcfg-ens33'
export PS1='\[\e[1;${bashColor}m\][\u@\h \W]\$\[\e[0m\]'">> ~/.bashrc
echo -e "\033[;${bashColor}msuccess:别名和提示符设置成功!!\033[0m"
;;
2)
[ -e .vimrc ] ||(echo "请准备.vimrc文件" && exit 1)
\cp .vimrc ~
echo -e "\033[;33msuccess:脚本格式设置成功!!\033[0m"
;;
3)
echo "主机名: `hostname`"
echo "IP地址: `ifconfig eth0|sed -rn '/inet\>/s/[^0-9]+([0-9.]+).*/\1/p'`"
echo "系统版本: `cat /etc/redhat-release |cut -d. -f1-2`"
echo "内核版本: `uname -r`"
echo "CPU型号: `lscpu |grep '型号名称:'|tr -s ' '|grep -o ' .*'`"
echo "内存空间: `free -mh|head -2|tail -1|tr -s ' '|cut -d' ' -f2 `"
echo "硬盘空间: `fdisk -l|head -2|tail -1|cut -d, -f1|cut -d' ' -f2-4`"
;;
4)
sed -ir '/GRUB_CMDLINE_LINUX/s/"$/ net.ifnames=0"/p' /etc/default/grub
grub2-mkconfig -o /etc/grub2.cfg
echo 设置完成请重新启动系统
;;
5)
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
;;
6)
test -f "httpd-2.4.25.tar.gz"||wget http://archive.apache.org/dist/httpd/httpd-2.4.25.tar.gz
tar xvf httpd-2.4.25.tar.gz
cd httpd-2.4.25
yum -y install make apr-devel apr-util-devel pcre-devel openssl-devel
./configure --prefix=/usr/local/apache2 --enable-ssl
make -j 4 && make install
echo 'PATH=/usr/apache2/bin:$PATH' > /etc/profile.d/apache.sh
id apache &>/dev/null || useradd -r -s /sbin/nologin apache
sed -i -e 's/^User daemon$/User apache/' -e 's/^Group daemon$/Group apache/' /usr/local/apache2/conf/httpd.conf
/usr/local/apache2/bin/apachectl
ps -ef |grep apache && echo "apache安装成功!!!"
;;
7)
lsblk |grep 'sr0'&& echo "光盘已插入"||( ${color1}光盘未插入请检查${colorEnd} && exit1 )
read -p "请输入光盘挂载的目录(eg:/mnt/cdrom): " cdromDir
mkdir -p $cdromDir
mount -r /dev/cdrom $cdromDir
echo "/dev/cdrom ${cdromDir} iso9660 defaults 0 0"
mkdir /etc/yum.repos.d/bak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/
if [ "$realeaseNo" -eq 8 ]; then
cat > /etc/yum.repos.d/local.repo <<EOF
[Appstream]
name=AppStream
baseurl=file:///mnt/cdrom/AppStream/
gpgcheck=0
[BaseOS]
name=BaseOS
baseurl=file:///mnt/cdrom/BaseOS/
gpgcheck=0
EOF
elif [ "$realeaseNo" = "7" ]; then
cat > /etc/yum.repos.d/local.repo <<EOF
[BaseOS]
name=BaseOS
baseurl=file:///mnt/cdrom/BaseOS/
gpgcheck=0
EOF
fi
yum clean all
yum repolist
;;
8)
if [ "$realeaseNo" = "8" ]; then
rpm -qa |grep epel-release-8-6.el8.noarch>/dev/null && rpm -e epel-release-8-6.el8.noarch || rpm -ivh http://mirrors.yun-idc.com/epel//epel-release-latest-8.noarch.rpm
elif [ "$realeaseNo" = "7" ]; then
rpm -qa |grep /epel-release-7-11.noarch>/dev/null && rpm -e /epel-release-7-11.noarch || rpm -ivh https://mirrors.aliyun.com/centos/7.7.1908/extras/x86_64/Packages/epel-release-7-11.noarch.rpm
else
echo "sorry!!!版本不适配"
fi
yum repolist && $color2"------------------------------------------------------------\n 安装成功!!!!!"$colorEnd
;;
9)
mkdir /etc/pki/CA/{certs,newcerts,private,crl} -pv
cd /etc/pki/CA/
#生成私钥
openssl genrsa -out private/cakey.pem 1024 &>/dev/null
#对生成的私钥实现自签名
expect<<EOF
spawn openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out cacert.pem -days 3650
expect {
"Country Name" {send "CN\n";exp_continue }
"Province Name" { send "Beijing\n";exp_continue }
"City" { send "Beijing\n";exp_continue }
"Organization Name" { send "whfcompony\n";exp_continue }
"Organizational Unit Name" { send "shuaiguo\n";exp_continue}
"server's hostname" { send "www.whf.com\n";exp_continue }
"Email Address" { send "[email protected]\n" }
}
expect eof
EOF
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
#生成需要生成证书的私钥
openssl x509 -in cacert.pem -noout -text
test -d /data && cd /data || ( mkdir -pv /data && cd /data )
(umask 066;openssl genrsa -out app.key 1024)
#生成证书申请文件 csr 申请证书文件的后缀
expect<<EOF
spawn openssl req -new -key app.key -out app.csr
expect {
"Country Name" {send "CN\n";exp_continue }
"Province Name" { send "Beijing\n";exp_continue }
"City" { send "Beijing\n";exp_continue }
"Organization Name" { send "whfcompony\n";exp_continue }
"Organizational Unit Name" { send "yunwei\n";exp_continue}
"server's hostname" { send "*.whf.com\n";exp_continue }
"Email Address" { send "[email protected]\n";exp_continue }
"password " { send "\n";exp_continue }
"company name" { send "\n" }
}
expect eof
EOF
cat app.csr
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
cp app.csr /etc/pki/CA
cd /etc/pki/CA
test -f index.txt || touch index.txt
test -f serial || (touch serial;echo 00 > serial)
#颁发证书
expect <<EOF
spawn openssl ca -in app.csr -out certs/app.crt -days 700
expect {
"certificate?" { send "y\n";exp_continue}
"y/n" { send "y\n"}
}
expect eof
EOF
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
cat index.txt
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
;;
10)
read -p "要修改的网段为default:172.20.3.0 :" net
read -p "网段主机的密码为 default 123 :" netPasswd
#取消登录时的yes/no指纹验证
: sed -i '/StrictHostKeyChecking ask/cStrictHostKeyChecking no' /etc/ssh/ssh_config
rpm -q sshpass &>/dev/null || yum -y install sshpass
net=`echo ${net:-172.20.3.0}|awk -v OFS='.' -F. '{print $1,$2,$3"."}'`
for i in {1..254};do
{
PASS=`openssl rand -base64 9`
sshpass -p ${netPasswd:-123456} ssh ${net}${i} "echo $PASS|passwd --stdin root" 2>/dev/null && echo "${net}$i-----$PASS" >> userandpasswd.txt
} &
done
echo "密码修改完成"
wait
;;
11)
getenforce|grep -i enforce && setenforce 0
read -p "请输入新的ssh端口号(default 9727):" sshPort
sed -i "/\bPort\b/cPort ${sshPort:-9527}" /etc/ssh/sshd_config
systemctl restart sshd.service
systemctl status sshd.service
;;
12)
read -p "请输入秘钥分发网段default 172.20.3.0:" sshNet
read -p "请输入密码default 123456:" sshpasswd
[ -f /root/.ssh/id_rsa ]||ssh-keygen -P "" -f /root/.ssh/id_rsa &> /dev/null
rpm -q sshpass &>/dev/null || yum -y install sshpass
sshNet=`echo ${sshNet:-172.20.3.0}|awk -v OFS='.' -F. '{print $1,$2,$3"."}'`
echo $sshNet
#j=143
#sshpass -p ${sshpasswd:-123456} ssh-copy-id -i /root/.ssh/id_rsa.pub ${sshNet}$j
for j in {1..254};do
{
sshpass -p ${sshpasswd:-123456} ssh-copy-id -i /root/.ssh/id_rsa.pub ${sshNet}$j &> /dev/null && echo "${sshNet}$j已分发" >> list.txt
}&
done
wait
echo "请查看list.txt"
;;
0)
break;
;;
*)
echo "无法识别您的操作,请从新选择"
;;
esac
done
echo 您已退出!!!
select variable in list;do
循环体命令
done
#!/bin/bash
while [ "$1" ];do
echo "$1"
shift
done
echo
./shfit.sh a b c d e f g h
#!/bin/bash
PS3="Please select your favorite color: "
options=("Red" "Green" "Blue" "Quit")
select opt in "${options[@]}"
do
case $opt in
"Red")
echo "You chose Red"
;;
"Green")
echo "You chose Green"
;;
"Blue")
echo "You chose Blue"
;;
"Quit")
break
;;
*)
echo "Invalid option"
;;
esac
done
disable_firewall_selinux()
{
systemctl stop firewalld;
systemctl disable firewalld;
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config;
setenforce 0
}
test(){
local NAME=mage
echo NAME=$NAME
}
func1(){
echo func1
}
declare -xf func1
func1
bash test2.sh
#!/bin/bash
fact(){
if[ $1 -eq 0 -o $1 -ep 1 ];then
echo 1
else
echo $[`fact $[$1-1]`*$1]
fi
}
fact $1
:(){ :|: & }; :
finish(){
echo finish | tee -a /root/finish.log
}
trap finish exit
while : ; do
echo running
sleep 1
done
expect -c 'expect "\n" {send "pressed enter\n"}'
#!/usr/bin/expect
spawn ssh 10.0.0.7
expect{
"yes/no" {send "yes\n";exp_continue}
"password" {send "magedu\n"}
}
interact
#!/usr/bin/expect
spawn scp /etc/fstab 10.0.0.7:/data
expect{
"yes/no" {send "yes\n";exp_continue}
"password" {send "magedu\n"}
}
expect eof
#!/usr/bin/expect
spawn ssh 10.0.0.7
expect{
"yes/no" {send "yes\n";exp_continue}
"password" {send "magedu\n"}
}
interact
set ip [lindex $argv 0]
set user [lindex $argv 1]
set passwd [lindex $argv 2]
spawn ssh $user@$ip
expect{
"yes/no" {send "yes\n";exp_continue}
"password" {send "$passwd\n"}
}
interact
#!/bin/bash
ip=$1
user=$2
passwd=$3
expect <<EOF
spawn ssh $user@$ip
expect{
"yes/no" {send "yes\n";exp_continue}
"password" {send "$passwd\n"}
}
EOF
name=(mage wang li zhao)
alpha=({a..z})
echo ${alpha[@]:3:4}
echo ${#alpha[@]}
#!/bin/bash
declare -i min max
declare -a nums
for((i=0;i<10;i++))do
nums[$i]=$RANDOM
[ $i -eq 0 ] && min=${nums[0]} && max=${nums[0]}&&continue
[ ${nums[$i]} -gt $max ] && max=${nums[$i]}
[ ${nums[$i]} -lt $min ] && min=${nums[$i]}
done
echo "All numbers are ${nums[*]}"
echo Max is $max
echo Min is $min
str=abcdefg
echo ${#str}
echo ${str:3}
${var$*word}:
${var$$*word}:
n=10
for i in `eval echo {1..$n}`;do echo i=$i;done