也不好归类,就统一放这里吧。如果以后用得着,来这里捞捞。
一,两个if小脚本
增加用户和设置密码
#!/bin/bash
# version:0.6
read -p "请输入用户名:" user
read -p "请输入密码:" pass
#if [ ! -z "$user" ]; then
# if [ ! -z "$pass" ]; then
# useradd "$user"
# echo "$pass" | passwd --stdin "$user"
# fi
#fi
#if [[ ! -z "$user" && ! -z "$pass" ]]; then
# useradd "$user"
# echo "$pass" | passwd --stdin "$user"
#fi
#if [ ! -z "$user" -a ! -z "$pass" ]; then
# useradd "$user"
# echo "$pass" | passwd --stdin "$user"
#fi
if [ ! -z "$user" ] && [ ! -z "$pass" ]; then
useradd "$user"
echo "$pass" | passwd --stdin "$user"
fi
判断机算机CPU型号和创建文件
#!/bin/bash
if grep -q AMD /proc/cpuinfo; then
echo "AMD CPU"
fi
if grep -q Intel /proc/cpuinfo; then
echo "Intel CPU"
fi
if ! mkdir "/media/cdrom"
then
echo "failed to create cdrom directory."
fi
if ! yum -y -q install ABC
then
echo "failed to install soft ABC."
fi
if systemctl is-enabled $1 &> /dev/null; then
echo "$1是开机自启动项。"
else
echo "$1不是开机自启动项。"
fi
二,nginx一键安装脚本
#!/bin/bash
# 功能描述:一键源码安装NGINX软件包
# 定义不同的颜色属性
setcolor_failure="echo -en \\033[91m"
setcolor_success="echo -ne \\033[32m"
setcolor_normal="echo -e \\033[0m"
# 判断是否以管理员身份执行脚本
if [[ $UID -ne 0 ]]; then
$setcolor_failure
echo -n "请以管理员身份运行该脚本"
$setcolor_normal
exit
fi
# 判断系统中是否存在wget下载工具
# wget 使用-c选项可以开启断点续传功能
if rpm --quiet -q wget; then
wget -c http://nginx.org/download/nginx-1.14.0.tar.gz
else
$setcolor_failure
echo -n "未找到wget,请先安装该软件"
$setcolor_normal
exit
fi
# 如果没有nginx帐户,则脚本自动创建此帐户
if ! id nginx &> /dev/null; then
adduser -s /sbin/nologin nginx
fi
# 测试是否存在正确的源码包软件
# 在源码编译安装前,先安装相关依赖包
# gcc:C语言编译器,pcre-devel:Perl兼容的正则表达式库
# zlib-devel:gzip压缩库,openssl-devel:Openssl加密库
if [[ ! -f nginx-1.14.0.tar.gz ]]; then
$setcolor_failure
echo -n "未找到nginx源码包,请先正确下载该 软件..."
$setcolor_normal
exit
else
yum -y install gcc pcre-devel zlib-devel openssl-devel
clear
$setcolor_success
echo -n "接下来花几分钟时间编译源码安装nginx。。。"
$setcolor_normal
sleep 6
tar -zxvf nginx-1.14.0.tar.gz
# 编译源码安装nginx,指定帐户和组,指定安装路径,开户需要的模块,禁用不需要的模块
cd nginx-1.14.0/
./configure \
--user=nginx \
--group=nginx \
--prefix=/data/server/nginx \
--with-stream \
--with-http_ssl_module \
--with-http_stub_status_module \
--without-http_autoindex_module \
--without-http_ssi_module
make
make install
fi
# CHECK安装后
if [[ -x /data/server/nginx/sbin/nginx ]]; then
clear
$setcolor_success
echo -n "一键部署nginx已经完成"
$setcolor_normal
fi
三,case语句生成的菜单
#!/bin/bash
clear
echo -e "1.查看网卡信息"
echo -e "2.查看内存信息"
echo -e "3.查看磁盘信息"
echo -e "4.查看CPU信息"
read -p "请输入选项[1~4]:" key
case $key in
1)
ifconfig | head -2;;
2)
mem=$(free |grep Mem |tr -s " " |cut -d" " -f7)
echo "本机内存剩余容量为:${mem}K.";;
3)
root_free=$(df |grep /$ |tr -s " " |cut -d" " -f4)
echo "本机根分区剩余容量为:${root_free}K.";;
4)
cpu=$(uptime |tr -s " " |cut -d" " -f12)
echo "本机CPU 15min的平均负载为:$cpu.";;
*)
echo "输入有误,超出1~4的范围";;
esac
四,for的不同用法
C语言风格的for
#!/bin/bash
# C语言风格的FOR循环语法格式示例
for ((i=1;i<=5;i++)); do
echo $i
done
for ((i=1,j=5;i<=5;i++,j--)); do
echo "$i $j"
done
从命令行获取for循环
#!/bin/bash
for i; do
echo $i
done
闰年判断
#!/bin/bash
# 条件1:能被4整除但不能被100整除 条件2:能被400整除
# 满足条件1或条件2之一,就是闰年
for i in {1..5000}
do
if [[ $[i%4] -eq 0 && $[i%100] -ne 0 || $[i%400] -eq 0 ]]; then
echo "$i:是闰年"
else
echo "$i:非闰年"
fi
done
seq生成循环序列
#!/bin/bash
# 可能使用$()或``对命令进行扩展
net="192.168.4"
for i in $(seq 254); do
echo "ping -c2 -i0.2 -W1 $net.$i &> /dev/null"
if [ $? -eq 0 ]; then
echo "$net.$i is up."
else
echo "$net.$i is down."
fi
done
五,想画个象棋棋盘
结果没画好
#!/bin/bash
# 功能描述:打印国际象棋棋盘
for i in {1..8};do
for j in {1..8};do
sum=${i+j}
if [[ $[sum%2] -ne 0 ]]; then
echo -ne "\033[41m \033[0m"
else
echo -ne "\033[47m \033[0m"
fi
done
echo
done
六,99乘法法
#!/bin/bash
# 打印9*9乘法表
for ((i=1;i<=9;i++)); do
for ((j=1;j<=i;j++)); do
echo -n "$i*$j=$[i*j] "
done
echo
done
image.png
网友评论