天衣有缝

冠盖满京华,斯人独憔悴~
posts - 35, comments - 115, trackbacks - 0, articles - 0
   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

linux学习的一些记录(原创)

Posted on 2007-04-18 15:58 天衣有缝 阅读(1241) 评论(0)  编辑 收藏 引用 所属分类: linux shell/kernel

1.whereis which 的区别

  which: 只在PATH环境变量中寻找文件

  whereis: 在系统定义的目录中寻找

 

2.根据关键字查找man

  举例:apropos split  或者  man -k split

 

3.sed示例:

  sed -e s/root/toor/g /etc/passwd > ~/test.out         替换 /etc/passwd中的roottoor输出到~/test.out

  sed -e 's/root/toor/g; s/ftp/ptf/g' /etc/passwd       使用 -e 指定多个命令

  sed -e 's/root/toor/g' -e ' s/ftp/ptf/g' /etc/passwd  同上

  使用命令文件:

  /* test.sed 开始,不包含本行 */

  s/root/toor/g
  s/ftp/ptf/g

  /* test.sed 结束,不包含本行 */

  指令:sed -f test.sed /etc/passwd

 

4.awk示例:

  awk '{print $0}' /etc/passwd                          $0表示完整的输入记录

  awk -F":" '{print $1}' /etc/passwd                    打印第一列,以:为分隔符

  awk -F":" '{print "username: "$1 "\t\t\t user id: "$3}' /etc/passwd        格式化并打印

  使用命令文件:

  /* test.awk 开始,不包含本行 */

  BEGIN{
        FS=":"
  }
  {printf "username: "$1 "\t\t\t user id: "$3"\n"}
  END{
        printf "all done processing /etc/passwd\n"
  }

  /* test.awk 结束,不包含本行 */

  指令:awk -f test.awk /etc/passwd

 

 

5.shell脚本

  1)特殊变量:

      ?前一个命令输出状态

      0当前脚本名

      1~9参数

  2)范例1,使用if语句

    #!/bin/bash

 

    echo "guest the select color"
    read COLOR

 

    if [ $COLOR = "yellow" ] 
    then
            echo "you are correct"
    elif [ $COLOR = "blue" ]
    then
            echo "you are correct also"
    fi

 

  3)范例2,使用case语句

    #!/bin/bash

 

    case "$1" in
        start)
                echo "start......"
                ;; 
        stop)
                echo "stop......"
                ;; 
        status)
                echo "status......"
                ;; 
        *) 
                echo "usage: $0 {start | stop | status}"
                ;;  
    esac

 

  4)范例3,使用迭代流程

    #!/bin/bash

 

    echo "guest color: red, blue or orange\n"
    read COLOR

 

    while [ $COLOR != "orange" ]
    do
            echo "incorrect, try again"
            read COLOR
    done

 

    echo "correct"

 

  5)使用双引号进行命令替换

    lines="$(wc -l 3.sh)"

    echo $lines

 

  6)测试文件

    -d file        目录

    -e file        存在

    -r file        可读

    -w file        可写

    -x file        可执行


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理