有趣的bash脚本

1、编写脚本/root/bin/createuser.sh,实现如下功能:使 用一个用户名做为参数,如果指定参数的用户存在,就显示 其存在,否则添加之;显示添加的用户的id号等信息

#!/bin/bash
read -p "Please input username: " n
if id $n &> /dev/null;then
    echo "The user is exited!"
else 
    useradd $n
    id $n
fi

2、编写脚本/root/bin/yesorno.sh,提示用户输入yes或no, 并判断用户输入的是yes还是no,或是其它信息 

     

#!/bin/bash
read -p "please input yes or no " yon
case $yon in
Y|y|yes|YES|yEs|yeS|YES|YEs|Yes)
    echo "your food is THE PEOPLE OF FUJIAN"
    ;;
N|n|no|NO)
    echo "your food is noodles"
    ;;
*)
    echo "Please input right pattern or GET OUT!"
    ;;
esac

3、编写脚本/root/bin/filetype.sh,判断用户输入文件路径 ,显示其文件类型(普通,目录,链接,其它文件类型) 

    

read -p "Please input the filepath: " f
if [ ! -e $f ];then
    echo "the file is not exited,please input the right filepath" && exit 
elif [ -f $f ];then
    echo "the file is regular file"
elif [ -d $f ];then
    echo "the file is directory file" 
elif [ -l $f ];then
    echo "the file is link file"  
else 
    echo "the file is other type"
fi

4、编写脚本/root/bin/checkint.sh,判断用户输入的参数是 否为正整数

#!/bin/bash
read -p "Please input the number: " n
if [[ "$n" =~ ^[0-9]+$ ]];then
    echo the input is a  postive integer 
else 
    echo the input is not a postive integer
fi

>###以下皆用for语句实现

5、判断/var/目录下所有文件的类型

     

#!/bin/bash
for type in  /var/* ;do
    if [ -h $type -o -L $type ];then
        echo "the $type is a link file"
    elif [ -f $type ];then 
       echo "the $type is a reguler file"
    elif [ -d $type ];then
       echo "the $type is a dir file"
    elif [ -b $type ];then
       echo "the $type is a block file"
    elif [ -c $type ];then
       echo "the $type is a character file"
    elif [ -p $type ];then
       echo "the $type is a pipe file"  
    else 
       echo "the $type is other file"
    fi
done
wait

6、添加10个用户user1-user10,密码为8位随机字符

#!/bin/bash
for uid in {1..10};do
    if id user$uid &> /dev/null;then 
        echo the user$uid is exited
    else
       useradd  user$uid 
       passwd=`openssl rand -base64 6`
       echo "user$uid:$passwd" >> /app/user.log
       echo $passwd | passwd --stdin user$uid > /dev/null && echo user$uid is created Successfully!!! 
    fi
done

7、/etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件 ;分别读取每个文件,以K开头的输出为文件加stop,以S开头的输 出为文件名加start,如K34filename stop  S66filename start

    

 #!/bin/bash
for c in /etc/rc.d/rc3.d/* ;do
    n=`echo $c | sed -r 's@(^/.*/)([^/].*/?)@\2@'`
    if [[ "$n" =~ "^K"]];then
        mv /etc/rc.d/rc3.d/"$n" /etc/rc.d/rc3.d/"$n"stop
    elif [[ "$n" =~ "^S"]];then
        mv /etc/rc.d/rc3.d/"$n" /etc/rc.d/rc3.d/"$n"start
    fi
done

8、编写脚本,提示输入正整数n的值,计算1+2+…+n的总和

     

#!/bin/bash
read -p "Please input the number: " n
    if [[ "$n" =~ ^[0-9]+$ ]] ; then
        sum=0
        for n in `seq $n`;do
            let sum=sum+n
        done
        echo the sumnumber is $sum
     else
        echo "Please input the right number!"
     fi

9、计算100以内所有能被3整除的整数之和

     

#!/bin/bash
sum=0
m=3
for n in `seq 100`;do
    let a=n%m
    if [ $a -eq 0 ];then 
        let sum=sum+n
    fi
done
echo $sum

10、编写脚本,提示请输入网络地址,如192.168.0.0,判断输入 的网段中主机在线状态 

     

#!/bin/bash
read -p "Please input IP: " ip
if [[ "$ip" =~ ([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3} ]];then
    a=`echo $ip | cut -d. -f1-3`
    for b in {0..254};do
        {
        if ping -c1 -W1 $a.$b &> /dev/null;then
            echo $a.$b is up!
        fi
        }&
    done
else
    echo please input the right IP!
fi
wait

11、打印九九乘法表

    #!/bin/bash
for a in {1..9};do
for b in seq $a;do
let c=ab
echo -n “$b
$a=$c
done
echo
done

12、在/testdir目录下创建10个html文件,文件名格式为数字N(从 1到10)加随机8个字母,如:1AbCdeFgH.html 

     

#!/bin/bash
if [ ! -d /testdir ];then
    mkdir /testdir/ &> /dev/null
fi
for n in {1..10};do
    for a in `cat /dev/urandom |tr -dc "a-zA-Z"|head -c 8`;do
        touch /testdir/$n$a.html
    done
    echo $n$a.html is already created!
done

13、打印等腰三角形

     

#!/bin/bash
read -p "请输出层数:" L
if [[ "$L" =~ ^[0-9]+$ ]];then
    for k in `seq $L`;do
        for a in `seq $[$L-$k]`;do
            echo -n " "
        done
        for b in `seq $[$k*2-1]`;do
            echo -en "\033[3$Yan;5m❄\033[0m"
        done
        echo
    done
else 
    echo Please input the number!
fi

14、打印国际象棋(4格,用while实现)

 

 #!/bin/bash
k=0
while [ $k -lt 4 ];do
    l=0
    while [ $l -lt 4 ];do
       echo -ne "\033[41m    \033[0m"
       echo -ne "\033[43m    \033[0m"
       let l++
    done
    echo
    l=0
    while [ $l -lt 4 ];do
       echo -ne "\033[41m    \033[0m"
       echo -ne "\033[43m    \033[0m"
       let l++                                                          
    done
    echo
    l=0
    while [ $l -lt 4 ];do
       echo -ne "\033[43m    \033[0m"
       echo -ne "\033[41m    \033[0m"
       let l++
    done
    echo
    l=0
    while [ $l -lt 4 ];do 
       echo -ne "\033[43m    \033[0m"
       echo -ne "\033[41m    \033[0m"
       let l++                                                          
   done
   echo
let k++
done

原创文章,作者:OscaoChaser,如若转载,请注明出处:http://www.178linux.com/85329

(1)
OscaoChaserOscaoChaser
上一篇 2017-08-24
下一篇 2017-08-26

相关推荐

  • 马哥教育网络班21期+第8周课程练习

    1、请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别 集线器(HUB)是在OSI模型的第一层——物理层——连接多台主机、延长网络的设备。其主要功能是将从一个端口接收到的数据包转发给所有端口。中继器(Repeater)是将由电缆传过来的电信号或光信号调整波形和放大再传给另一个电缆的设备,提供多端口服务的中继器称作集线器。二者的主要区别是…

    Linux干货 2016-08-29
  • 2016全球运维大会,优云蒋君伟演讲“CMDB+自动化的管理融合”成一大亮点

    2016全球运维大会于9月23日-24日在上海盛大开幕。作为国内运维行业的重量级大会,优云产品总监蒋君伟在自动化专场与来自全国各地的运维同行一起探讨、分享业内自动化运维的最佳实践。现场情绪热烈,气氛高涨,成为了本届全球运维大会的一大亮点。 全新梳理自动化与CMDB的融合之道 全球运维大会当天,运维自动化专场很多大牛针对自动化运维管理中的CMDB进行了激烈的讨…

    Linux资讯 2016-12-05
  • Linux程序包管理方式

    Linux程序包安装和管理方式共计三种:          一、[yum|dnf],通过官网或者其他开源网站提供的文件服务器,本机镜像源等途径进行安装。         二、rpm,通过官网或者其他开源网站通过…

    Linux干货 2016-07-29
  • Linux基础之加密通讯过程详解

    加密通讯过程详解 第一阶段 客户端->服务器端 向服务器声明自己的加密通讯协议版本,ssl或者tls 支持的加密算法 支持的压缩算法 第二阶段 服务器端->客户端 向客户端确认使用的加密通讯协议版本 确认的加密方法 确认压缩方法 服务器端证书 第三阶段 客户端->服务器端 客户端验证服务器端证书 发证机构 证书完整性 证书持有者 证书有效期…

    2017-09-16
  • linux之/home目录转移分区。

    linux之/home目录转移分区。     I,基本思路,将/home目录的数据转移到新的分区,再将/home目录挂载到新的分区。     II,添加硬盘,进行分区,添加新硬盘不重启机器识别命令echo “- – -”  /sys/class/scsi_host/host#/scan,然后进行 …

    Linux干货 2017-06-19
  • 虚拟机配置网卡别名及centos 6 之bonding

    1、虚拟网卡实现一个网卡多个地址(测试环境为虚拟机),此处使用eth1网卡,并且将eth1的IP设置成固定的,其实还可以设置成DHCP自动获取,这就是Linux强大之处。但是由eth1虚拟出来的两张网卡不能使用DHCP自动获取。具体步骤如下(设置的IP看个人喜欢) [root@localhost network-scripts]# cat…

    Linux干货 2016-09-05