零碎知识点

  • 开启一个脚本相当于开启一个子进程,变量不会继承
  • 注意()里面也会开启子进程,变量不会继承,如果想要作为一个整体要用{}。
  • 但是注意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 $*  name@192.168.1.2:/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