1. <var id="fe6gj"></var>

    <rp id="fe6gj"><nav id="fe6gj"></nav></rp>

    <noframes id="fe6gj"><cite id="fe6gj"></cite>

    <ins id="fe6gj"><button id="fe6gj"><p id="fe6gj"></p></button></ins>
    1. <tt id="fe6gj"><i id="fe6gj"><sub id="fe6gj"></sub></i></tt>
        始創于2000年 股票代碼:831685
        咨詢熱線:0371-60135900 注冊有禮 登錄
        • 掛牌上市企業
        • 60秒人工響應
        • 99.99%連通率
        • 7*24h人工
        • 故障100倍補償
        您的位置: 網站首頁 > 幫助中心>文章內容

        bash編程入門

        發布時間:  2012/9/15 18:03:46
        1. ping 主機
        #!/bin/bash
        HOSTNAME=$1 #注意位置參數的使用 if ping -c2 $HOSTNAME;then
        echo "The $HOSTNAME is online."
        else
        echo "The $HOSTNAME is down."
        fi
        ==================================================================
        2.找文件—》找到就備份—》沒找到就顯示其他信息
        #!/bin/bash
        DIR=$1
        BACKUP=$2
        #destination of the backup.
        if [ ! -d $2 ] ;then
        echo "The directory does not exist!"
        elif [ $# -eq 2 ];then
        echo "$DIR is backuped to $BACKUP... "
        else
        echo -e "Usage: $0 dir backupfile\n for example:$0 /etc/passwd /tmp/bash"
        fi
        ==================================================================
        3.找用戶—>并統計行數—>
        #!/bin/bash
        USERNAME=$1
        RES=$(cat /etc/passwd | grep $USERNAME | wc -l)
        if [ "$RES" == "1" ] ;then
        echo "The user you want to find is $USERNAME."
        elif [ $RES -eq 0 ];then
        echo "The user you want to find doesnot exist."
        else
        echo "Some other errors happen"
        fi
        ==================================================================
        4.查看文件是否存在并且可寫
        #!/bin/bash
        if [ -f $1 -a -w $1 ];then
        echo "The file is writable."
        else
        echo "This a directory."
        fi
        ==================================================================
        5.簡單的if-else嵌套
        #!/bin/bash
        HOST=$1
        if ping -c2 $HOST &> /dev/null ;then
        echo "$HOST is online. "
        else
        echo "$HOST is down."
        fi
        ==================================================================
        6.第5個程序可改寫如下:
        #!/bin/bash
        HOST=$1
        ping -c2 $HOST &> /dev/null && echo "$HOST is online " || echo "$HOST is down"
        ==================================================================
        7.查看文件夾是否可寫可執行
        #!/bin/bash
        DIR=$1
        [ -r $DIR ] && echo "The file is readable" || echo "The directory is not readable"
        [ -w $DIR ] && echo "The file is writable" || echo "The file is not writable"
        [ -x $DIR ] && echo "The file is excutable" || echo "The file is not excutable"
        ==================================================================
        8.簡單的菜單選項設置
        #!/bin/bash
        echo "======================="
        echo -e "\t\t1.Linux"
        echo -e "\t\t2.Unix"
        echo -e "\t\t3.Windows"
        echo "======================="
        read NUMBER
        if [ $NUMBER -eq 1 ];then
        echo "You have chosen RedHad Linux 6.0"
        elif [ $NUMBER -eq 2 ];then
        echo "You have chosen IBM AIX"
        elif [ $NUMBER -eq 3 ];then
        echo "You have chosen Windows!"
        else
        echo "You have chosen a wrong number!"
        fi
        ==================================================================
        9.簡單退出值的設定
        #!/bin/bash
        FILENAME=$1
        if [ -d $FILENAME ];then
        echo "this is a directory!"
        exit 10
        elif [ ! -w $FILENAME ];then
        exit 20
        else
        exit 1
        fi
        ==================================================================
        10.case的用法(模擬服務的啟動)
        把寫好的腳本加入到 /etc/rc.d/init.d并注意加上著色幾行,并用命令
        Chkconfig —add SERVICENAME
        #!/bin/bash
        # chkconfig: 235 90 12
        # description:
        if [ $# -lt 1 ];then
        echo "Usages: services $1 {start|stop|restart|status}"
        else
        case "$2" in
        start)
        echo "The service $1 is starting"
        ;;
        stop)
        echo "The service $1 is stopping"
        ;;
        restart)
        echo -e "The servie $1 is stopping\nThe service is starting"
        ;;
        status)
        echo "The service $1's status is..."
        ;;
        esac
        fi
        ==================================================================
        11.用source和bash調用程序的時候,返回值不同。
        ○ 1
        #!/bin/bash
        bash ./b.sh
        RES=$?
        echo "The $0 pid is $$"
        case "$RES" in
        10)
        echo "Network problem"
        ;;
        20)
        echo "Directory does not exist"
        ;;
        esac
        ○ 2
        #!/bin/bash
        echo " The $0 pid is $$"
        exit 10
        ==================================================================
        12.for循環中的變量字符集
        #!/bin/bash
        for HOST in 172.24.254.254 172.24.254.200 172.24.123.123
        do
        ping -c2 $HOST &> /dev/null && echo "$HOST is online" \
        || echo "$HOST is down"
        done
        ==================================================================
        13.for循環變量字符集
        #!/bin/bash
        for HOST in 172.24.0.{1..10}
        do
        ping -c2 $HOST &> /dev/null && echo "$HOST is online" \
        || echo "$HOST is down"
        Done
        ==================================================================
        14.for循環變量文件集
        #!/bin/bash
        for FILE in /etc/*.conf
        do
        cp $FILE /tmp/tmp
        done
        #!/bin/bash
        DEST=/var/tmp
        for FILENAME in /lib64/*.* /etc/*.conf
        do
        cp $FILENAME $DEST &> /dev/null
        logger "backup $FILENAME now..."
        echo "Backup $FILENAME TO $DEST" >> ~/backup.log
        done
        ==================================================================
        15.for循環指令集
        #!/bin/bash
        for USERINFO in $(cat /etc/passwd | grep bash$ | cut -d: \
        -f1,3)
        do
        _USENAME=$(echo $USERINFO | cut -d: -f 1)
        _UID=$(echo $USERINFO | cut -d: -f 2)
        if [ $_UID -lt 500 -a $_UID -ne 0 ];then
        logger "$_USERNAME is a system user ,but can \
        log now"
        fi
        done
        ==================================================================
        16.輸出9*9乘法表
        #!/bin/bash
        for NUM1 in {1..9}
        do
        for NUM2 in {1..9}
        do
        RES=$((NUM1 * NUM2))
        echo -en "$NUM1 * $NUM2 = $RES\t"
        done
        echo
        done
        ==================================================================
        17.輸出9*9乘法表
        #!/bin/bash
        for ((i=1;i<10;i++))
        do
        for ((p=1;p<=i;p++))
        do
        RES=$(($p * $i))
        echo -en "$p * $i = $RES\t"
        done
        echo
        done
        echo
        ==================================================================
        18.輸出黑白象棋
        #!/bin/bash
        for HANG in {1..9}
        do
        for LIE in {1..9}
        do
        RES=$((HANG + LIE))
        if [ $(($RES % 2)) -eq 0 ];then
        echo -en "\033[47m \033[0m"
        else
        echo -en "\033[40m \033[0m"
        fi
        done
        echo
        done
        ==================================================================
        19.輸出黑白象棋
        #!/bin/bash
        for HANG in {1..9}
        do
        for LIE in {1..9}
        do
        RES=$((HANG + LIE))
        if ((($RES % 2) == 0)) ;then
        echo -en "\033[47m \033[0m"
        else
        echo -en "\033[40m \033[0m"
        fi
        done
        echo
        done
        ==================================================================
        20.輸出數字形狀
        #!/bin/bash
        echo "Please type your number:"
        read a
        for ((i=1;i<=a;i++))
        do
        for ((p=1;p<=i;p++))
        do
        echo -n "$p"
        done
        echo
        done
        echo
        ==================================================================
        21.計算器(加減乘除)
        echo "..............x"
        echo "............../"
        echo "..............q"
        echo "Please type your word:(e.g.1 + 2)"
        read a b c
        do
        case $b in
        +) echo " $a + $c =" `expr $a + $c`;;
        -) echo " $a - $c =" `expr $a - $c`;;
        x) echo " $a x $c =" `expr $a \* $c`;;
        /) echo " $a / $c =" `expr $a \/ $c`;;
        esac
        case $a in
        q) break ;;
        esac
        done
        ==================================================================
        22.輸出直角三角形
        #!/bin/bash
        echo "Please type a number:"
        read num
        for ((i=1;i<num;i++))
        do
        for ((j=0;j<num-i;j++))
        do
        echo -n ""
        done
        for ((j=0;j<2*i-1;j++))
        do
        echo -n "*"
        done
        ==================================================================
        23.輸出翻轉三角形
        #!/bin/bash
        echo "Please type a number:"
        read num
        for ((i=1;i<num;i++))
        do
        for ((j=0;j<num-i;j++))
        do
        echo -n ""
        done
        for ((j=0;j<2*i-1;j++))
        do
        echo -n "*"
        done
        echo ""
        done
        for ((i=1;i<=num;i++))
        do
        for ((j=0;j<i;j++))
        do
        echo -n ""
        done
        for ((j=0;j<2*(num-i)-1;j++))
        do
        echo -n "*"
        done
        echo ""
        done
        ==================================================================
        24.菜單的設定
        !/bin/bash
        while [[ "$CHOOSE" != "q" && "$CHOOSE" != "Q" ]]
        do
        clear
        echo "1.Linux"
        echo "2.Unix"
        echo "q.quit"
        read CHOOSE
        case $CHOOSE in
        1)
        echo "You have chosen Linux"
        exit 10
        ;;
        2)
        echo "You have chosen Uinx"
        exit 20
        ;;
        esac
        done
        ==================================================================
        25.簡單函數,函數可接受位置參數,此函數名字為24.sh
        #!/bin/bash
        clean_tmp_file()
        {
        echo "clean $1..."
        }
        #$1 is a dabase name and $2 is a folder
        backup_database()
        {
        echo "backup database $1 to $2"
        }
        check_file_md5()
        {
        echo "check $1 sum md5 value"
        }
        clean_tmp_file /tmp/empty
        ==================================================================
        26.調用函數庫中的函數
        #!/bin/bash
        source ./24.sh
        clean_tmp_file /etc/passwd
        check_file_md5 /etc/group
        backup_database /mys
        ==================================================================
        27.路徑的設置
        #!/bin/bash
        setPath ()
        {
        PATH=/bin:/usr/bin
        if [ $UID -eq 0 ];then #如果是root用戶
        PATH=$PATH:/usr/bin/sbin:/sbin #路徑追加sbin的目錄
        fi
        if (($2 == "after"));then #根據位置參數2,決定如何添加新路徑
        PATH=$PATH:$1 #在原PATH后面追加
        else
        PATH=$1:$PATH #在原PATH之前添加
        fi
        }

        本文出自:億恩科技【www.endtimedelusion.com】

        服務器租用/服務器托管中國五強!虛擬主機域名注冊頂級提供商!15年品質保障!--億恩科技[ENKJ.COM]

      1. 您可能在找
      2. 億恩北京公司:
      3. 經營性ICP/ISP證:京B2-20150015
      4. 億恩鄭州公司:
      5. 經營性ICP/ISP/IDC證:豫B1.B2-20060070
      6. 億恩南昌公司:
      7. 經營性ICP/ISP證:贛B2-20080012
      8. 服務器/云主機 24小時售后服務電話:0371-60135900
      9. 虛擬主機/智能建站 24小時售后服務電話:0371-60135900
      10. 專注服務器托管17年
        掃掃關注-微信公眾號
        0371-60135900
        Copyright© 1999-2019 ENKJ All Rights Reserved 億恩科技 版權所有  地址:鄭州市高新區翠竹街1號總部企業基地億恩大廈  法律顧問:河南亞太人律師事務所郝建鋒、杜慧月律師   京公網安備41019702002023號
          0
         
         
         
         

        0371-60135900
        7*24小時客服服務熱線

         
         
        av不卡不卡在线观看_最近2018年中文字幕_亚洲欧美一区二区三区_一级A爱做片免费观看国产_日韩在线中文天天更新_伊人中文无码在线