SHELL

来自bbxwiki
跳转至: 导航搜索

获得文件集合

  1. 获得文件集合: FILES=(/etc/init.d/*d)
  2. 取第1个: $FILES 或 ${FILES[0]}
  3. 取集合: ${FILES[@]} 或 ${FILES[*]}


读取配置文件

独占行csv风格

# redis http://download.redis.io/releases/redis-3.0.7.tar.gz $SDK_HOME
# 缺点不适用参数本身有空格情况
eval $(cat "$F_SRC_CONF" | grep $1 | awk '{printf("bname=\"%s\" uri=\"%s\" args=\"%s\"", $1, $2, $3)}')
echo "bname=$bname"
echo "uri=$uri"
echo "args=$args"

ini风格

[redis]
URI=http://download.redis.io/releases/redis-3.0.7.tar.gz
PREFIX=$SDK_HOME
 
[googletest]
URI=git@github.com:google/googletest.git
eval $(cat src.conf | sed -n "/^\[redis\]$/,/^[ \t]*$/p" | sed 1d)


字符串修剪

  1. var=${FILES[0]}
  2. remove prefix: NAME=${var#/etc/}
  3. remove suffix: NAME=${NAME%d}

字符串修剪示例

移除扩展名
X="hello-2016.tar.bz2"; echo X=$X; Y=${X%.*}; echo Y=$Y; Z=${Y%.*}; echo Z=$Z

文件名、扩展名

srcfile='/tmp/a/b.d/c/foobar-2018.e1.tar.bz2'
cat << EOF
file-dir=$(dirname $srcfile)
file-name=$(basename $srcfile)
file-path-no-ext=${srcfile%.*} # /tmp/a/b.d/c/foobar-2018.e1.tar
file-ext=${srcfile##*.} # bz2
# dange
file-path-no-all-ext=${srcfile%%.*} # :( Oh my God
file-all-ext=${srcfile#*.} # :( Oh my God
done
EOF


字符串分片示例

以长度为n为例 X=Brian
  1. 获取[0,2]
    Y=${X::2}; echo $Y
  2. 获取[1,3]
    Y=${X:1:3}; echo $Y
  3. 获取[0,n-2]
    Y=${X::(${#X}-2)}; echo $Y

变量命名

  1. XX_HOME 指XX根路径, 如JAVA_HOME
  2. YY_DIR 指YY的目录,如JAVA_BIN_DIR=$JAVA_HOME/bin


小功能

  • 获得本机IP-V4地址
ifconfig | grep "inet" | awk -F" " '{print $2}' | grep -v ":"
  • 多个处理连写"{}在当前进程执行, "()"在子进程执行
xx=old && ( echo old-xx=$xx; xx=new; echo this-xx=$xx; ) && echo global-xx=$xx
yy=old && { echo old-yy=$yy; yy=new; echo update-global-yy=$yy; } && echo global-yy=$yy
  • 获得脚本根路径
# get script dir abs-path
PROJ_ROOT_HOME=$(pushd `dirname $0` >/dev/null; pwd; popd >/dev/null);
  • 按行读取文件
function fread() {  cat $1 | while  read line; do echo $line ; done }
cat /etc/passwd | while  read line; do echo $line ; done
  • 命令行缺省参数
# cmdline arguments
TEST_IN=${1:-test.in}
TEST_OUT=${2:-report/base.0}
[ ! -d "$TEST_OUT" ] && mkdir -p "$TEST_OUT"
  • 获得文件大小
fsz=$(stat -c "%s" "$srcfile")
  • 拷贝文件的日期
touch "$dstfile" -r "$srcfile"

FTP自动登录

  • FTP自动登录
ftp -ni <<EOF
open $FTP_SERVER
user $FTP_USER $FTP_PASSWD
bin
lcd "$LOCAL_STUB_DIR"
cd $SRC_IN_FTP_CORE_DIR
mget installer *.tar.bz2
cd $SRC_IN_FTP_XX_DIR
mget *.tar.bz2
EOF


文件重命名

  • 生成重命名脚本
xx=./*; for x in $xx; do basename $x | awk -F. '{printf("mv \"%s\" \"%s%s.%s\"\n", $0, "haha-xd-201510-0", $2, $3)}' >> 1.sh ; done;
  • 使用printf + eval
for x in $(ls *); do eval $(printf "mv %d xx-%04d.xml" $x $x); done;
  • 日期修改
# 文件重命名
# mv 2017-10-19 105525.jpg 2017-10-19_10-55-25_6S.jpg
for x in *; do l=${x% *}; r=${x#* }; ext=${x#*.*};  r1=${r:0:2};r2=${r:2:2};r3=${r:4:2}; mv "$x" "${l}_${r1}-$r2-${r3}_6S.$ext";  done;


日期加减

  • 日期加减.
    • date +%Y%m%d --date="-1 day"


color-show

  • 彩色输出-函数
#
# utils
# color echo
NORMAL=$(tput sgr0)
RED=$(tput setaf 1; tput bold)
BLUE=$(tput setaf 4; tput bold)
GREEN=$(tput setaf 2; tput bold)
YELLOW=$(tput setaf 3; tput bold)
function red()    { echo -e "$RED$*$NORMAL"; }
function blue()  { echo -e "$BLUE$*$NORMAL"; }
function green()  { echo -e "$GREEN$*$NORMAL"; }
function yellow() { echo -e "$YELLOW$*$NORMAL"; }
  • 终端颜色show
# 终端颜色show
# 前景色
for ((i=0; i<=7; i++)); do  echo $(tput setaf $i; tput bold)"show me the color=$i-bold"$(tput sgr0); done
# 背景色
for ((i=0; i<=7; i++)); do  echo $(tput setab $i; tput bold)"show me the color=$i-bold"$(tput sgr0); done
# 前背景色
for ((i=0, j=7; i<=7, j>=0; i++, j--)); do  echo $(tput setaf $i; tput setab $j; tput bold)"show me the color=$i-bold"$(tput sgr0); done


简单的菜单功能

#!/bin/bash
MYDATE=`date +%d/%m/%Y`
THIS_HOST=`hostname`
USER=`whoami`
while :
do
  clear
  cat<<EOF
  _______________________________________________________________
  User:$USER        Host:$THIS_HOST          DATE:$MYDATE
  _______________________________________________________________
                 1:List files in current dir
                 2:Use the vi editor
                 3:See who is on the system
                 H:Help sreen
                 Q:Exit Menu
  _______________________________________________________________
EOF
  echo -e -n "\tYour Choice [1,2,3,H,Q]>"
  read CHOICE
    case $CHOICE in
    1) ls
      ;;
    2) vi
      ;;
    3) who
      ;;
    H|h)
      cat<<EOF
      This is the help screen,nothing here yet to help you!
EOF
      ;;
    Q|q) exit 0
      ;;
    *) echo -e "\t\007unknown user response"
      ;;
    esac
  echo -e -n "\tHit the return key to continue"
  read DUMMY
done


参见