2018年3月8日 星期四

Linux Vim editor

  • 將部分字串替換

範例:將12~26行中,所有 *.html 替換為 /*
:12,26s/\([a-z]\{1,\}\.html/\/\1/ 
  • .vimrc sample of 1 

    set tabstop=4
    set softtabstop=4
    set shiftwidth=4
    set textwidth=0
    set expandtab
    set autoindent
    set fileformat=unix
    set cursorline
    set showmatch
    set backspace=indent,eol,start
    set ruler       " show line and column number
    set wrap
    set linebreak
    set nolist
    filetype plugin on
    filetype indent on

    "au BufRead,BufNewFile *.py,*.pyw,*.c,*.h match BadWhitespace /\s\+$/
    au BufRead,BufNewFile *.py
                \ set syntax=python |
                \ let python_highlight_all=1 |
                \ let g:pymode_python='python' |
                \ let g:pymode_virtualenv=1 |
                \ let g:pymode_breakpoint=1 |
                \ let g:pymode_syntax=1 |
                \ let g:syntastic_python_python_exec='python' |
                \ inoremap :     |

    au BufRead,BufNewFile [D|d]ockerfile* set syntax=Dockerfile
    au BufRead,BufNewFile [M|m]akefile* set syntax=Makefile
    au BufRead,BufNewFile *.sh set syntax=sh
    au BufRead,BufNewFile *.html,*.php
                \ set syntax=php |
                \ set tabstop=2 |
                \ set softtabstop=2 |
                \ set shiftwidth=2 |
                \ set smarttab |


    set encoding=utf-8
    set fileencoding=utf-8
    set t_Co=256
    colo koehler
    "colorscheme wombat256mod

    inoremap ( ()
    inoremap [ []
    inoremap { {}

    syntax on
    set showcmd
    set nu
    set clipboard=unnamed



2016年9月2日 星期五

SUBNET 與 IP 的計算 (bash script)

function subnetonetmask(){
SUBNET=$1
NUM=128
SUM=0
NETMASK=''

if [ $(( SUBNET % 8 )) -eq 0 ]; then
  M=$(( SUBNET / 8 ))
  N=0
  NETMASK=$(echo $(printf '255.%.0s' $(eval "echo {1.."$((M))"}"))$(printf "$SUM.%.0s" $(eval "echo {1.."$((4-M))"}"))|cut -d'.' -f1,2,3,4)

else
  N=$(( SUBNET % 8 ))
  M=$(( (SUBNET - N)/8 ))
  while true
  do
#   echo "$SUM, $NUM"
    SUM=$(( SUM + NUM ))
    NUM=$(( NUM / 2 ))
    N=$(( N - 1))
    if [ $N -eq 0 ]; then
      break
    fi
  done
  NETMASK=$(echo $(printf '255.%.0s' $(eval "echo {1.."$((M))"}"))$SUM.$(printf "0.%.0s" $(eval "echo {1.."$((4-M-1))"}"))|cut -d'.' -f1,2,3,4)
fi

#echo "M=$M, N=$N"
echo $NETMASK
}

function subip(){
IP=${1}
MASK=${2}
SUBIP=''
for n in 1 2 3 4
do
  if [ $n -eq 4 ]; then
    dot=''
  else
    dot='.'
  fi
  SUBIP=${SUBIP}$(( $(echo $IP|cut -d'.' -f$n) & $(echo $MASK|cut -d'.' -f$n) ))${dot}
done

echo ${SUBIP}
}

=============================================
使用範例:

echo $(subnetonetmask 21)
Result >> 255.255.248.0

echo $(subip 192.168.1.253 255.255.255.128)
Result >> 192.168.1.128