SHELL 示例

来源:互联网 发布:网络攻击模拟器 编辑:程序博客网 时间:2024/05/17 08:23

处理命令行参数

测试: sh test.sh -b qiao -a -c  -- asdf sd  w w

#!/bin/bashwhile getopts :ab:c optdo    case "$opt" in     a) echo fund -a;;    b) echo fund -b with option $OPTARG;;    c) echo fund -c ;;    *) echo unknown arg $opt;;    esacdone#echo $* $OPTINDshift $[ $OPTIND - 1 ]count=1for i in "$@"do    echo para $count is $i    count=$[ $count+1 ]done

读取命令

#!/bin/bash                                                                                                                                                                  read -p "Enter your name please:"  first lastecho Welcom $last.$firstread -p "And your favourate?:"echo Your favourate is : $REPLY

设置超时时间:

#!/bin/bash# timing readif read -t 5 -p "请输入姓名:" name then    echo Welcom $name!                                                                                                                                                                                                                       else    echo "Sorry, timeout!!"fi~~

读取文件:

#!/bin/bash#读取文件测试cat /etc/passwd | while read line                                                                                                                                                                                                            do    echo Line: $line    IFS_OLD=$IFS        IFS=:    for value in $line    do          echo "  $value"    done    IFS=$IFS_OLDdone~

信号

禁止中断 (使用trap命令)

#!/bin/bashtrap "echo '您不能中断执行'" SIGINT SIGTERMecho "测试开始"count=1while [ $count -lt 5 ]do     echo "Loop #$count"    sleep 3    count=$[$count+1]done

函数

#!/bin/bashecho "Now Program [$(basename $0)] Begin. PID: $$ TIME:$(date)"function addem {    if [ $# -le 0 ] || [ $# -gt 2 ]    then        echo -1    elif [ $# -eq 1 ]    then        echo $[ $1 + $1 ]    else        echo $[ $1 + $2 ]    fi}r1=$(addem 1 3)r2=$(addem 1)r3=$(addem)echo r1:$r1 r2:$r2 r3:$r3

函数里的全局变量: 默认都是全局的

#!/bin/bashecho "Now Program [$(basename $0)] Begin. PID: $$ TIME:$(date)"function dbl {    value=$[ $value*2]}read -p"Please enter a value:" valuedblecho the new value is $value

数组传参

#!/bin/bashecho "Now Program [$(basename $0)] Begin. PID: $$ TIME:$(date)"function testarray {    local newArray=(`echo "$@"`)    echo the new array in func is : ${newArray[*]}}myarray=(one two three)echo the original array is : ${myarray[*]}testarray ${myarray[*]}

返回数组

#!/bin/bashecho "Now Program [$(basename $0)] Begin. PID: $$ TIME:$(date)"function testarray {    local arr=(2 3 4 5)    echo ${arr[*]}}myarray=$(testarray)echo the array is : ${myarray[*]}

递归(斐波那契数列)

#!/bin/bashfunction factorial {    if [ $1 -eq 1 ]     then        echo $1    else        local tmp=$(factorial $[ $1-1 ])        echo $[ $tmp * $1 ]    fi}read -p"Please enter:" valresult=$(factorial $val)echo The factorial of $val is $result

sed 与 gawk

统计 $PATH 中各个目录中文件的个数

#!/bin/bashallPath=$(echo $PATH | sed 's/:/ /g')for dir in $allPathdo    check=$(ls $dir)    count=0    for file in $check    do        count=$[ $count+1 ]    done    echo $dir - $countdone

给数字添加“ ,” ,其中的正则表达式为:

#!/bin/bashfactorial=1counter=1read -p"Enter a number:" number#number=$1while [ $counter -lt $number ]do    factorial=$[ $factorial * $counter]        counter=$[ $counter+1 ]doneecho Original data: $factorialresult=$(echo $factorial | sed '{        :start        s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/        t start        }')echo $result

每行前添加行号

 sed '=' test.txt  sed 'N;s/\n/ /'


删除多余空白行 (连续空白多余一行)

sed '/./,/^$/!d' test.txt

实用工具

列出家目录下,按大小排序的前十名的文件,并输出总大小

du  --max-depth=1 ~ | sort -nr | sed "="  | sed "N; s/\n/ /g" | sed '{11,$D}' |gawk 'BEGIN{total=0}{total+=$2;print $1":",$2,$3;}END{print "TOTAL:"total}'

du  --max-depth=1 ~ | sort -nr | sed "="  | sed "N; s/\n/ /g" | sed '{11,$D}' |gawk 'BEGIN{total=0}{total+=$2;print $1":",$2,$3;}END{print "TOTAL:"total}'


Delete_User.sh




shell中的${},##和%%的使用

http://blog.csdn.net/shmilyringpull/article/details/7631106


0 0