有趣的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)
上一篇 2017-08-24 18:58
下一篇 2017-08-26 09:23

相关推荐

  • centos系统自动化安装

    本章内容 系统安装过程配置anaconda自动化安装系统 安装程序 CentOS系统安装 系统启动流程: bootloader–>kernel(initramfs)–>rootfs–>/sbin/init anaconda: 系统安装程序 tui: 基于图形库curses的文本窗口 gui:图形窗口 安装程序启动过程 MBR…

    Linux干货 2016-09-19
  • 磁盘管理之swap,移动设备及dd命令的使用

    首先我们来复习一下前一天的内容 CHS Sector(扇区) 512字节 track(磁道) 63个 2^6个扇区 1024个磁道,10个位存储磁道数 track=cylinder(柱面) cylinder=(容量)sector(512)track(63)head(256)=8M head(磁头) 256 8位存储 MBR的分区方式都是基于柱面为整…

    Linux干货 2016-09-07
  • 何为正则表达式?

    何为正则表达式?   UNIX/Linux上有许多文本处理工具,其中最主要最重要要属grep、sed、和awk三种了,被称为文本处理三剑客。但是要完全认识他们的各种功能,则必须现在正则表达式及其元字符的使用上打好基础。 什么是正则表达式呢?正则表达式(regular expression,RE)是一种字符模式,用于在查找过程中匹配指定的字符。正则表…

    Linux干货 2016-08-16
  • Linux软件包安装

    Linux系统和Windows系统在软件包安装上区别很大:     软件运行环境:         API: Application Programming Interface    POSIX: P…

    Linux干货 2016-08-21
  • Linux运维之进程管理

    一、      进程概念 进程是内核的一个功能,在Linux中,运行一个程序或命令可以出发一个事件而驱动一个PID,在linux系统中,系统只识别二进制程序文件,我们可以通过执行系统上的二进制程序来运行程序,进而产生进程。在linux系统中第一个进程是init程序,它是系统开机第一个加载的程序,用来支撑系统的…

    Linux干货 2016-09-13
  • 揭开链接文件的面纱——从根本上剖析硬链接与软链接异同

    在linux的学习过程中,链接文件的学习让不少人头疼,很多同学往往分不清什么是硬链接,什么是软链接,对于两者的概念和区别常常容易混淆、搞不清楚。今天我们就从原理、从根本上为大家辨析一些两者之间的区别,相信大家在看完这篇文章之后对链接文件会有一个清晰正确的认识。 1、在实现原理上不同 硬链接,涉及到文件的底层模式,因此被称为硬链接文件。硬链接文件只是一个指针指…

    Linux干货 2016-10-20