neo's profile十年一场梦 ..BlogListsGuestbookMore Tools Help

Blog


    10/21/2009

    shell 要点 粗略

    #! bin/bash
    指定解释器 shell的绝对路径
     
    (1)
    -普通文件
    d目录
    l符号链接
    b块设备
    c字符设备
    rwxrwxrwx 421,421,421   百,十,个
    chmod -R 664/temp/*   -R 连同子目录下的文件一起设置
    目录的权限与文件不同  r-在目录中可列出文件, w-在目录中创建文件 ,x-可搜索
    目录的权限将会覆盖文件的权限
    chmod u+s xxx      设置suid   但当改变一个文件的所有权时,suid也会被清除掉
    chown zhao  jia    //将文件jia的所有权由用户jia改变为zhao
    chgrp  zhaogroup jia   //改变组
    查看所属组  id
    umask 缺省权限
    $home/ .profile
    文件umask最大6,目录最大7 
    根据umask计算权限  umask002
    777-002 目录权限
    再去掉所有x 就是文件的权限
    硬连接--指向node
    软连接--指向path
    in  makefile xxx
    不可以给目录创建硬连接,同一文件系统,是源的一个copy
     
    (2)
    useradd -d /usr/sam -m sam
    useradd -s /bin/sh -g group -G admin,root gem
    usermod -d/usr/sa -m sam
    userdel xxx
    passwd sam
    passwd -d sam  //del
    passwd -l sam //lock锁定
    groupmod -g 103 -n group2 group3 //-n修改组名
    username:passwd:uid:gid:描述:/d:shell
    //给 zhao 用户执行ls的权限
    /etc/sudoers << zhao  sun=/usr/sbin/ls
     
    (3)
    find .(/)   -name  "xx"  -exec xxx {} \;
    -name "xx "
    -type f 类型为普通文件   d目录
    -mtime +5 5日以前文件
    -perm
    -size
    -exec
    -ok  提示执行
                            |xargs xxx
    find . -name "[A-Z]*"
    ....   -path "/etc/" -prune  //避开某个目录
           -nouser   //查找属主已删除的文件
           -mtime -n//几日之内的
                  + //
           -newer xx //比xx新的
           -size +500 -and -size -2000    n=0.5k
           -mount 在当前文件系统中找
     
    (4)
    crontab -e
    打开一个crontab文件 添加一个条目
    分 时 日 月 周 命令
    -来表示范围 如周1-5
    * all
    , or
    0,30 18-23 * * * //18:00-23:00每隔30分钟
    at -f xxx now +1minute
              15:00 may24
    xxx &  //后台执行
    2>&1 // 1标准输出 2错误输出 &与什么一样, 2和1一起作为标准输出到某个文件
    nohup xxx & //帐号退出后仍后台执行
    [ab] //a或b
     
    (5)
    echo "xx`tty`xx"  //可执行tty  ``外接命令
         "xxx \"xxx\""   //  \"
    cat -n xxx  //加行号
    cat /dev/null >xx
    xxx|tee -a xxx
    0 1 2 输入 输出 错误

    (6 7)
    exec ,
    source //当前shell执行,会改变原环境
    (,,)
    {,,} //当前shell执行,会改变原环境
    正则是个么模板
    *  //匹配前面0-n次
    +  //1-n
    ? //0,1
    {n}  //n次
    {n,} //至少n次
    {n,m}  //n-m
    ?接上述后面-〉非贪欲模式;找极少匹配
    . //任意除了/n
    (xx) //匹配获取
    (?:xx) //非获取
    (?=xx)  //正向非获取
    (?!xx)  //反
    [xyz]  //x or y or z
    \b 单词边界  zhao  o
    \d 数字 \D= !\d
    \s 空白符
    \w 数字 字母 _
     
    (8)
    grep -n "xx" file //显示匹配行及行号
         -c  //匹配行计数
         -i  //忽略大小写
         -v  //非
    精确匹配  grep "xx\>"
    grep "xx" file| grep "xx"  //多级匹配
    " " 字符串
    ' ' 正则
    [[:space:]]  //空格 tab
     
    (9)
    awk -Fxx 'xx' file //-F指令文件域分隔符 缺省为空格
    awk '{print $0}' file >file1  所有域
    awk 'BEGIN{动作1}{2}END{3}' file
    ~/xx/   //匹配正则表达式
    =="xx"  //为精确匹配  xx不再是正则 而是字符串
    内置变量
    NR //记录个数
    NF //域个数 $NF 最后一个域
    awk中修改任何域,只是修改的缓存里的内容,实际文件中的未被修改
    total+=$1 //累加
    gsub(a,b)  //用b替换a
    sub(a,b)   //....只替换第一次的
    index(s,t) //返回s中字符串t的第一个位置
    match(a,b) //检查a中是否包含b
    split(x,array,"-")  //将字符串x以-拆分到array数组中,返回个数
    substr(s,x,y)  按从x位置开始y长度返回子串
     
    (10)
    sed与awk类似,是对拷贝的操作
    sed 'xxx' file1 > file2
    '1,3' //利用行号~~第1-3行
    '1,$' //行首到行尾
    '/正则/'
    p  打印 '1,5p'  //打印1-5行
                        d  删除
    '4,/xx/'  //仅从第4行开始 去匹配正则
    sed "/xx/a\xxx" //将xxx插入到/xx/行后面,实际文件
             i\                        前
             c\            替换/xx/行
    sed 's/正则/替换/'
                                 g  //缺省下仅替换一次,g全局替换
                      '           w file'   //将替换行输出定向到一个文件
    将模式也当作替换的一部分
    sed 's/abc/xxx&/'  //相当于将abc换成xxxabc
    sed '/xx/r file2' file1   //将file2中的内容加到file1的xx后面
    sed '/xx/q' file  //首次匹配后退出
     
    (11)
    sort  -t: -u +0 +1.2 +3nr file   //-u删重复行
    uniq -u xxx
    join -j1 1 -j2 3 file1 file2
    split -10 file
    head(tail) -n 2 file
               -c 200k
    tail -f xx  //日志滚动读取
    tac xx //逆序
     
    (12)
    cat xx | tr "[a-z]" "[A-Z]"  //小写转大写
    tr -s "[\r]" "[\n]"   //用换行代替ctrl+m  dos->unix    -s删去重复的

    --1 what shell?
    没办法直接操作kernel,而是通过shell与kernel沟通的
    interface, command interpreter
    --2 shell prompt, cr
    shell prompt 提示符 $,#    ---你现在可以输入命令了
    cr  由enter输入产生           ---你可以执行这个命令了
    prompt与cr之间的是命令行
    --3 echo
    echo缺省情况下会 在结尾接一个\n
    但可以echo -n xx , 要求不打印\n
     echo -e  "xx"  启动转义\
    \b回退符
    echo $a    打印变量a
    --4 ' '   " "
    字符分为普通字符与元字符两种 , 元: $ = () {} && ....
    ' '   之中的所有元字符全都关闭
    " "  。。。部分...........关闭     如 $就不关闭   cr不关闭
    另外可用 \x  反斜杠将元关闭
    a=0
    awk '{print $' "$a"'}' file
    --5 var=value?export
    export xx    //设为环境变量 供之后使用
    unset xx   //将该变量去掉  ,注意并非除掉其值而是变量   
    这两个 都有继承性
    而简单的=没有继承性
    a=b
    b=c
    a不一定=c
    a=b
    export a
    实际上是变量b被提为环境变量了

    --6
    fork 在 subshell中执行   ./xxx
    source 在当前shell中执行   source ./myscript
    exec script在同一进程上执行,但原进程会被立即结束 exec ./xx
    --7
    () subshell
    {}    同shell执行
    --8 
    $()  命令替换
    ${} 变量替换
    去掉某些东西
    ${a#*x} 
    ${a##*x}
    ${a%x*}
    ${a%%x*}
    #左起 %右起 单个:仅一个  2个:皮配最多 x到那个符号止
    提取
    ${a:x:y}  取从x起y个字符
    替换
    ${a/x/y} 将第一个x换成y
    ${a//x/y}     所有
    附值
    b=${a=x}   
    b=${a:=x}
    a unset   a=x b=x
    对于: 如果a为null,则会有不同,a=x b=x    若无: a=null b=null
     
     $(())
    作整数运算
    --9
    $10  注意这不是第10个变量 而是 $1
    $#  取参数个数 不算$0的 
    $@
    $*  
    都指的是全部参数 除$0
    " " 下两者会有不同 "$@" 会得到各个参数 返回n个word
                                        "$*"    将各个参数整合为1个字段
    -10
    a&&b  a为true时才执行b
    a||b           假
    -11 >
    set -o noclobber 关闭>的覆盖功能 >时会提示
    echo "xx" >| file    //临时打开
    输出 和错误 优先级会高于输入 于是 
    cat <file >file   打印空,,file先被清掉了 

    -12  if, case
    if a; then
           b
    elif  c; then
           d
    else
           e
    fi
    a为真则执行b 否则判断c,为真则执行d,  若a c都假则执行e
    case "$a" in
       x1)
           xx
           ;;
       x2)
           ;;
    ...
       *)
          ...
          exit 1
    esac
    -13  for , while ,until
    for 变量 in x x x
    do
        ....
    done
    while [ ]; do
          ...
    done
    until [ ]; do   //与while相反, []为假才循环
         ..
    done

    other
    /sbin/sh  静态链接
    /bin/sh  -->/usr/bin/sh  动态链接  共享库函数
    shell 变量名长度256字符
     alias,builtin command,function,external command

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://sh-neo.spaces.live.com/blog/cns!1E3CA285E5F9E122!759.trak
    Weblogs that reference this entry
    • None