shell脚本编写、一键安装nginx、条件语句、 检测网段脚本、 打印九九乘法表、

1.shell脚本

1.编写及运行脚本

[root@13git ~]# vim hello.sh

[root@13git ~]# bash hello.sh 
[root@13git ~]# sh hello.sh 
[root@13git ~]# source hello.sh         //在当前进程执行
[root@13git ~]# chmod +x hello.sh 
[root@13git ~]# ./hello.sh 

2.一键安装nginx

[root@13git ~]# vim nginx.sh

#!/bin/bash

yum -y install gcc gcc-c++ make pcre-devel openssl-devel wget
cd /usr/local/src
wget "https://nginx.org/download/nginx-1.27.0.tar.gz"
tar xf nginx-1.27.0.tar.gz
cd nginx-1.27.0
./configure --prefix=/usr/local/nginx
make -j 4
make install

 [root@13git ~]# source nginx.sh        //运行脚本

3.测试网络脚本:

[root@13git ~]# vim ping.sh
#!/bin/bash
read -p "请输入要测试的网址:" web
ping -c 3 $web &> /dev/null
if [ $? -eq 0 ];then
    echo "此网络通!"
else
    echo "无法访问"
fi
[root@13git ~]# bash ping.sh 
请输入要测试的网址:www.baidu.com
此网络通!

4.条件语句:

[root@13git ~]# vim touch.sh

#!/bin/bash

echo "1新增文件 2删除文件 3查找文件 4修改文件"
read -p "请输入序号选择功能:" m
if [ $m == 1 ];then
        touch aaaa.txt
elif [ $m == 2 ];then
        rm -rf aaaa.txt
else
        echo "其他功能正在开发中"
fi

运行文件,查看内容: 

[root@13git ~]# bash touch.sh 
1新增文件 2删除文件 3查找文件 4修改文件
请输入序号选择功能:4
其他功能正在开发中
[root@13git ~]# bash touch.sh 
1新增文件 2删除文件 3查找文件 4修改文件
请输入序号选择功能:1
[root@13git ~]# ls
aaaa.txt                           mongodb-linux-x86_64-rhel70-3.6.3.tgz
[root@13git ~]# bash touch.sh 
1新增文件 2删除文件 3查找文件 4修改文件
请输入序号选择功能:2
[root@13git ~]# ls
anaconda-ks.cfg                        nginx.sh

 5.创建用户和密码脚本:

[root@13git ~]# vim zhanghao.sh
#!/bin/bash
read -p "username:" u
read -p "password:" p
useradd $u
echo $p | passwd --stdin $u
if [ $? -eq 0 ];then
    echo "账户$u注册成功"
fi

[root@13git ~]# bash zhanghao.sh 
username:pp
password:1
更改用户 pp 的密码 。
passwd:所有的身份验证令牌已经成功更新。
账户pp注册成功

 6.遍历脚本

[root@13git ~]# vim bianli.sh 

#!/bin/bash

for x in "$*"
do
        echo $x
done

echo $?
echo $#
echo $0

[root@13git ~]# bash bianli.sh 1 2 3 d s 
1 2 3 d s
0
5
bianli.sh
[root@13git ~]# 

 7.脚本实例:

[root@13git ~]# vim city.sh

#!/bin/bash
for city in 青岛 青海 陕西 北京
do      
        echo "$city是个好地方!"
done

[root@13git ~]# bash city.sh 
青岛是个好地方!
青海是个好地方!
陕西是个好地方!
北京是个好地方!
[root@13git ~]# 

 8.检测网段脚本:

[root@13git ~]# vim pingwangduan.sh

#!/bin/bash
for IP in $(echo 192.168.2.{10..20})
do
        ping -c 2 -i 0.1 $IP &> /dev/null
        if [ $? -eq 0 ];then
        echo "Host $IP is up."
        fi
done

[root@13git ~]# bash pingwangduan.sh 
Host 192.168.2.13 is up.
Host 192.168.2.16 is up.
[root@13git ~]# 

 9.打印九九乘法表:

[root@13git ~]# vim jiujiu.sh

[root@13git ~]# cat jiujiu.sh
#!/bin/bash
for i in `seq 9`
do
    for j in `seq $i`
    do
        echo -n  $i*$j=$[i*j]
    done
    echo  ""
done

[root@13git ~]# bash jiujiu.sh 
1*1=1 
2*1=22*2=4 
3*1=33*2=63*3=9 
4*1=44*2=84*3=124*4=16 
5*1=55*2=105*3=155*4=205*5=25 
6*1=66*2=126*3=186*4=246*5=306*6=36 
7*1=77*2=147*3=217*4=287*5=357*6=427*7=49 
8*1=88*2=168*3=248*4=328*5=408*6=488*7=568*8=64 
9*1=99*2=189*3=279*4=369*5=459*6=549*7=639*8=729*9=81 
[root@13git ~]# 

10. 打印菱形

[root@13git ~]# vim lingxing.sh 
#!/bin/bash
for (( i = 1; i < 12; i++))
do
   if [[ $i -le 6 ]]
   then
    for ((j = $((12-i)); j > i; j--))
    do
        echo -n " "
    done
    for ((m = 1; m <= $((2*i-1)); m++))
    do
        echo -n "* "
    done
        echo ""
#****************************************************************************
   elif [[ $i -gt 6 ]]
   then
    n=$((12-i))
    for ((j = $((12-n)); j > n; j--))
    do
        echo -n " "
    done
    for ((m = 1; m <= $((2*n-1)); m++))
    do
        echo -n "* "
    done
        echo ""
   fi
done


11.

[root@13git ~]# vim selinux.sh

#!/bin/bash
#备份

read -p "请输入指定IP地址:" ip

#替换dhcp为none
sed -i '/dhcp/s/dhcp/none/g' /etc/sysconfig/network-scripts/ifcfg-ens33
#在文档最后添加5行


#IPADDR
sed -i '$aIPADDR='"$ip"'' /etc/sysconfig/network-scripts/ifcfg-ens33

#NETMAST
sed -i '$aNETMASK=255.255.255.0' /etc/sysconfig/network-scripts/ifcfg-ens33

#GATEWAY
sed -i '$aGATEWAY=192.168.2.1' /etc/sysconfig/network-scripts/ifcfg-ens33

#DNS1
sed -i '$aDNS1=8.8.8.8' /etc/sysconfig/network-scripts/ifcfg-ens33

#DNS2
sed -i '$aDNS2=114.114.114.114' /etc/sysconfig/network-scripts/ifcfg-ens33

#修改uuidsed -i '/UUID/cUUID='"$(uuidgen)"'' /etc/sysconfig/network-scripts/ifcfg-ens33

#修改主机名
read -p "请输入主机名称" hn
hostnamectl set-hostname $hn

#关闭selinux
 setenforce 0
sed -i '/SELINUX=/cSELINUX=disable' /etc/selinux/config

#关闭防火墙
 systemctl stop firewalld
 systemctl disable firewalld

#关闭Network
# systemctl stop NetworkManager
# systemctl disable NetworkManager

[root@13git ~]# bash selinux.sh 
请输入指定IP地址:192.168.2.13
请输入主机名称mm
setenforce: SELinux is disabled

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值