Coder Social home page Coder Social logo

scripts's Introduction

##My shell scripts

###skill for bash

1.show line number

export PS4='+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}: '

2.debug apart of code

set -x
THE CODE BE DEBUGED
set +x

3.add debug flag

code like this:

DEBUG=0
_ERR_HDR_FMT="%.23s %s[%s]: "
_ERR_MSG_FMT="${_ERR_HDR_FMT}%s\n"

_log() {
	if test $DEBUG -eq 1 ; then
		printf "$_ERR_MSG_FMT" $(date +%F.%T.%N) ${BASH_SOURCE[1]##*/} ${BASH_LINENO[0]} "${@}"
	fi
}

and run as this:

DEBUG=1 ./test.sh
  1. add text to the head of file, using cat - $file, link stdout to the head of $file

    file=data.txt echo "test text to head of file" | cat - $file >$file.new

  2. merge two lines to one

    sed 'N;s/\n/ /' 1.txt

  3. print the next line of match line

    sed '/33/{n;p}' 1.txt

  4. merge all line to one

    sed ':a;N;s/\n/ /;ba;' 1.txt

  5. print the last line

    tail -n 1 1.txt tail -1 1.txt sed -n '$p' 1.txt awk 'END{print}' 1.txt

  6. output multiple lines

    cat <<TT-Test-111 This is line 1 of message This is line 2 of message This is line 3 of message TT-Test-111

  7. comment block code, it is useful for debuging

    : <<COMMENTBLOCK echo "abc" echo "123" COMMENTBLOCK

    : <<DEBUGXXX for file in * do cat "$file" done DEBUGXXX

  8. array

    DISKS=($(ls /dev/sd*)) LENGTH=${DISKS[@]} for x in "${DISKS[@]}" ; do echo $x done

  9. test expr is equal to [ expr ], for more help man test. some example:

    This code is from /etc/init.d/nfs

    Source networking configuration.

    [ -f /etc/sysconfig/network ] && . /etc/sysconfig/network

    Check that networking is up.

    [ "${NETWORKING}" != "yes" ] && exit 6 [ -x /usr/sbin/rpc.nfsd ] || exit 5

  10. print function name:

    function test_func() { echo "Current $FUNCNAME, $FUNCNAME =&gt; (${FUNCNAME[@]})" } test_func

for more info:

###Advance skill

1.cursor moving

"\033[<L>;<C>H"  move curse to specify position, <L> is line number, <C> is crow number
"\033[<N>A"      move the current curse up N lines.(remember to replace <N> to digits)
"\033[<N>B"      move down
"\033[<N>C"      move right
"\033[<N>D"      move left

2.change color

syntax:
	echo -e '\E[COLOR1;COLOR2mSome text goes here.'

FGRED=`printf "\033[31m"`
FGCYAN=`printf "\033[36m"`
BGRED=`printf "\033[41m"`
FGBLUE=`printf "\033[35m"`
BGGREEN=`printf "\033[42m"`
 
NORMAL=`printf "\033[m"`
 
echo "${FGBLUE} Text in blue ${NORMAL}"
echo "Text normal"
echo "${BGRED} Background in red"
echo "${BGGREEN} Background in Green and back to Normal ${NORMAL}"

code color/编码 颜色/动作
0  重新设置属性到缺省设置
1  设置粗体
2  设置一半亮度(模拟彩色显示器的颜色)
4  设置下划线(模拟彩色显示器的颜色)
5  设置闪烁
7  设置反向图象
22 设置一般密度
24 关闭下划线
25 关闭闪烁
27 关闭反向图象
30 设置黑色前景
31 设置红色前景
32 设置绿色前景
33 设置棕色前景
34 设置蓝色前景
35 设置紫色前景
36 设置青色前景
37 设置白色前景
38 在缺省的前景颜色上设置下划线
39 在缺省的前景颜色上关闭下划线
40 设置黑色背景
41 设置红色背景
42 设置绿色背景
43 设置棕色背景
44 设置蓝色背景
45 设置紫色背景
46 设置青色背景
47 设置白色背景
49 设置缺省黑色背景

echo -e "\033[1mThis is bold text.\033[0m"
echo -e "\033[4mThis is underlined text.\033[0m"
echo -e '\E[34;47mThis prints in blue.'; tput sgr0
echo -e '\E[33;44m'"yellow text on blue background"; tput sgr0
echo -e '\E[1;33;44m'"BOLD yellow text on blue background"; tput sgr0

reference:

###pdf

reference:

###wifi module

reference:

###raid module

Reference:

###genmk

a script for generating configure file, which can use to generate Makefile

###ds2img

A python script transfer c header file which contains lots of data structures to a dot script, and then convert to a image file

###pmt

Project MoniTor, for windows and linux platform

###sysmon

System infor monitor (cpu,mem,ps,FD,network)

###netspeed

Get network RT speed and NIC speed

###iscsi

iscsi tools

scripts's People

Contributors

matrix207 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.