Chinaunix首页 | 论坛 | 博客
  • 博客访问: 634376
  • 博文数量: 17
  • 博客积分: 248
  • 博客等级: 二等列兵
  • 技术积分: 237
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-26 12:13
个人简介

别让往昔的悲伤和对未来的恐惧,毁了你当下的幸福。

文章分类

全部博文(17)

文章存档

2013年(6)

2012年(2)

2011年(9)

我的朋友

分类: Python/Ruby

2011-09-01 00:21:32

#### shell 的定义
     shell 是作为用户与unix系统之间的操作接口的程序
#### 在linux系统中mount DOS软盘
   $ mount -t msdos /dev/fd0 /mmt/floppy
   $ cp * /mmt/floppy
   $ umount /dev/fd0
   $ls -p  在目录后面加上/
#### file 和 ls (list) 命令
  常用选项(man ls)
   $ls -a   列出所有文件包括隐藏文件
   $ls -l  列表方式列出
   $ls -R  列出目录下的内容
   $ls -t  以文件最近被修改的时间排序
   $ls -r  旧的文件在前
  
#### cat filename  查看文件
   $cat -n filename
  
#### wc filename 统计文件行数,单词数,字符数
   $wc -l filename
   $wc -w filename
   $wc -c filename
  
#### cp source destination
   $cp -i source destination   交互式复制
   $cp -r source destination  递归复制
   $cp -f source destination  强制复制
   $cp -p source destination  复制保留权限
#### mv source destination
   $mv -i source destination
#### rm filename
   $rm -irf filename
  
  
  
nohup /usr/local/mine/message_receive_test/bin/start.sh 5 > stdout.log 2>&1 &
#### mkdir
   $mkdir -p /temp/directory/test
  
#### 使用cd(change directory)、pwd、ls、mkdir、cp
   $cd /home/denise/krsort.dir.old
   $pwd
   $ls -l
   $mkdir ../krsort.dir.new
   $cp krsort ../krsort.dir.new
   $ls -l ../krsort.dir.new
   $mv krsort ../krsort.dir.new
   $touch ../krsort.dir.new/.dotfile  (touch用来创建空文件或更新文件时间标记)
   $rm -ifr ../krsort.dir.new/.dotfile
#### >、>> 重定向  >!、>>! 强制覆盖 (输出重定向)
   0  标准输入
   1  标准输出
   2  标准错误输出
  
#### 参看文件内容:cat、more、head、tail
#### split、wc、 sort
  $split -l line_count 把文件分成行为line_count的多个文件
  $split -b n      把文件分成字节数为n的多个文件
  $wc -l filename   文件行数
  $wc -c filename  文件字符数
  $wc -w filename  文件字数
  $sort -t: -r -u -k4 filename 以:为分隔符,对第四个域进行反排序并删除重复的行。(sort默认的第一个域是0. -m是合并两个文件)
#### find
   $find -atime +/-n  查找n天前访问的文件。+n 表示早于n天前访问的,-n 是表示晚于n天前
   $find -ctime n   修改的文件
   $find -empty
   $find -exec command
   $find -group name
   $find -print
   $find -type
   $find -size +/-n
   $find -name
   $find . -user root -type f -perm 775 -print
   $find . -size +5000c \( -name *.a -o -name *.c\) -print | wc -l
   $find . -name "*.log" -mtime +10 -exec rm {} \;
   $find . -name "*.log" | xargs rm
#### chmod   (u-user,g-group,o-other,a-all,operation:'+,-,=')
   $chmod a=r *
   $chmod go-w,a+x filename
   $chmod 421 filename
#### chown change owner
   $chown options user:group filename
   $chown -R test: /home/test/fileone
  
#### chgrp change group
   $chgrp options group filename
  
  
#### vi useage
     command:
     h left a char
     l ringht a char
     j up a line
     k down a line
     G end of the file line
     nG move to the file n line
     w move next word begin
     W move next word begin and ignore punctuation(标点符号)
     b move front word's begin
     B move front word's begin and ignore punctuation
     L move the screen last line
     M move the screen middle line
     H move the screen first line
     e move the next word end
     E move the next word end and ignore punctuation
     ( move the statement begin
     ) move the statemnet end
     { move the part begin
     } move the part end
     0 move the current line begin
    
     r 替换当前文本
     R 一直替换倒按下ESC
     cw 更改至下一个字开始处
     cc 替换当前行
     cG
    
     ctr-e 将屏幕上滚一行
     ctr-y 将屏幕下滚一行
     ctr-u 将屏幕上滚半页
     ctr-d 将屏幕下滚半页
     ctr-b 将屏幕上滚一页
     ctr-f 将屏幕下滚一页
    
#### shell program
  $ echo
  $ date
  $ read
  $0   命令名     $1-$9 第1个参数倒第9个参数
  $#    命令行参数的数目
  $*   所有命令行参数
#### 变量、数组变量
   $variable=one
   $echo $variable
   $array=(ma [3]=b [1]=cc)
   $echo ${array[0]}  ma
   $echo ${array[1]} cc
   $array_variable=(one two three)
   $echo ${array_variable[*]}
   $echo ${array_variable[@]}
   $array_variable[0]=one
   $array_variable[1]=two
   $array_variable[2]=three
   $echo ${array_variable[*]}
   $echo ${array_variable[@]}
   $echo ${#array_variable[0]} 返回第一个元素的长度
   $unset variable
  
#### 导出环境变量
   $export variable
#### test
   $string1=string2   如果两个字符串相同为真
   $string1!=string2     不等则为真
   $-b file    文件存在且是一个快特殊文件、则为真
   $-c file    文件存在且是一个字符特殊文件、则为真
   $-d file    文件存在且是一个目录、则为真
   $-e file      文件存在则为真
   $-f file    文件存在且是一个规则文件则为真
   $-g file       文件存在且设置了SGID位的值、则位真
   $-h file    文件存在且为一个符号连接、则位真
   $-k file    文件存在且设置了“sticky”位的值、则位真
   $-p file    文件存在且为一已命名管道、则位真
   $-r file    文件存在且可读、则位真
   $-s file    文件存在且大小大于零、则位真
   $-u file    文件存在且设置了SUID的值、则位真
   $-w file    文件存在且可写、则位真
   $-x file    文件存在且可读、则位真
   $-o file       文件存在且被有效用户ID所拥有、则位真
   $-z string   若string长度为零则为真
   $-n string   若string长度不为零则为真
   $expression1 -eq expression2    =
   $-ne         !=
   $-gt         >
   $-ge         >=
   $lt         <
   $le         =<
  
#### 特殊变量
   $0   执行命令的名字
   $n     执行命令所带参数对应
   $#   参数个数
   $*   列出说有参数
   $@   用于有引号的参数
   $?   前一个命令执行后的退出状态
   $$   当前shell的进程号
   $!   前一个后台命令的进程号
####  shift  移减参数
#### 文本过滤器
   $head
   $tail
   $grep
   $sort -rn -k 2,2 test.txt     ###对test的第二列进行排序安数字的大小顺序
   $uniq -u d c   -u只显示不重复的行;-d 只显示有重复数据行,只显示一行; -c 打印每一重复行出现的次数
   $join -o 1.1,2.2 test1.txt test2.txt  将test1的第一个域和test2的第二个域接一起
   $cut -d: -f3 test.txt    以:为分隔符抽取第三个域
   $cut -c 4-6 test.txt   抽取4-6之间的字符
   $cut -f 3 -d :  test.txt   抽取第3个字段,以:为分隔符
   $tr -s "[a-z]" < test.txt   去掉重复的字符
   $tr -s "[\012]" < test.txt  去掉空行
   $tr -s "[\n]" < test.txt
   $tr -s "[\32]" < test.txt
   $tr -s "[\r]" "[\n]" < test.txt  删除^M换成\n
   $tr -s "[\015]" "[\n]" < test.txt   同上
  
   exp:
    $tr -s "[\n]" < test.txt   删除空行
    $tr "[a-Z]" "[A-Z]" < test.txt  小写 -》大写
    $tr "[:lower:]" "[:upper:]" < test.txt  同上(字符类)
    $tr -cs "[a-z][A-Z]" "[\n*]" < test.txt  删除数字
    $tr -s "[:]" "[\t]"  将:替换成\t
#### sed and awk
   $sed /pattern/p 打印符合pattern的行
   $sed /pattern/d 删除符合pattern的行
   $sed 's/file/FIle/' 将file替换成FILE
   $sed 's/file/FILE/g' 全局替换
   $sed -n '2p' test.txt   打印文件第二行
   $sed -n '1,3p' test.txt  打印1至3行
   $sed -n '/file/'p test.txt  打印包含file的行
   $sed -e '/file/=' test.txt  打印包含file的行号
   $sed '/^$/d' file 删除空行
   $sed -n '4,/the/'p test.txt
   $sed '/company/ a\' test.txt
   $sed -n '2,/install/s/$/***xg***/p' file 从第二行倒包含install的行的末尾加上***xg***
   $sed '/anacon/r filetwo' file  从filetwo中读取匹配的行写入倒file中
   $sed '4 i\' test.txt
   $sed -n 's/\(inst\)all/\1xg/p' test.txt    模版inst被保存在1(tag1)中,all替换成xg,结果install替换成instxg
   sed 's/\t//g' 27397_report.txt  替换\t为''
  
  
   $awk -F:'command' inputf-ile  -F 指明input-file的分隔符为:默认为空格,$0 表示所有域
   $awk -f awk-script-file input-file(s)  -f调用awk脚步文件
   $awk '{printf "|The name is:%-10s ID is %8s|\n",$1,$4}' hold_file
   $awk '$7 > 4{print $1 > "subdatafile"}' datafile   awk 重定向
   $awk 'BEGIN{"date" | getline d; split(d,mon); print mon[2]}'
   $awk 'BEGIN{while ("ls" | getline) print}'
   $awk '{name[x++]=$2}; END{for(i=0; i < NR; i++)\
     print i,name[i]}' datafile     数组
  
   exp:
   $awk '{print $0}' test.txt > now 把结果输出到now中
   $awk '{print $0}' test.txt | tee -a now 把结果追加到now并输出结果
   $awk 'GEGIN{print "start file\n---------------"} {print $2} END{print "end file"}' test-file
   $awk '{if($4~/Brown/) print $0}' test.txt     打印包含Brown的记录($0 表示打印所有域; ~表示匹配正则表达式)
   $awk '$3=="test" {print $0}' test.txt      精确匹配
   $awk '$1 !~/Brown/ {print $0}' test.txt    打印不包含Brown的记录
   $awk '{if($1 !~/Bronwn/) print{print $0}}' test.txt  同上
  
   ####### awk 内部变量
        ARGC  命令行参数个数
        ARGV  命令行参数排列
        ENVIRON 支持队列中系统环境变量的使用
        FILENAME  awk浏览的文件名
        FNR  浏览文件的记录数
        FS   设置输入域分隔符,等价与命令行-F
        NF   浏览记录的域个数
        NR   已读记录数
        OFS  输出域分隔符
        ORS  数据记录分隔符
        RS   控制记录分隔符
        IGNORECASE  忽略大写写(仅在gwak中有效)
        SUBSEP  下标分隔符
       
        exp:
        $awk '{print NF,NR,$0} END{print FILENAME}' test.txt
        $awk 'BEGIN { FLAG="file" } { if( $1==FLAG ) print $0 }' test.file
        $awk '(count+=$2); END{print "the col count:"count}' test.file
        $awk '{(count+=$2)} END{print "the col count:"count}' test.file
        $ll | awk '/^[^d]/ {print $5,$9} {count+=$5} END{print "the file size-sum:"count}'
    ###### awk内置函数
        gsub(r,s)    整个$0中用s替代r
        gsub(r,s,t)   整个t中用s替代r
        index(s,t)   返回s中字符串t的第一位置
        length(s)    返回s长度
        math(s,r)    测试s是否包含匹配r的字符串
        split(s,a,fs)  在fs上将s分成序列a
        sprint(fmt,exp) 返回经fmt格式化后的exp
        sub(r,s)    用$0中最左边最长的字符串代替s
        substr(s,p)   返回字符串s中从p开始的后缀部分
        substr(s,p,n)  返回字符串s中从p开始长度位n的后缀部分
        exp:
        $awk 'gsub(/34/,3434) {print $0}' test.txt 将34替换成3434
        $awk 'BEGIN {str-length=index("testin","in"); pirnt "str-length:"str-length}'
        $awk 'BEGIN{print length("test")}'
        $awk 'BEGIN{print match(test,/t/)}'
        $awk 'BEGIN{print split("12#df#4545",array,"#")}'
        $awk 'END{start=match("good os name", /[a-z]a/); print start}' datafile  输出na的位置9
        $awk 'END{start=match("good os name", /[a-z]a/); print RSTART,RLENGTH}' datafile 输出na的位置9,和na的长度
        $awk 'BEGIN{print toupper("linux"), tolower("MAN")}'
        $awk 'BEGIN{now=strftime("%y-%m-%d"); print now}'
       $awk 'BEGIN{now=strftime("%Y-%m-%d"); print now}'
       $awk '{print(int(13/3))}' datafile 去掉小数,不舍入
       $awk '{print rand()}' datafile  产生0-1之间的随即数
#### 正则表达式
   $ /^$/  空行
   $ /^.*$/  整行
   $ / */  一个或多个空格
   $ /<[ ^>][^>]×>/ html或xml标记
   $ /[a-zA-Z][a-zA-Z]*:\/\/[a-zA-Z0-9][a-zA-Z0-9\.]*.*/  有效的URL
   $ \$[0-9]*\.[0-9][0-9]/     美元数量格式
  
#### 信号处理
   SIGHUP 1  检测控制终端的挂起或控制进程的死亡
   SIGINT 2  键盘中断
   SIGQUIT 3  从键盘退出
   SIGKILL 9  杀死信号
   SIGALRM 14 报警时钟信号
   SIGTERM 15  终止信号
  
  
#### shell 调试
  $/bin/sh -nv ./buggy1.sh   对buggy1.sh 进行语法检查
  
  
#### tee
   $who | tee who.out
   $who | tee -a who.out  追加到who.out后
  
#### 合并标准输入、输出
   $grep "test.*" > grep.out 2>&1

#### 正则表达式
   ^           行首
   $           行尾
   ^[the]         以the开头的行
   [Ss]igna[lL]     匹配Signal、signal、signaL、SignaL
   [Ss]ingal[lL]\.    同上,但加一句点
   [mayMAY]       包含may大写或小写字母的行
   ^USER$        只包含USER的行
   [tty]$        以tty结尾的行
   \.          带句点的行
   ^d..x..x..x      对用户、用户组及其他用户组成员可执行权限的目录
   ^[^l]         排除关联目录的目录列表
   [.*0]         0之前或之后加任意字符
   [000*]        000或更多
   [iI]         大写或小写i
   [^$]         空行
   [^.*$]        匹配行中任意字符
   ^......$       包含6个字符的行
   [a-zA-Z]       任意单个字符
   [^0-9\$]       非数字或美元标识
   
#### grep 的精确匹配
   $grep -i "test\>" -c   匹配含有test的行
   $grep -E '219|255' test.out  -E 使用扩展模式匹配 219或255
   $ls -l | grep  '^d'
   $ls -l | grep '^[^d]'
   $ls -l | grep '^d.....x..x'
   $grep -2 name file  打印包含name的上下各两行
   $grep -A 2 name file  打印包含name的下两行
   $grep -B 2 name file  打印包含name的上两行
   $grep -e '-name' file 去掉-的意义
   $grep -x -e '-name file' file 匹配整行且去掉-的意义
  
  
#### set 调试脚本
     set -n 读命令但并不执行
     set -v 显示读取的所有行
     set -x 显示所有命令及其参数
 
#### << 一些是用技巧
   $ cat >> myfile << NEWFILE    快速创建一个文件
   $ lpr << QUICKDOC       快速创建打印文档
 
# trap 在脚本中捕获信号
 $ trap "command" signals
# eval 扫描命令行进行置换
 # 显示传给脚步的最后一个变量
 $ #!/bin/sh
  #evalit
  echo "Total number of argumets passed is $#"
  echo "The process ID is $$"
  echo "Last argument is " $(eval echo \$$#)
# logger 命令
 $ logger -p notice "This is a test message. Please Ignore $LOGNAME"
 tail -f /var/log/messages

#### gcc的使用
$gcc -O2 -o hello hell.c       -O2 优化级别, -o 编译成可执行文件
$gcc -I /usr/local/include hello.c  -I 指定头文件的位置
$gcc -o hello hello.c /usr/lib/libm.a  指定库
$ar crv libfoo.a bill.o fred.o    归档函数库
$ranlib libfoo.a    为函数库建立一个内容表
$ldd hello      查看程序用的共享库
#### Linux 文件类型
 -   普通文件
 b  块专有设备文件
 c  字符专有设备文件
 d  目录
 l  连接
 n  网络专有文件
 p  命名管道专有文件,也称为FIFO
 s  套接字

####  Linux 下全新的问题
 r read   4
 w write   2
 x excute  1
#### 通配符
 *     匹配零个或多个字符
 ?     匹配任意单个字符
 [test]  匹配列表中的任意字符
 [1-9]   匹配范围中的字符
 {tt,pp}  匹配tt或pp
#### execute command
  $echo $(pwd)
  $echo `pwd
#### exec 替换当前进程而不需要启动一个新的进程
  $exec > temp  exec把当前shell的标准输出打开倒temp
  $ls
  $date
  $exec > /dev/tty  标准输出显示在屏幕上
#### 参数扩展
 ${param:-default}   如果param是空,就把它设置为default的值
 ${#param}       给出param的长度
 ${param%word}     从param的尾部开始删除匹配word的最小部分返回声誉部分
 ${param%%word}    从param的尾部开始删除匹配word的最大部分返回剩余部分
 ${param#word}     从param的头部开始删除匹配word的最小部分返回剩余部分
 ${param##word}    从param的头部开始删除匹配word的最大部分返回剩余部分
 

#### jobs
 $jobs -l  显示在后台运行的进程及pid
 $jobs %%  显示最近执行的命令
 $jobs -x echo %1  只显示进程的pid且是指定作业,1为作业号
 $fg %1    把指定的作业放到前台执行
 
 
#### 参数顺序
 $echo a b c
 $echo !$  返回命令的最后一个参数
 $echo !^   返回命令的第一个参数
 $echo !*  返回命令的全部参数
#### declare
 $declare -f   显示定义函数清单
 
 
 
#tar
1、*.tar 用 tar –xvf 解压
2、*.gz 用 gzip -d或者gunzip 解压
3、*.tar.gz和*.tgz 用 tar –xzf 解压
4、*.bz2 用 bzip2 -d或者用bunzip2 解压
5、*.tar.bz2用tar –xjf 解压
6、*.Z 用 uncompress 解压
7、*.tar.Z 用tar –xZf 解压
8、*.rar 用 unrar e解压
9、*.zip 用 unzip 解压
tar czvf test.tar.gz test/
tar xzvf test.tar.gz ./

#grep info
$free
$grep SwapTotal /proc/meminfo
$grep MemTotal /proc/meminfo
$grep "model name" /proc/cpuinfo
#operator system info 
lsb_release -a
 
 
awk -F"|" -v OFS='|'  '{print $1,$2,$3}' GM0205201106131500025958-33288-20110613150006.log|grep '301004'>user.log
nohup /usr/local/mine/message_receive_test/bin/start.sh 5 > stdout.log 2>&1 &
阅读(753) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~