`
saiyaren
  • 浏览: 225849 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

shell 总结 原创-胡志广

阅读更多

1.   Shell 读取文件和写文件

 

for line in $(<top30000.url.utf-8.http_server_front_hphp.txt); do

        tmp_port=8080;

        for((i=0;i<=7;i++));do

echo ${line/192\.168\.12\.63/192\.168\.12\.63:$tmp_port} >>top30000.url.utf-8.http_server_front_hphp_mul_port.txt;

            tmp_port=$[tmp_port+1]

        done;

done;

 

top30000.url.utf-8.http_server_front_hphp.txt文件名

通过<读取文件 line是每行的内容

 

替换文件内容:

${变量/被替换内容/替换内容}

替换两边的一定要注意是{} 不是小括号

输出文件:

echo “xxxx” >文件名

 

追加输出文件:

echo “xxxx” >>文件名

 

第二种读取方式:

$ cat file | while read line; do echo $line; done

 

例:

$ cat /root/test.txt | while read line; do

echo $line;

done

输出是:

aaaa

bbbb

cccc dddd

 

for的是分割输出

$ for line in $(<file); do echo $line; done

aaaa

bbbb

cccc

dddd

2.   shell 参数

sh test.sh 111 222 333

然后再shell中获取参数如下获取,如:

$1 是第一个参数111

$2 是第二个参数 222

$3 是第三个参数 333

依次类推

$0 shell本身

 

3.   Shell if 用法

if [ $1 == "aaa" ]; then

        echo $line

fi

 

if 后需要有空格

[需要有空格

$1后需要有空格

==后需要有空格

“aaa”后需要有空格

]后需要有分号(;)

;号后有空格

 

如果不按照规则,那么会出现如下错误

syntax error near unexpected token `fi'或是then

 

shellif else用法:

if ....; then

  ....

elif ....; then

  ....

else

  ....

fi

 

当运行下面脚本时,如果if 后的 $a 不加引号,则会报:

test.sh: line 5: [: too many arguments

这个错误;所以需要先加上“”编程字符串

  a="hhvm don't run"

        echo $a

        if [  "$a"  == "hhvm don't run" ]; then

                echo "============"

        fi

 

 

4.   shell 分割字符串

#!/bin/bash

 

str="hello,world,i,like,you,babalala"

arr=(${str//,/ })

 

for i in ${arr[@]}

do

    echo $i

done

 

分割成数组arr ,用逗号分割

 

方法2

$ cat split.sh

#!/bin/sh

 

# Script to split fields into tokens

 

# Here is the string where tokens separated by colons

s="first column:second column:third column"

ip_arr=()

IFS=":"     # Set the field separator

set $s      # Breaks the string into $1, $2, ...

i=0

for item    # A for loop by default loop through $1, $2, ...

do

    ip_arr=("${ip_arr[@]}" "$item")

    echo "Element $i: $item"

    ((i++))

done

5.   Shell 获取数组长度

 array=(bill   chen  bai   hu); 

num=${#array[@]}  

 

遍历数组:

for((i=0;i<num;i++))

do

         echo ${atrr[$i]};#这里需要用{}引用上才可以

done;

 

6.   shell 创建空文件

:>input_ware_data.txt;

 

 

7.   Shell 执行命令后给变量赋值

$a=`pwd`;

echo $a

 

这样就可以输出pwd的返回值了

注意这里的引号是键盘1左边的那个按钮

 

 

8.   使用wgetcurl时出错需要用引号引用URL

使用wgetcurl时需要用引号引用住URL

Wget –SO 文件名 “www.baidu.com”

Curl “www.baidu.com”

 

 

9.   Shell累加

c=0

for((i=0;i<10;i++));do

    #c++;

    :  $[c++]

    echo $c;

done;

 

这样去实现累加:

:  $[c++]

冒号和$之间要有空格

10.         Shell 整除

c=90010

echo $((c/1000))

输出90

 

11.         Shell 赋值

i=$((c/1000));

赋值语句前后不能有空格

 

12.         Shell 多进程

for ((i=0;i<5;i++));do

  {

  sleep 3;echo 1>>aa && echo "done!"

  } &

  done

 

就是在按后面加了一个&,在后台执行多个shell脚本

 

 

13.  去掉系统提示信息

执行语句后面加上2>/dev/null就可以清楚系统的输出信息

如:

Curl www.baidu.com 2>/dev/null

这样就不会输出提示信息了

 

 

14.         Shell 创建文件夹

mkdir –p [文件夹]

-p 是递归创建

 

15.         shell获取时间戳

参考网址:

http://www.2cto.com/os/201109/105312.html

 

当前时间戳:(年月日时分秒)

a=`date  +%Y%m%d%H%M%S`

echo $a;

 

6天时间戳:(年月日时分秒)

a=`date -d "-6 day"  +%Y%m%d%H%M%S`

echo $a;

 

-d 中可以是hour , day,week

 

 

16.         Shell 数组

A=(a b c def)

for i in "${A[@]}"; do

 

     echo $i

 

done

 

#输出A的下标

echo ${A[1]}

 

参考:

http://doudouclever.blog.163.com/blog/static/17511231020127288621354/

 

17.         获取输入参数数组

for i in "$@"; do

echo $i

done

 

$@是获取的输入参数的数组,$i是获取的输入的每个参数的值

test.sh 1 2 33

输出的内容为:

1

2

33

这就是获取到的3个输入参数,输入多少个参数都可以

 

 

18.          Find 遍历删除

for file in `find . -name "Runtime"`;do

    echo "$file==========="

    rm -rf $file/*;

    ls -l $file;

done;

 

19.         shell 求余

i=$((2000%1000));

echo $i;

 % 求余

 

20.         Shell 字符串连接

a=”aa”;

a=$a"bb";

echo $a;

 

输出是aabb

 

 

21.         Shell 抽取字符awk

如日志是这样的:

14.245.173.44 - - [25/Apr/2013:22:24:13 +0800] "GET /ProductPageService.aspx?method=GetCommentSummaryBySkuId&referenceId=1008195527&callback=getCommentCount HTTP

 

每个空格就是一个位置,那么抽取

/ProductPageService.aspx?method=GetCommentSummaryBySkuId&referenceId=1008195527&callback=getCommentCount

这个就是第7

 

awk '{print $7}' clubservice_aspx.log >cbs.log

抽取文件clubservice_aspx.log,然后输出到cbs.log

详细的见下面的url

http://hi.baidu.com/ziyingshaozhu/item/8473541d9cf6028488a95681

 

22.         shell 按行切割文件

#按每个文件1000行来分割除

split -l 1000 httperr8007.log httperr

split  -l 行数 切割的文件 生成文件前缀

详细见:

http://blog.sina.com.cn/s/blog_62db9b1901017aiz.html

 

23.         查找目录下的所有文件

a=`ls cbs`

#echo $a

for file in $a;do

    echo $file

done

 

 

24.         shell 判断为空

1. 变量通过" "引号引起来

如下所示:,可以得到结果为 IS NULL.

        #!/bin/sh

        para1=

        if [ ! -n "$para1" ]; then

               echo "IS NULL"

        else

                echo "NOT NULL"

        fi

         if [ -z $para1 ];then

                     echo “is null”

       fi

2. 直接通过变量判断

如下所示:得到的结果为: IS NULL

        #!/bin/sh

        para1=

        if [ ! $para1 ]; then

            echo "IS NULL"

        else

            echo "NOT NULL"

        fi

         

3. 使用test判断

     得到的结果就是: dmin is not set!

        #!/bin/sh

        dmin=

        if  test -z "$dmin"  then

          echo "dmin is not set!"

        else 

          echo "dmin is set !"

        fi

 

 

4. 使用""判断

        #!/bin/sh 

        dmin=

        if [ "$dmin" = "" ]; then

          echo "dmin is not set!"

        else 

          echo "dmin is set !"

        fi

 

详细查看

http://blog.csdn.net/runming918/article/details/7226507

 

 

25.         shell 逻辑运算符

一、逻辑运算符

 

 

 

逻辑卷标

表示意思

1.

关于档案与目录的侦测逻辑卷标!

-f

常用!侦测『档案』是否存在 eg: if [ -f filename ]

-d

常用!侦测『目录』是否存在

-b

侦测是否为一个『 block 档案』

-c

侦测是否为一个『 character 档案』

-S

侦测是否为一个『 socket 标签档案』

-L

侦测是否为一个『 symbolic link 的档案』

-e

侦测『某个东西』是否存在!

2.

关于程序的逻辑卷标!

-G

侦测是否由 GID 所执行的程序所拥有

-O

侦测是否由 UID 所执行的程序所拥有

-p

侦测是否为程序间传送信息的 name pipe 或是 FIFO (老实说,这个不太懂!)

3.

关于档案的属性侦测!

-r

侦测是否为可读的属性

-w

侦测是否为可以写入的属性

-x

侦测是否为可执行的属性

-s

侦测是否为『非空白档案』

-u

侦测是否具有『 SUID 』的属性

-g

侦测是否具有『 SGID 』的属性

-k

侦测是否具有『 sticky bit 』的属性

4.

两个档案之间的判断与比较 ;例如[ test file1 -nt file2 ]

-nt

第一个档案比第二个档案新

-ot

第一个档案比第二个档案旧

-ef

第一个档案与第二个档案为同一个档案( link 之类的档案)

5.

逻辑的『和(and)』『或(or)

&&

逻辑的 AND 的意思

||

逻辑的 OR 的意思

  

 

运算符号

代表意义

=

等于 应用于:整型或字符串比较 如果在[] 中,只能是字符串

!=

不等于 应用于:整型或字符串比较 如果在[] 中,只能是字符串

小于 应用于:整型比较 [] 中,不能使用 表示字符串

大于 应用于:整型比较 [] 中,不能使用 表示字符串

-eq

等于 应用于:整型比较

-ne

不等于 应用于:整型比较

-lt

小于 应用于:整型比较

-gt

大于 应用于:整型比较

-le

小于或等于 应用于:整型比较

-ge

大于或等于 应用于:整型比较

-a

双方都成立(and 逻辑表达式 –a 逻辑表达式

-o

单方成立(or 逻辑表达式 –o 逻辑表达式

-z

空字符串

-n

非空字符串

 

参考网址:

http://www.cnblogs.com/chengmo/archive/2010/10/01/1839942.html

 

 

26.         curl引用cookie

curl –b test=”aaa” http://club.jd.com

 

-b name=”value”

Testname

Aaavalue

 

27.         查看进程使用物理内存

Ps aux|grep xxx

查询出pid

比如pid 11460那么查询物理内存就如下:

cat  /proc/11460/status|grep VmRSS

返回结果:

VmRSS: 190332 kB VmRSS: 190332 kB

 

28.         Grep 不区分大小写

grep –i  “aaa”

 

 

29.         awk 分割字符串

a.       log中的信息为:

a:1

b:2

c:3

…..

然后通过:分割,那么a,b,c就是$1;1,2,3就是$2
写法如下:-F 后面写上分割的符号

cat a.log|awk -F ':' '{print $1}'

 

30.         查看线程

ps -eLf|grep hhvm

 

31.         shell 提示符,输入内容进行选择

如提示内容,然后输入yes no 等等的,然后进入不同分支

while true;do

#stty -icanon min 0 time 100

echo -n "Automatic execute ten seconds after,Are you sure you want to start the task(yes or no)?"

read Arg

case $Arg in

c)

  echo "cc"

  exit;;

Y|y|YES|yes)

  break;;

N|n|NO|no)

  exit;;

"")  #Autocontinue

  break;;

esac

done

 

echo

echo "others function..."

 

32.         curl 获取http状态

http_status=`curl -s -w %{http_code} -o /dev/null -e $REFERER_URL "$line" 2>/dev/null `;

-s 是清楚垃圾信息;

-w

 

33.         Cp  强制覆盖

yes|cp a.txt /export/

 

用管道线默认确认提示

2
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics