shell 总结

Reference:

http://saiyaren.iteye.com/blog/1943207

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

shell
的 if 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.    
使用
wget 和
curl 时出错需要用引号引用
URL

使用
wget 和
curl 时需要用引号引用住
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

} &

&
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 "[email protected]"; do

echo $i

done

[email protected]
是获取的输入参数的数组, $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 "$

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”

Test
是 name

Aaa
是 value

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/

用管道线默认确认提示

时间: 2024-08-04 20:02:48

shell 总结的相关文章

【Linux系列】【基础版】第四章 Shell基础之正则表达式

4. Shell基础之正则表达式     4.1 正则就是一串有规律的字符串         4.1 grep              4.1.1 格式: grep [-cinrvABC] 'word' filename             4.1.2 -c //count,表示行数             4.1.3 -i //不区分大小写             4.1.4 -n  //显示行号             4.1.5 -r  //遍历所有子目录             4

linux Shell函数

Shell函数类似于Shell脚本,里面存放了一系列的指令,不过Shell的函数存在于内存,而不是硬盘文件,所以速度很快,另外,Shell还能对函数进行预处理,所以函数的启动比脚本更快. 1.函数定义 1 2 3 4 function 函数名() {     语句     [return] } 关键字function表示定义一个函数,可以省略,其后是函数名,有时函数名后可以跟一个括号,符号"{"表示函数执行命令的入口,该符号也可以在函数名那一行,"}"表示函数体的结

Shell实现跳板机,为什么用跳板机

整理自:http://blog.chinaunix.net/uid-22101889-id-3167454.html 注意:请谨慎使用,到现在为止,使用了,我还没找到改回去的方法. 1.     问题 第一.很多大公司的服务器都不允许直接登录,而是通过一个跳板机才能登录过去.在跳板机中,通常只能执行几个少数命令(如SSH),而其他命令是不允许执行的,那么怎样才能实现这个功能呢? 第二.一些小公司,由于服务器比较少,不需要什么跳板机之类的说法,公司的开发运维人员加起来也就那么十几二十人,通常大家都

linux shell基础语法

1.第一个Shell脚本 打开文本编辑器,新建一个文件,扩展名为sh(sh代表shell),扩展名并不影响脚本执行,见名知意就好,如果你用php写shell 脚本,扩展名就用php好了. 输入一些代码: #!/bin/bash echo "Hello World !" "#!" 是一个约定的标记,它告诉系统这个脚本需要什么解释器来执行,即使用哪一种Shell.echo命令用于向窗口输出文本. 运行Shell脚本有两种方法. 1.1作为可执行程序 将上面的代码保存为t

shell中test命令方法详解

test命令用法.功能:检查文件和比较值 1)判断表达式 if test  (表达式为真) if test !表达式为假 test 表达式1 –a 表达式2                  两个表达式都为真 test 表达式1 –o 表达式2                 两个表达式有一个为真 2)判断字符串 test –n 字符串                                   字符串的长度非零 test –z 字符串                          

shell脚本

-e filename 如果 filename存在,则为真 [ -e /var/log/syslog ]-d filename 如果 filename为目录,则为真 [ -d /tmp/mydir ]-r filename 如果 filename可读,则为真 [ -r /var/log/syslog ]-w filename 如果 filename可写,则为真 [ -w /var/mytmp.txt ]-x filename 如果 filename可执行,则为真 [ -L /usr/bin/gr

20.5 Shell脚本中的逻辑判断;20.6 文件目录属性判断;20.7 if特殊用法;20.8 20.9 cace判断(上下)

扩展: select用法 http://www.apelearn.com/bbs/thread-7950-1-1.html 20.5 Shell脚本中的逻辑判断 格式1:if 条件 ; then 语句; fi 1. 创建if1.sh测试脚本: [[email protected] ~]# vi if1.sh a=5,如果a大于3,满足这个条件,显示ok 添加内容: #!/bin/bash a=5 if [ $a -gt 3 ] then echo ok fi 2. 执行if1.sh脚本: [[e

20.1 Shell脚本介绍;20.2 Shell脚本结构和执行;20.3 date命令用法;20.4 Shell脚本中的变量

20.1 Shell脚本介绍 1. shell是一种脚本语言 aming_linux blog.lishiming.net 2. 可以使用逻辑判断.循环等语法 3. 可以自定义函数 4. shell是系统命令的集合 5. shell脚本可以实现自动化运维,能大大增加我们的运维效率 20.2 Shell脚本结构和执行 1. 开头(首行)需要加: #!/bin/bash 2. 以#开头的行作为解释说明: 3. 脚本的名字以.sh结尾,用于区分这是一个shell脚本 4. 执行.sh脚本方法有两种:

shell 中seq的用法 echo -n用法

用法:seq [选项]... 尾数 或:seq [选项]... 首数 尾数 或:seq [选项]... 首数 增量 尾数 从1循环到100的两种方法(bash 其它的shell没试过)for x in `seq 1 100`;do echo $x;donefor x in {1..100};do echo $x;done echo -n 不换行输出 $echo -n "123" $echo "456" 最终输出 123456 echo -e 处理特殊字符 若字符串中

shell之数组的使用

数组 Array 一段连续的内存空间 1) 定义数组 [[email protected] shell]# aa[0]=martin [[email protected] shell]# aa[1]=jerry [[email protected] shell]# aa[2]=mike [[email protected] shell]# aa[10]=alice [[email protected] shell]# bb=(192.168.1.1 192.168.1.2 192.168.1.3