有趣的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.www58058.com/85329

(1)
OscaoChaserOscaoChaser
上一篇 2017-08-24 18:58
下一篇 2017-08-26 09:23

相關推薦

  • 推薦-?以各種方式實現yum源,簡單暴力,絕對實操干貨!

    以各種方式實現yum源,簡單暴力,絕對實操干貨! 科普:yum不是程序包安裝工具,而是rpm包前端管理工具,通過yum可以更好的管理rpm的安裝卸載 以各種方式實現yum源,簡單暴力,絕對實操干貨! 一、本地yum源之“掛載光盤鏡像實現”! 二、本地yum源之“掛載本地磁盤上的鏡像文件”! 三、如能上網,實現自定義指定鏡像網站yum源 四、炸天重磅來襲?。?!…

    Linux干貨 2016-04-11
  • N21-天天-第九周課程練習

    1、寫一個腳本,判斷當前系統上所有用戶的shell是否為可登錄shell(即用戶的shell不是/sbin/nologin);分別這兩類用戶的個數;通過字符串比較來實現; #!/bin/bash awk -F: '$NF~"/sbin/nologin" {shell++} $NF!~&qu…

    Linux干貨 2016-09-01
  • 自己做一個CA

    構建CA服務器    CA配置文件位置:        /etc/pki/tls/openssl.cfg           &n…

    Linux干貨 2017-04-11
  • N22-第6周作業-冥界之王

    請詳細總結vim編輯器的使用并完成以下練習題1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#;    [root@CentOS6 /]# cp /etc/rc.d/rc.sysinit /tmp…

    Linux干貨 2016-09-19
  • Lvm的創建

    一、LVM相關基礎: ????PE:類似與磁盤的block,這個的大小也會影響VG的大小 ????PV:是磁盤分區或邏輯上與磁盤分區具有相同功能的設備(RAID),是LVM的基本存儲模塊,但與基本的物理存儲模塊相比,卻包含有lvm相關的參數 ????VG:類似于非lvm系統中的物理磁盤,包含多個pv ????LV:類似于非lvm系統中的磁盤分區 ? PV相關…

    2016-04-10
  • Linux網絡相關概念及bash腳本編程練習

    馬哥教育網絡班第23期+第八周課堂練習 Linux網絡相關概念及bash腳本編程練習 習題: 1.請描述網橋、集線器、二層交換機、三層交換機、路由器的功能、使用場景與區別 (1)網橋: 橋接器(英語:network bridge),又稱網橋,一種網路裝置,負責網路橋接(network bridging)之用。 橋接器將網絡的多個網段在數據鏈路層(O…

    Linux干貨 2016-11-15
欧美性久久久久