欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

LINUX 下的 shell 脚本——应用实例和项目

程序员文章站 2022-03-17 14:02:10
...

LINUX 下的 shell 脚本——应用实例和项目

1.实例一

用户建立脚本
执行 users_create.sh userlist passlist
建立 userlist 列表中的用户
设定 userlist 列表中的密码为 passlist 列表中的密码
当脚本后面跟的文件个数不足两时,报错
当文件行数不一致时报错
当文件不存在时报错
当用户存在时报错

10 #!/bin/bash
 11 ping -c 1 172.25.254.10 &> /dev/null
 12 [ $? -eq 0 ] && {
 13         echo "ping is ok"
 14 } ||{
 15         echo "ping is not ok"
 16         exit
 17 }
 18 [ `setenforce 0 &> /dev/null` -a `systemctl stop firewalld &> /dev/null`]
 19 [ $? -eq 0 ] && {
 20         echo "selinux and fierwall set success"
 21 }||{
 22         echo "selinux and firewalld set failed"
 23         exit
 24 }
 25 rpm -q nfs-utils &> /dev/null
 26 if [ $? -eq 0 ];then
 27         echo "nfs-server has installed"
 28 else
 29         echo "you should install nfs-server"
 30         dnf install nfs-utlis -y  &> /dev/null
 31         systemctl enable --now nfs-server &> /dev/null
 32         echo "nfs-server install success"
 33 fi
 34 read -p "please input a share dir:" dir
 35 if [ -e $dir ];then
 36         echo "share dir $dir exist"
 37 else
 38         mkdir $dir
 39         echo "share dir $dir create success"
 40 fi
 41 chmod 1777 $dir
 42 read -p "please input share ip range:" iprange
 43 read -p "please input permission(ro/rw):" permission
 44 read -p "input 1 -> clear config,defaults is add:" choice
 45 if [ $choice -eq 1 ];then
 46         > /etc/exports
 47 fi
 48 cat >> /etc/exports <<EOF
 49 $dir $iprange($permission)
 50 EOF
 51 systemctl restart  nfs-server | grep active &> /dev/null
 52 showmount -e 172.25.254.20 | grep $dir
 53 [ $? -eq 0 ] && echo "share success" || echo "share fail"
 54 echo "welcome for you next time"

运行结果:

LINUX 下的 shell 脚本——应用实例和项目

2.实例2

shell脚本完成nfs网络系统的搭建

LINUX 下的 shell 脚本——应用实例和项目

10 #!/bin/bash
 11 ping -c 1 172.25.254.10 &> /dev/null
 12 [ $? -eq 0 ] && {
 13         echo "ping is ok"
 14 } ||{
 15         echo "ping is not ok"
 16         exit
 17 }
 18 [ `setenforce 0 &> /dev/null` -a `systemctl stop firewalld &> /dev/null`]
 19 [ $? -eq 0 ] && {
 20         echo "selinux and fierwall set success"
 21 }||{
 22         echo "selinux and firewalld set failed"
 23         exit
 24 }
 25 rpm -q nfs-utils &> /dev/null
 26 if [ $? -eq 0 ];then
 27         echo "nfs-server has installed"
 28 else
 29         echo "you should install nfs-server"
 30         dnf install nfs-utlis -y  &> /dev/null
 31         systemctl enable --now nfs-server &> /dev/null
 32         echo "nfs-server install success"
 33 fi
 34 read -p "please input a share dir:" dir
 35 if [ -e $dir ];then
 36         echo "share dir $dir exist"
 37 else
 38         mkdir $dir
 39         echo "share dir $dir create success"
 40 fi
 41 chmod 1777 $dir
 42 read -p "please input share ip range:" iprange
 43 read -p "please input permission(ro/rw):" permission
 44 read -p "input 1 -> clear config,defaults is add:" choice
 45 if [ $choice -eq 1 ];then
 46         > /etc/exports
 47 fi
 48 cat >> /etc/exports <<EOF
 49 $dir $iprange($permission)
 50 EOF
 51 systemctl restart  nfs-server | grep active &> /dev/null
 52 showmount -e 172.25.254.20 | grep $dir
 53 [ $? -eq 0 ] && echo "share success" || echo "share fail"
 54 echo "welcome for you next time"

 

拓展用shell脚本搭建Samba服务器

 10 #!/bin/bash
 11  ping -c 1 172.25.254.20 &> /dev/null
 12   [ $? -eq 0 ] && {
 13           echo "ping is ok"
 14   } ||{
 15           echo "ping is not ok"
 16           exit
 17   }
 18 [ `setenforce 0 &> /dev/null` -a `systemctl stop firewalld &> /dev/null` ]
 19  [ $? -eq 0 ] && {
 20          echo "selinux and fierwall set success"
 21  }||{         echo "selinux and firewalld set failed"
 22         exit
 23  }
 24 rpm -q samba &> /dev/null
 25 if [ $? -eq 0 ];then
 26         echo "samba has installed"
 27 else
 28         echo "you should samba service"
 29         dnf install samba -y &> /dev/null
 30 fi
 31 rpm -q samba-common  &> /dev/null
 32 if [ $? -eq 0 ];then
 33         echo "samba-common has installed"
 34 else
 35         echo "you should samba-common"
 36         dnf install samba -y &> /dev/null
 37 fi
 38 rpm -q samba-client &> /dev/null
 39 if [ $? -eq 0 ];then
 40         echo "samba-client has installed"
 41 else
 42         echo "you should samba-client"
 43         dnf install samba -y &> /dev/null
 44 fi
 45 systemctl enable --now  smb &> /dev/null
 46 systemctl start smb &> /dev/null
 47 smbclient  -L //172.25.254.20
 48 echo "you can use samba"
 49 pdbedit -L &> /dev/null
 50 if [ $? = 0 ];then
 51         echo "smb user has: " 
 52         pdbedit -L | cut -d ":" -f 1
 53 else
 54         echo "you can add smb user:"
 55 fi
 56 smbclient  //172.25.254.20/`pdbedit -L | cut -d ":" -f 1` -U `pdbedit -L | cut -d ":" -f 1`

作业:

●判断/tmp/run目录是否存在,如果不存在就建立,如果存在就删除目录里所有文件

10 #!/bin/bash
 11 [ -e /tmp/run ]
 12 if [ $? -eq 0 ];then
 13         rm -fr /tmp/run/*
 14         echo "/tmp/run/ all file has remove"
 15 else
 16         mkdir /tmp/run
 17         echo "/tmp/run/ create success"
 18 
 19 fi


● 输入一个路径,判断路径是否存在,而且输出是文件还是目录,如果是链接文件,还得输出是 有
效的连接还是无效的连接

10 #!/bin/bash
 11 read -p "Please input a run:" run
 12 if [ -e $run ];then
 13         if [ -d $run ];then
 14                 echo "$run is dir"
 15         else
 16                 if [ -f $run -a -L $run ];then
 17                         echo "$run is valid link file"
 18                 else
 19                                 echo "$run is invalid link file"
 20                 fi
 21         fi
 22 fi


● 交互模式要求输入一个ip,然后脚本判断这个IP 对应的主机是否 能ping 通,输出结果类似于:
Server 10.1.1.20 is Down! 最后要求把结果邮件到本地管理员aaa@qq.com
aaa@qq.com

#!/bin/bash

read -p "Please input ip address:" ip

ping -c1 $ip &>/dev/null
if [ $? -eq 0 ];then
  echo "Server $ip is okey!" | mail -s "Server State" aaa@qq.com
else
  echo "Server $ip is down!" | mail -s "Server State" aaa@qq.com 
fi


● 写一个脚本/home/program,要求当给脚本输入参数hello时,脚本返回world,给脚本输入参数
world时,脚本返回hello。而脚本没有参数或者参数错误时,屏幕上输出
“usage:/home/program hello or world”

#!/bin/bash

if [ "$1" == "hello" ];then
  echo "world"
elif [ "$1" == "world" ];then
  echo "hello"
else
  echo "usage:/home/program hello or world"
fi


● 写一个脚本自动搭建nfs服务

#!/bin/bash

#name:auto_build_nfs_serve.sh
#path:/tmp/
#update: 2020-3-11

read -p "Please input Sever Address:" ip
#检测网络连接是否正常
ping -c1 $ip &>/dev/null && echo "Server connection is normal"

#第一步:关闭selinux和防火墙
#selinux是安全增强型 Linux(Security-Enhanced Linux)简称,是linux的一个安全子系统
#临时关闭Selinux: setenforce 0
#永久关闭:vim /etc/sysconfig/selinux
#SELINUX=enforcing 改为 SELINUX=disabled

setenforce 0 &>/dev/null  && echo "selinux has closed..."
systemctl stop firewall &>/dev/null && echo "Firewall has been closed ..."

#第二步:确认软件是否安装
#查询指定软件是否已安装:rpm -q <软件名> 或者 rpm -qa <软件名> 或者 rpm -qa | grep <软件名>
rpm -aq | grep rpcbind &>/dev/null
if [ $? -eq 0 ];then
    echo "rpcbind has been installed"
else 
    yum install rpcbind -y &>/dev/null && echo "Intalling the rpcbind..."
fi


#第三步:创建和发布共享目录
read -p "Please enter the directory you want to share:" dir
# 创建目录,-p 确保目录名称存在,不存在的就建一个。
mkdir $dir -p &>/dev/null
chmod 777 $dir
read -p "Please enter the network segment to be shared" NS
read -p "Please enter the permission to share, enter ro or rw:" PERMISSION
cat >> /etc/exports << end
$dir  $NS($PERMISSION)
end


#第四步:设置nfs服务开机自启动
systemctl restart rpcbind.service &>/dev/null && echo "rpcbind service has been started..."
systemctl restart nfs.service &>/dev/null && echo "nfs service started successfully"

 

相关标签: 运维开发