1. 程式人生 > 其它 >shell指令碼程式設計基礎(練習)

shell指令碼程式設計基礎(練習)

技術標籤:Q&A、筆記、練習linux

  • shell程式設計指令碼
    1、編寫指令碼systeminfo.sh,顯示當前主機系統資訊,包括主機名,IPv4地址,作業系統版本,核心版本,CPU型號,記憶體大小,硬碟大小
    #!/bin/bash
    COLOR="\033[32m"
    COLOREND="\033[0m"
    echo -e "The hostname is $COLOR `hostname` $COLOREND"
    echo -e "IPv4 address is $COLOR `ifconfig ens33|egrep
    -o '[0-9.]{7,15}'|head -1`
    $COLOREND"
    echo -e "OS Version is $COLOR `cat /etc/redhat-release` $COLOREND" echo -e "Kernel Version is $COLOR `uname -r` $COLOREND" echo -e "CPU type is $COLOR `lscpu|grep 'Model name'|cut -d: -f2|tr -s ' '` $COLOREND" echo -e "Disk space is $COLOR
    `lsblk|grep disk|head -1|tr -s ' '|cut -d' ' -f4` $COLOREND"
    echo -e "Memory size is $COLOR `free -h|grep Mem|tr -s ' '|cut -d' ' -f2` $COLOREND" unset COLOR COLOREND
    在這裡插入圖片描述
    2、編寫指令碼backu.sh,可實現每日將/etc/目錄備份到/backup/etcYYYY-mm-dd中
    #!/bin/bash
    mkdir backup
    echo "start backup!"
    sleep 2
    cp -av /etc /backup/etc`
    date +%F`
    echo "finished!"
    3、編寫指令碼disk.sh,顯示當前硬碟分割槽中空間利用率最大的值
    #!/bin/bash
    COLOR="\033[$[RANDOM%7+31]m"
    COLOREND="\033[0m"
    echo "The maximum utilization value in the disk space is :"
    echo -e "$COLOR `df -h|tr -s ' '|cut -d ' ' -f5|sort -nr|head -1` $COLOREND"
    
    在這裡插入圖片描述
    4、編寫指令碼links.sh,顯示正連線主機的每個遠端主機的IPv4地址和連線數,並按連線數從大到小排序
    #!/bin/bash
    COLOR="\e[1;35m"
    COLOREND="\e[0m"
    echo -e "$COLOR `netstat -tan|grep 'ESTAB'|tr -s ' '| cut -d' ' -f6|sort -nr` $COLOR"
    


  • 算術邏輯運算和條件測試
    1、編寫指令碼sumid.sh,計算/etc/passwd檔案中的第10個使用者和第20個使用者的UID之和
    #!/bin/bash
    Uid10=`cat /etc/passwd|head -10|tail -1|cut -d: -f3`
    Uid20=`cat /etc/passwd|head -20|tail -1|cut -d: -f3`
    sum=$[Uid10+Uid20]
    echo $sum
    
    2、編寫指令碼sumspace.sh,傳遞兩個檔案路徑作為引數給指令碼,計算這兩個檔案中所有空白行之和
    #!/bin/bash
    read -p "input the first fileName:" f1
    read -p "input the sencond fileName:" f2
     
    sum1=`cat $f1 | grep "^$" | wc -l`
    sum2=`cat $f2 | grep "^$" | wc -l`
                                                           
    let sum=$[sum1+sum2]
    echo FirstFileName is $f1
    echo SencondFileName is $f2
    echo FileNum is $sum
    
    3、編寫指令碼sumfile.sh,統計/etc,/var,/usr目錄中總共多少個一級子目錄和檔案
    #!/bin/bash
    read -p "input the first direction  " d1
    read -p "input the two direction  " d2
    read -p "input the direction  " d3
     
    num1=`ls -l $d1 | egrep "(^d.*)|(^-.*)" | wc -l`
    num2=`ls -l $d2 | egrep "(^d.*)|(^-.*)" | wc -l`
    num3=`ls -l $d3 | egrep "(^d.*)|(^-.*)" | wc -l`
    
    let sum=$[num1+num2+num3]
    echo $sum
    
    4、若硬碟空間利用率超過80%則列印告警資訊,反之列印正常資訊
    #!/bin/bash
    NORMAL=80
    COLOR="\033[31m"
    COLOREND="\033[0m"
    WARN=`df|egrep -o ^/dev/sd[a-Z].*[0-9]+%|tr -s " " %|cut -d% -f5|sort -nr|head -1`
    test $WARN -ge $NORMAL && echo -e "$COLOR warning! $COLOREND" || echo "normal"
    


  • 指令碼條件分支和安全
    1、編寫指令碼createuser.sh,實現如下功能:使用一個使用者名稱作為引數,如果指定引數的使用者存在,就顯示其存在,否則新增之;顯示新增的使用者的id號等資訊
    #!/bin/bash
    read -p "please input your name :" username
    if [ -z $username ];then
    	echo "please input your name again :"
    	exit
    elif id $username &>/dev/null;then
    	echo "this username is exists!"
    	exit
    else
    	useradd $username && echo "$username is created, `id $username`"
    fi
    
    2、編寫指令碼yesorno.sh,提示使用者輸入yes或no,並判斷使用者輸入的是yes還是no,或是其他資訊
    #!/bin/bash
    read -p "please input yes or no :" input
    case $input in
    Y|y)
    	echo "you choose yes"
    	;;
    [Yy][Ee][Ss])
    	echo "you choose yes"
    	;;
    N|n)
    	echo "you choose no"
    	;;
    [Nn][Oo])
    	echo "you choose no"
    	;;
    *)
    	echo "please input yes or no again !"
    	;;
    esac
    
    3、編寫指令碼filetype.sh,判斷使用者輸入檔案路徑,顯示其檔案型別(普通,目錄,連結,其他檔案型別)
    #!/bin/bash
    read -p "please input a file :" file
    [[ -z $file ]] && { echo "please input a correct file name !" ; exit ; }
    type=`ls -ld $file|cut -c 1`
    case $type in
    -)
    	echo "ordinary file"
    	;;
    d)
    	echo "Dir file"
    	;;
    l)
    	echo "Link file"
    	;;
    *)
    	echo "othsers"
    	;;
    esac
    
    4、編寫指令碼checkint.sh,判斷使用者輸入的引數是否為正整數
    #!/bin/bash
    read -p "please enter a positive integer :" enter
    [[ -z $enter ]] && { echo "please enter again !"; exit 10; }
    if [[ $enter =~ ^[1-9][0-9]*$ ]];then
    	echo "right!"
    else
    	echo "error!"
    fi
    
    5、讓所有使用者的PATH環境變數的值多出一個路徑,例如:/usr/local/apache/bin
    vim /etc/profile.d/env.sh
    
    export PATH=/usr/local/apache/bin:$PATH
    
    source /etc/profile.d/env.sh
    
    6、使用者root登入時,將命令指示符變成紅色,並自動啟用如下別名:
    rm= ` rm -i `
    cdnet= `cd /etc/sysconfig/network-scripts`
    editnet= `vim /etc/sysconfig/network-scripts/ifcfg-eth0`
    editnet= `vim /etc/sysconfig/network-scripts/ifcfg-eno16777736或ifcfg-ens33` (如果系統是CentOS7)
    vim /etc/issue
    
    rm= `rm -i`
    cdnet= `cd /etc/sysconfig/network-scripts/`
    editnet= `vim /etc/sysconfig/network-scripts/ifcfg-eth0`
    editnet= `vim /etc/sysconfig/network-scripts/vim /root/reset.shifcfg-ens33`
    
    7、任意使用者登入系統時,顯示紅色字型的警示提醒資訊“Hi,dangerous!”
    vim /etc/issue
    
    \e[1;31m Hi,dangerous! \e[0m
    
    8、編寫生成指令碼基本格式的指令碼,包括作者,聯絡方式,版本,時間,描述等
    vim ~/.vimrc
    
    set number              
    set ai
    set cursorline
    set completeopt=preview,menu
    set enc=utf-8
    syntax on
    set showmode
    set showcmd
    set mouse=v
    set ruler
    set hlsearch
    set spell spelllang=en_us
    
    filetype indent on
    autocmd BufNewFile *.sh exec ":call SetTitle()"
    func SetTitle()
    	if expand("%:e")=='sh'
    		call setline(1,"#!/bin/bash")
    		call setline(2,"#")
    		call setline(3,"#*************************************")
    		call setline(4,"#author:                        Blurry")
    		call setline(5,"#date:           ".strftime("%Y-%m-%d"))
    		call setline(6,"#description:                     test")
    		call setline(7,"#*************************************")
    		call setline(8,"#                                     ")
    
    endif
    
    endfunc
    
    9、編寫使用者的環境初始化指令碼reset.sh,包括別名,登入提示符,vim的設定,環境變數等
    #!/bin/bash
    cat > /etc/profile.d/env.sh << EOF
    PS1="\[\e[35;47m\][\[email protected]\h \t \W]\\$\[\e[0m\]"
    alias cdnet="cd /etc/sysconfig/network-scripts"
    alias editnet="vim /etc/sysconfig/network-scripts/ifcfg-ens33"
    export PATH=/app/bin:$PATH
    EOF
    
    cat > ~/.vimrc << EOF
    set nu
    EOF