美文网首页
shell入门12-几个练习shell知识点的脚本汇总

shell入门12-几个练习shell知识点的脚本汇总

作者: 万州客 | 来源:发表于2022-12-30 11:58 被阅读0次

也不好归类,就统一放这里吧。如果以后用得着,来这里捞捞。

一,两个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

相关文章

  • Shell入门笔记

    Shell脚本:Linux Shell脚本学习指南菜鸟教程 - Shell教程Linux入门 - Shell脚本是...

  • Shell 概述

    学习 Shell 主要包括的内容: Shell 脚本入门 Shell 变量 Shell 内置命令 Shell 运算...

  • shell脚本

    shell入门 脚本格式入门 脚本以!/bin/bash开头,指定解析器 第一个shell脚本 需求 创建shel...

  • shell入门学习(1)——语法基础

    本文为转载,原文:shell入门学习(1)——语法基础 介绍 Shell Script,Shell脚本与Windo...

  • Linux操作系统命令汇总

    SHELL脚本学习笔记 标签(空格分隔): linux shell脚本 1. 常用命令汇总 alias 设置别名u...

  • 2019-09-19

    Shell 概述 Shell 解析器 查看系统shell解析器 默认解析器为bash Shell 脚本入门 新建h...

  • 自动化脚本实践(Shell + Expect)

    Linux Shell脚本入门: Linux awk 命令 | 菜鸟教程 Shell 教程 | 菜鸟教程 lin...

  • Shell | 基础知识点实例整理(一)

    写一个简单的Shell脚本 代码如下: 练习使用Shell变量 代码如下: 练习使用Shell的命令行参数 代码如...

  • 嵌入式day12

    shell脚本的本质 shell脚本语言是解释型语言 shell脚本的本质:shell命令的有序集合 shell编...

  • Bash shell

    Shell脚本编程30分钟入门 1. $开头shell变量的含义: $1, $2, $3, ... are the...

网友评论

      本文标题:shell入门12-几个练习shell知识点的脚本汇总

      本文链接:https://www.haomeiwen.com/subject/wmqtcdtx.html