shell脚本

功能点

  1. 日志系统

  2. 添加调试参数
    通过参数控制是否开启时间检查

  3. 更新系统时间

  4. 配置文件中的空格
    函数中不加双引号

任意目录上传

#!/bin/bash

if [ $# \= 1 ]
then
    main_path=$1
else
    main_path=/var/home/autoup_all_file
fi

mkdir  -p $main_path


this_filepath=$(dirname $(readlink -f $0))
this_filename=$(basename $0)


echo $this_filepath
echo $this_filename

# 从jenkins上下载其他文件
# get /root/autoup_wac/auto* $main_path
# cp ${this_filepath}/*.ini $main_path
# 用绝对路径执行脚本和配置文件

vim配置文件

if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"
   set fileencodings=ucs-bom,utf-8,latin1
endif

set nocompatible        " Use Vim defaults (much better!)
set bs=indent,eol,start         " allow backspacing over everything in insert mode
"set ai                 " always set autoindenting on
"set backup             " keep a backup file
set viminfo='20,\"50    " read/write a .viminfo file, don't store more
                        " than 50 lines of registers
set history=50          " keep 50 lines of command line history
set ruler               " show the cursor position all the time

" Only do this part when compiled with support for autocommands
if has("autocmd")
  augroup redhat
  autocmd!
  " In text files, always limit the width of text to 78 characters
  " autocmd BufRead *.txt set tw=78
  " When editing a file, always jump to the last cursor position
  autocmd BufReadPost *
  \ if line("'\"") > 0 && line ("'\"") <= line("$") |
  \   exe "normal! g'\"" |
  \ endif
  " don't write swapfile on most commonly used directories for NFS mounts or USB sticks
  autocmd BufNewFile,BufReadPre /media/*,/run/media/*,/mnt/* set directory=~/tmp,/var/tmp,/tmp
  " start with spec file template
  autocmd BufNewFile *.spec 0r /usr/share/vim/vimfiles/template.spec
  augroup END
endif

if has("cscope") && filereadable("/usr/bin/cscope")
   set csprg=/usr/bin/cscope
   set csto=0
   set cst
   set nocsverb
   " add any database in current directory
   if filereadable("cscope.out")
      cs add $PWD/cscope.out
   " else add database pointed to by environment
   elseif $CSCOPE_DB != ""
      cs add $CSCOPE_DB
   endif
   set csverb
endif

" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
  syntax on
  set hlsearch
endif

filetype plugin on

if &term=="xterm"
     set t_Co=8
     set t_Sb=dm
     set t_Sf=dm
endif

" Don't wake up system with blinking cursor:
" http://www.linuxpowertop.org/known.php
let &guicursor = &guicursor . ",a:blinkon0"

文件锁

#!/bin/bash

main_path=/home

mutex_file="${main_path}/$(basename $0)_mutex"

if [ -e $mutex_file ]
then
    echo "Other instance is running"
    exit
fi
#[ -e $mutex_file ] && exit

echo $$ > $mutex_file
trap "rm -f ${mutex_file}; exit" 0 1 2 3 9 15

echo 'do something'

sleep 300

复制内容到vim

解决办法:
1,在拷贝前输入:set paste (这样的话,vim就不会启动自动缩进,而只是纯拷贝粘贴)
2,拷贝完成之后,输入:set nopaste (关闭paste)

调试日志

log的配置文件: log_config

#!/bin/bash

# 日志文件保存的位置
main_path="/root"
all_log_file="${main_path}/all.log"
debug_log_file="${main_path}/debug.log"
info_log_file="${main_path}/info.log"
warn_log_file="${main_path}/warn.log"
error_log_file="${main_path}/error.log"

# 设置调试类型的颜色, debug:黑色, info:蓝色, warn:黄色, error:红色
DEBUG_LOG="[debug]"
INFO_LOG="\033[34m[info]\033[0m"
WARN_LOG="\033[33m[warn]\033[0m"
ERROR_LOG="\033[31m[error]\033[0m"

# 自定义命令调用的函数: 将日志保存到对应的文件中, 等级依次递增
function echo_debug()
{
    if [ $# \= 4 ]
    then
        echo -ne [$(date +%Y/%m/%d_%T) $1 $2 $3]: ${DEBUG_LOG} $4 | tee -a $debug_log_file
    elif [ $# \= 3 ]
    then
        echo -ne [$(date +%Y/%m/%d_%T) $1 $2]: ${DEBUG_LOG} $3 | tee -a $debug_log_file
    fi
    echo | tee -a $debug_log_file
}
function echo_info()
{
    if [ $# \= 4 ]
    then
        echo -ne [$(date +%Y/%m/%d_%T) $1 $2 $3]: ${INFO_LOG} $4 | tee -a $debug_log_file $info_log_file
    elif [ $# \= 3 ]
    then
        echo -ne [$(date +%Y/%m/%d_%T) $1 $2]: ${INFO_LOG} $3 | tee -a $debug_log_file $info_log_file
    fi
    echo | tee -a $debug_log_file $info_log_file
}
function echo_warn()
{
    if [ $# \= 4 ]
    then
        echo -ne [$(date +%Y/%m/%d_%T) $1 $2 $3]: ${WARN_LOG} $4 | tee -a $debug_log_file $info_log_file $warn_log_file
    elif [ $# \= 3 ]
    then
        echo -ne [$(date +%Y/%m/%d_%T) $1 $2]: ${WARN_LOG} $3 | tee -a $debug_log_file $info_log_file $warn_log_file
    fi
    echo | tee -a $debug_log_file $info_log_file $warn_log_file
}
function echo_error()
{
    if [ $# \= 4 ]
    then
        echo -ne [$(date +%Y/%m/%d_%T) $1 $2 $3]: ${ERROR_LOG} $4 | tee -a $debug_log_file $info_log_file $warn_log_file $error_log_file
    elif [ $# \= 3 ]
    then
        echo -ne [$(date +%Y/%m/%d_%T) $1 $2]: ${ERROR_LOG} $3 | tee -a $debug_log_file $info_log_file $warn_log_file $error_log_file
    fi
    echo | tee -a $debug_log_file $info_log_file $warn_log_file $error_log_file
}

# 自定义命令, 将时间, 运行位置等信息传给, 上述函数
alias Pdebug='echo_debug $(basename $0) ${FUNCNAME} ${LINENO}'
alias Pinfo='echo_info $(basename $0) ${FUNCNAME} ${LINENO}'
alias Pwarn='echo_warn $(basename $0) ${FUNCNAME} ${LINENO}'
alias Perror='echo_error $(basename $0) ${FUNCNAME} ${LINENO}'

使用方法

在使用的文件中加载配置, log_config_file: 是配置文件, 绝对路径

source $log_config_file

Pdebug "debug information"
Pinfo "show basic information"
Pwarn "some warn"
Perror "fail"

测试程序:c.sh
在这里插入图片描述
效果
在这里插入图片描述
另附:
将shell脚本的所有执行过程保存在文件中

&> 不能分开
>: 覆盖, >> 追加
set -x    ##分步执行
exec &> /tmp/log.txt   ##脚本执行的过程和结果导入/tmp/log.txt文件中

修改系统时间

date -s "2022-01-22 15:49:56"
date +%Y-%m-%d\ %T

在这里插入图片描述

读取ini配置文件

# ini文件
[config]
wac=  sdf sdf  

# .sh文件
#!/bin/bash

function ReadINIfile()
{
    Configfile=$1
    Section=$2
    Key=$3
    ReadINI=`awk -F '=' '/\['$Section'\]/{a=1}a==1&&$1~/'$Key'/{print $2;exit}' $Configfile`
    echo $ReadINI
}

wac_name=$(ReadINIfile /root/config.ini config wac)

echo "-${wac_name}-"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值