红联Linux门户
Linux帮助

Shell脚本点滴

发布时间:2014-12-19 10:01:31来源:linux网站作者:wangwenwen

1. 获取脚本当前路径:

FILE_DIR=`echo $(cd $(dirname $0); pwd)`


2. shell整数计算可用expr,非整数计算可用awk内置函数实现:

四舍五入:

awk BEGIN'{printf("%d", 1.7+0.5)}'

保留小数:

awk BEGIN'{printf "%.2f\n", 2/3}'


3. (())

while read line

do

((lineNum++))

done < inputfile


4.  在文件中查找关键字时,用grep -c XXX 比grep XXX | wc -l 的效率要高,

但是查找进程时,ps -ef | grep XXX | grep -v grep |wc -l 才是对的,因为要过滤掉grep进程


5.  grep -A4 '#!/bin/bash' dir.sh   #-A4 代表显示前四行


6. cut命令

linux-0xvi:/opt/Program # echo abcdefghi | cut -b2-5

bcde

linux-0xvi:/opt/Program # echo abcdefghi | cut -c2-5

bcde

linux-0xvi:/opt/Program # echo abc:def:hij:kmn | cut -d: -f2-3

def:hij


7.linux捕获信号:

linux-0xvi:/opt/Program # cat signal.sh

#!/bin/bash

function handle

{

echo "received a signal"

}

trap 'handle' HUP INT QUIT TSTP

sleep 5


8.用stty和dd实现暂停

linux-0xvi:/opt/Program # cat press.sh

#!/bin/sh

function char

{

settty=$(stty -g)

stty raw

stty -echo

dd if=/dev/tty bs=1 count=1 2> /dev/null

stty echo

stty -raw

stty $settty

}

echo -n "press any key to continue..."

input=`char`


9. perl -e 'print time'


10. 一条命令建立目录树: mkdir -p test/{inc,src,help/{html,pdf,doc}}