Linux shell编程

  1、按行读取文件内容:

  方法1):

while read line
do
    echo $line
done < testfile

  方法2):

cat testfile | while read line
do
    echo $line
done

  使用这种语法时应该注意"跨“进程的问题:如下例,while所在进程是test.sh所在进程的子进程,其对MAX_COST_TIME的修改并未生效!

# test.sh
MAX_COST_TIME=0
cat cost_time.txt | while read COST_TIME
do
    if [ $COST_TIME -gt $MAX_COST_TIME ]
    then
        MAX_COST_TIME=$COST_TIME
    fi
done

# 最终MAX_COST_TIME的值还是0
echo $MAX_COST_TIME

 

  2、流程控制语句

  1)注意在if语句中,数字使用-gt/-lt/-eq等进行比较,而逻辑运算符使用-a(与)/-o(或)等表示

 

  3、here文档。在脚本中向命令传递多行输入,就像该命令是在读取文件或键盘输入一样。例子如下:

  1)将指定文本全部转换为大写:

tr a-z A-z << END_TEXT
one two three
four five six
END_TEXT

  2)输出重定向:

cat << END_TEXT > testfile
These sentences will be printed in testfile.
`date +%Y%m%d`
END_TEXT

  3)here文档与shell函数:

ParseText ()
{
    read line1 && read line2
    echo $line1 $line2
}

ParseText << END_TEXT
The first line.
The second line.
END_TEXT

  4)多行注释:

<< END_TEXT
Muti-line notes.
Muti-line notes.
END_TEXT

echo "This is the code!"

 

  4、shell中的运算:

  1)let内建命令:x=5; let "x=$x*5"; echo $x。

  2)双括号形式:echo $((3 * 100 / 26))。注意shell不支持浮点运算。

  3)将十六进制转换为十进制:echo $((16#100)),或echo $((0x123)),或let num=0x123; echo $num

  将十进制转换为十六进制:echo "obase=16;123" | bc ,或echo 100 16 o p |dc

  4)expr命令:

  基本运算:expr 30 \* 3(*需要转义); num=13; expr $num / 3

  字符串计算(长度):file=test.txt; len=`expr length "$file"`; echo $len

  字符串计算(子串):file=test.txt; expr substr $file 3 3(结果为st.)

  正则表达式:file=test.txt; expr $file : '.*'(相当于计算字符串长度); expr $file : '\(.*\)\.txt'(结果为test)

  5)浮点计算:

# scale指定保留小数点后多少位
$ echo "scale=2; 1+2*3-4/5" | bc
6.20

$ SUM=11
$ CNT=5
$ AVG=`awk -v sum=$SUM -v cnt=$CNT 'BEGIN{print sum/cnt }'`
$ echo $AVG
2.2

 

  5、shell中的列表:

  如list=(one two three four)。取列表的第二个元素:echo ${list[1]};计算列表元素的个数:echo ${#list[@]}

  另一种列表赋值的方法:str="one two three four five"; list=($str)

 

  6、shell函数

  1)参数处理

  (1)getopt(参考自http://blog.linuxeye.com/389.html)。脚本getopt.sh的内容:

#!/bin/bash

#以下假定运行该脚本的命令是bash getopt.sh -a -b arg0 arg1 arg2 -carg3
#则$@是"-a -b arg0 arg1 arg2 -carg3"
#-o指定短选项,--long指定长选项
#"b:"表示-b之后要带参数,"c::"表示-c之后带的是可选参数
#"--"有转义的意味,表示后面的字符串不解析为当前命令的选项。如"mkdir -f"是错误的,但"mkdir -- -f"可以创建名为"-f"的目录
#最后$TEMP的值是"-a -b 'arg0' -c 'arg3' -- 'arg1' 'arg2'"
TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long:: -- "$@"`

if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

#运行该命令后,当前shell环境中$1为"-a"、$2为"-b"、$3为"arg0"...
eval set -- "$TEMP"

while true
do
    case "$1" in
        -a | --a-long) echo "option a"; shift ;;
        -b | --b-long) echo "option b, argument $2"; shift 2 ;;
        -c | --c-long)
            case "$2" in
                "") echo "option c, no argument"; shift 2 ;;
                *)  echo "option c, argument $2"; shift 2 ;;
            esac ;;
        --) shift ; break ;;
        *)  echo "Internal error!"; exit 1 ;;
    esac
done

#剩余未匹配到的参数。即$TEMP中"--"之后的部分
echo "Remaining arguments:"
for argc
do
    echo '--> '"$argc";
done

 

  (2)getopts

 

 

 

 

 

不断学习中。。。

posted on 2013-10-12 11:03  han'er  阅读(313)  评论(0编辑  收藏  举报

导航