浅学一下Shell脚本(7)--一些案例

1.脚本自动搭建nfs服务

#!/bin/bash

echo '1,checking network... '
ping -c1 192.168.1.10 &> /dev/null
if [ $? -eq 0 ];then
	echo 'network is ok!'
else 
	echo 'network is not ok! '
	exit 1
fi

echo '2.Configure selinux and firewalld... '
setenforce 0 &> /dev/null
echo 'Selinux set disable'
systemctl stop firewalled &> /dev/null
echo 'firewalld stopped'


echo '3.Check rpcbind...'
rpm -q rpcbind &> /dev/null
if [ $? -eq 0 ];then
	echo 'rpcbind has installed!'
else
	yum install -y rpcbind &> /dev/null && echo 'rpcbind install successfully' || echo 'rpcbind install failed!'; exit 1
fi

echo '4.Creat Share Dir and Share...'
read -p "Please input share dir:" dir
if [ -e $dir ];then
	echo "Share Directory $dir exists!"
else
	mkdir -p $dir
	echo "Share Directory $dir creat success!"
fi
chmod 1777 $dir
read -p "Please input share subnet:" sub
read -p "Please input share permission(ro/rw): " permission
echo 'Edit nfs configure file /etc/exports'
read -p "input 1 for clear config , default is add: " choice
if [ $choice -eq 1 ];then
	> /etc/exports
fi
cat >> /etc/exports <<EOF
$dir $sub($permission)
EOF

echo "5.Start and enable service..."
systemctl status nfs-server.service | grep active &>/dev/null
if [ $? -eq 0 ];then
	echo 'nfs-server.servise is active , restarting now ...'
	systemctl restart nfs-server.service
	echo 'nfs-server.service is restart successfully!'
else
	systemctl enable --now rpcbind 
	systemctl encble --now nfs-server.service
fi
echo 'NFS builds completed!'

2.脚本统计web服务不同连接状态的个数

#!/bin/bash
#统计每个状态的个数
declare -A array1
states=`ss -ant|grep 80|cut -d' ' -f1`
for i in $states
do
        let array1[$i]++
done
#通过遍历数组里的索引和元素打印出来
for j in ${!array1[@]}
do
        echo $j:${array1[$j]}
done

3.创建5个用户,用户密码为随机数

#!/bin/bash
#产生一个保存用户名和密码的文件
echo user{1..3}:wang$[$RANDOM%9000+1000]|tr ' ' '\n'|tr ':' ' ' >> user_pass.file
#循环创建5个用户
while read user pass
do
    useradd $user
    echo $pass|passwd --stdin $user
done < user_pass.file

4.脚本搭建apache服务

1、用户输入web服务器的IP、域名以及数据根目录
2、如果用户不输入则一直提示输入,直到输入为止
3、当访问www.test.cc时可以访问到数据根目录里的首页文件“this is test page”

#!/bin/bash
conf=/etc/httpd/conf/httpd.conf
input_fun()
{
  input_var=""
  output_var=$1
  while [ -z $input_var ]
	do
	read -p "$output_var" input_var
	done
	echo $input_var
}
ipaddr=$(input_fun "Input Host ip[192.168.0.1]:")
web_host_name=$(input_fun "Input VirtualHostName [www.test.cc]:")
root_dir=$(input_fun "Input host Documentroot dir:[/var/www/html]:")

[ ! -d $root_dir ] && mkdir -p $root_dir
chown apache.apache $root_dir && chmod 755 $root_dir
echo this is $web_host_name > $root_dir/index.html
echo "$ipaddr $web_host_name" >> /etc/hosts

[ -f $conf ] && cat >> $conf <<end
NameVirtualHost $ipaddr:80
<VirtualHost $ipaddr:80>
	ServerAdmin webmaster@$web_host_name
	DocumentRoot $root_dir
	ServerName $web_host_name
	ErrorLog logs/$web_host_name-error_log
	CustomLog logs/$web_host_name-access_loh common
</VirtualHost>
end

5.关闭SELinux

#!/bin/bash
 

sed -i  '/^SELINUX/s/=.*/=disabled/' /etc/selinux/config
setenforce 0

6.脚本实现进度条

#!/bin/bash
i=0
str='#'
ch=('|' '\' '-' '/')
index=0
while [ $i -le 25 ]
do
    printf "[%-25s][%d%%][%c]\r" $str $(($i*4)) ${ch[$index]}
    str+='#'
    let i++
    let index=i%4
    sleep 0.1
done
printf "\n"
echo "安装完成"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值