練習腳本和一些可用腳本(更新中...)

零碎知識點

  • 開啓一個腳本相當於開啓一個子進程,變量不會繼承
  • 注意()裏面也會開啓子進程,變量不會繼承,如果想要作爲一個整體要用{}。
  • 但是注意exit的返回值即使在()裏面,但如果它是最後一個命令執行過後產生的,沒有再賦予新值,則它在當前的shell中仍然可以繼承判斷。就如同之前的 echo $name;(echo $name;) ,只要小括號內沒有賦值,則兩個name顯示的結果一樣,小括號和直接輸入bash開啓子進程仍然略有不同。

.vimrc設置,它要放在 ~/內

~/.vimrc

set ignorecase
set cursorline
set autoindent
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:                ZhangYinsheng") 
        call setline(5,"#QQ:                    744004845") 
        call setline(6,"#Date:                  ".strftime("%Y-%m-%d"))
        call setline(7,"#FileName:             ".expand("%"))
        call setline(8,"#URL:                   https://blog.51cto.com/14228129.")
        call setline(9,"#Description:          The test script")                                                        
        call setline(10,"#Copyright (C):        ".strftime("%Y")." All rights reserved")
        call setline(11,"#********************************************************************") 
        call setline(12,"") 
        endif
endfunc
autocmd BufNewFile * normal G

判斷centos版本

IDCentos_release_version.sh

#!/bin/bash
case "`cat /etc/redhat-release |grep -Eo " [0-9]{1,2}"|tr -d " " `"  in
        4) 
                echo "Centos version is 4" ;; 
        5)
                echo "Centos version is 5" ;;
        6)
                echo "Centos version is 6" ;;
        7)      
                echo "Centos version is 7" ;;
        8)
                echo "Centos version is 8" ;;
        9)
                echo "Centos version is 9" ;;
        10)
                echo "Centos version is 10" ;;                                              
        esac
  • 去掉數字前方一個或者多個空格有一個簡單的方法,就是讓一個變量等於它,然後輸出的時候不加上引號,就可以了,空格也算在格式裏面了。
  • 比如把上面的case後面的長式子賦值給一個變量之後再判斷,就不需要用tr命令去除空格了。
  • 但如果是字符中間的多個空格用這種方法會變成一個,不會全部去掉要注意。

報警磁盤和inode編號用量:

#!/bin/bash
Perscent=10  #警告閾值    
Usedisk="`df | grep "/dev/sd"|grep -E " [0-9]+%" -o| tr -d "%" |sort -nr |head -1 `";
InodeUsedisk="`df -i | grep "/dev/sd"|grep -E " [0-9]+%" -o| tr -d "%" |sort -nr |head -1 `";
[ $Usedisk -ge $Perscent ] && echo Warning! Useddisk is ${Usedisk}% ;
[ $InodeUsedisk -ge $Perscent ] && echo Warning! InodeUseddisk is ${InodeUsedisk}% ;
[ $Usedisk -lt $Perscent ] && [ $InodeUsedisk -lt $Perscent ] && echo Disk Using Condition is Good!;

腳本參數引用以及$*,$@,shift判斷

agr1.sh agr1 arg2...

#!/bin/bash
echo "1st arg is $1"
echo "2st arg is $2"
echo "2st arg is $3"
echo "10st arg is ${10}"
echo "all args are $*"
echo "all args are $@"
echo "args sumnumber is $#"
echo "scriptname is `basename $0`"
echo -e "\e[1;33marg2 use \$* begins test:\e[0m"
arg2.sh "$*"  #use $*
echo -e "\e[1;33marg3 use \$@ begins test:\e[0m"
arg3.sh "$@"  #use $@

echo -e "\e[1;32mShift once test:\e[0m"
shift
echo "1st arg is $1"
echo "2st arg is $2"
echo "2st arg is $3"
echo "10st arg is ${10}"
echo -e "\e[1;32mShift twice test:\e[0m"
shift
echo "1st arg is $1"
echo "2st arg is $2"
echo "2st arg is $3"
echo "10st arg is ${10}"       

arg2.sh同arg3.sh:

#!/bin/bash
echo "1st arg is $1"
echo "2st arg is $2"
echo "2st arg is $3"
echo "10st arg is ${10}"
echo "all args are "$*""
echo "all args are "$@""
echo "args sumnumber is $#"       
echo "scriptname is `basename $0`"

測試環境變量腳本中傳遞

father.sh

#!/bin/bash
export name=farther

echo "start farther.sh"
echo "name=$name"
echo "farther pid is $BASHPID"
son.sh

son.sh

#!/bin/bash
echo "starting son.sh "
echo "son.sh pid is $BASHPID"
echo "name=$name"
sleep 100

網絡拷貝文件到另一臺主機(簡單版本)

scpfile.sh arg1 arg2....

#!/bin/bash
scp $*  [email protected]:/data/scripttest

查看主機的各種信息,包括主機名,IPv4地址,操作系統版本,內核版本, CPU型號,內存⼤⼩,硬盤⼤⼩

SysteminfoCheck.sh

#!/bin/bash

備份文件(手動版本)

backup.sh

#! /bin/bash                                                                                                                   
#SOURCE="/data/script36"
DEST="/data/backup/backupfile_`date "+%F_%T"`" #Destination cp directory
BEGINCOLOR="\e[1;35m"
ENDCOLOR="\e[0m"
NOPASS=0                                       #Used to determine whether All filename are correct and exist, 0 is yes ,1is no.
#ARGNUM=$#
set -u    
                                               #variable is not exist ,then exit
#set -e                      
if [ $# -eq 0 ]; then
        echo -e "\e[1;31mPlease input one or more filename[directory]correctly!!!\e[0m"
        echo -e "\e[1;31mFor example :\e[0m\n\e[1;32mbackup.sh\e[0m /home/zhang/ /dev/zero ..."
        exit
else
        echo -e "\e[1;36mCheck input filename whether exists or not...\e[0m"
        for n in $*;do
                if [[ ! -a "$n" ]];then
                        echo -e "\e[1;31mThere is no file[Dir]\e[0m\e[1;5;41m$n\e[0m";
                        NOPASS=1;
                        #Judge if file exist;
                fi
        done
#exit;  
if [ $NOPASS -eq 1 ] ;then
echo -e "\e[1;5;31mCheck Not Pass ,Please check your input filename and redo!!!\e[0m"
exit
fi
echo -e "\e[1;32mFilename check done,All file exists\e[0m"
fi
#sleep 50
echo -e "\e[1;33mGet Ready For Backup After 10 Seconds...\e[0m"
sleep 0.5
echo -e "\e[1;5;31mYou can press down key \e[0m\e[1;5;34m\"ctrl + c\"\e[0m\e[1;5;31m to stop backup now!\e[0m"
sleep 0.5
echo "Counting down:"
for i in {1000..1};do                         #10seconds to stop backup
        x=$[i/100];
        y=$[i%100];
        if [ $y -lt 10 ]; then
                y="0$y"
        fi      
        let Rdmcolor=$RANDOM%7+31
        echo -ne "\e[1;${Rdmcolor}m\e[K${x}.${y}\e[0m\r";
        sleep 0.01
done
if [[ ! -d "$DEST" ]]; then
        mkdir -p "$DEST";
        echo -e "\e[1;32mMaking Backup_Destination_Directory Successfully!!!\e[0m";
fi
#sleep 0.1
echo -e "${BEGINCOLOR}Starting backup....$ENDCOLOR"   
#sleep 0.1
cp -a -v -u --backup=numbered $* $DEST
echo -e "${BEGINCOLOR}Backup is finished~$ENDCOLOR"
echo -e "\e[1;32mFile backup to Directory '$DEST'\e[0m"
# unset SOURCE DEST BEGINCOLOR ENDCOLOR

每日定時備份文件(自動版本)

  • 可以用軟鏈接指向backup.sh並判斷來進行設置,也可重新在寫一個腳本

查看當前遠程連接到此電腦的主機數的IP地址和個數,並按照從大到小順序排列。

linkcheck.sh

#!/bin/bash
echo "The Sumnumber and IPAdress Connecting Now:"                                                               
netstat -tan | tr -s " " ":" |cut -d: -f6 |grep -Eo "(([1-9][0-9]?|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9][0-9]?|1[0-9]{2}|2[0-4][0-9]|25[0-5])"|sort|uniq -c
  • 附加用法:可以這樣寫來加入路徑
    PATH=/###/###:$PATH
    echo $PATH >> /etc/profile.d/evn.sh

新裝linux環境設置(初版)

  • 變量修改要包含PATH ,HISTCONTROL ,HISTFORMAT,HISTSIZE,PS1
  • 要改別名
  • 要改登陸提示信息issue,motd
  • 要改umask控制新建文件的權限等
  • 要配置~/.vimrc
#! /bin/bash
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章