zoukankan      html  css  js  c++  java
  • linux命令技巧

    printf "%-5s %-10s %-4s " No Name Mark

    printf "%-5s %-10s %-4.2f " 1 Sarath 80.3456

    %-5s知名了一个格式为左对齐且宽度为5的字符串替换(-表示左对齐)


    pgrep -f wechat 查找有wechat的pid


    var="hello"

    echo $var和echo ${var} 一样

    获得变量值的长度:

    var='123456789012345!@#$'

    echo ${#var}    #19

    echo $0 显示用的什么shell

    -bash

    echo $SHELL 显示用的什么shell

    /bin/bash

     

     

    echo $UID 显示当前的用户id

    0

     


     

    使用函数添加环境变量

    prepend() { [ -d $2 ] && eval $1="$2${$1:+':'$$1}" && export $1 ;}

    使用方式:

    prepend PATH /opt/myapp/bin

     


     

    使用shell进行数学运算

    在 bash shell 中 ,使用 let (( )) 和 [ ] 执行基本的算术操作 ,而进行高级操作时 , expr 和 bc 这两个工具也会经常用到。

    1. #!/bin/bash  
    2. no1=4;  
    3. no2=5;  
    4. let result=no1+no2  
    5. echo $result  

     

     

    1. #!/bin/bash  
    2. no1=10;  
    3. let no1++  
    4. echo $no1  
    5. let no1--  
    6. echo $no1  

     

    简写:

    1. #!/bin/bash  
    2. no1=10  
    3. let no1+=5  
    4. echo $no1  
    5. let no1-=6  
    6. echo $no1  

     

     

     

    操作符 [ ] 的用法和 let 命令类似:

    #!/bin/bash
    no1=2
    no2=3
    result=$[ no1 + no2 ]
    echo $result
    #也可以使用 $前缀
    result=$[ $no1 + 5 ]
    echo $result

    expr:

    #!/bin/bash
    no1=3
    no2=4
    result=`expr $no1 + $no2`
    echo $result

    result=`expr 5 + 10`
    echo $result

    result=$(expr $no1 + 5)
    echo $result


    标准输出和标准错误

    cmd 2>stderr.txt 1>stdout.txt

    cmd 2>&1 output.txt   和   cmd &> output.txt 效果一样

    tee命令,得到标准输出并写入下一个文件


    普通数组和关联数组(hash数组)

    bash4.0版本之后才支持关联数组

    数组定义:

    array_var=(1 2 3 4 5 6)

    array_var[0]="test1"

    打印数组:
    echo ${array_var[0]}

    echo ${array_var[*]}      #打印所有数组成员

    echo ${array_var[@]}     #打印所有数组成员

    echo ${#array_var[*]}     #打印数组长度

    关联数组:略


    find

    find . -iname "example*"    #-iname忽略字母大小写

    find . ( -name "*.txt" -o name "*.pdf" ) -print        #-o  OR操作

    find /home/users -path "*/slynux/*" -print  #-path 用通配符匹配文件路径

    find . -regex ".*(.py|.sh)$"  #用正则找

    find . -iregex ".*(.py|.sh)$"  #让正则忽略大小写

    find . ! -name "*.txt" -print  #找出所有不以txt结尾的文件名

    find . -maxdepth 1 -name "f*" -print  #最大深度为1,也就是当前目录

    find . -mindepth 2 -name "f*" -print  #最小深度为2,从下一层目录找

    find . -type d -print  #找出所有目录,不包括文件

    find . -type f -print  #找出所有文件,不包括目录

    find . -type f -atime -7 -print   #7天内被访问过的恩所有文件

    find . -type f -atime 7 -print  #打印出恰好在7天前被访问过的所有文件

    find . -type f -atime +7 -print  #打印出访问时间超过7天的所有文件。

    -atime  -mtime  -ctime  #都是基于天

    -amin  -mmin  -cmin  #都是基于分钟

    find . -type f -amin +7 -print  #打印出访问时间超过7分钟的所有文件

    find . -type f -newer file.txt -print  #找出比file.txt修改时间更近的所有文件

    find . -type f -size +2k  #大于2kb的文件,b(512字节,块),c字节,w字(2字节),k(1024字节),M(1024k),G(1024M)

    find . -type f -size -2k  #小于2kb的文件

    find . -type f -size 2k  #大小等于2kb的文件

    find . -type f -name "*.swp" -delete  #删除swp文件

    find . -type f -perm 644 -print  #查找权限为644的文件

    find . -type f -name "*.php" ! -perm 644 -print  #找出服务器上所有的php文件并且权限不是644的。

    find . -type f -user alex -print  #找出所有alex拥有的文件


    分割文件

    split -b 10k data.file  #把文件切割成10k大小 data.file xaa xab xac xad

    split -b 10k data.file -d -a 4  #切割成4位数字补全的大小data.file x0009 x0019 x0029 x0039

    split -b 10k data.file -d -a 4 split_file  #data.file  split_file0000  split_file0001  split_file0002

    split -l 10 data.file  #10行一割


    使用环回文件

    dd if=/dev/zero of=big.img bs=1G count=1

    mkfs.ext4 big.img

    mkdir /mnt/loopback

    mount -o loop big.img /mnt/loopback


    sed

    sed 's/text/replace/' file >newfile 

    mv newfile file

    等价于 sed -i 's/text/replace/g' file

    sed -i 's/text/replace/3g' file  #从第三次找到的地方修改,改到最后

    echo this is an example | sed 's/w+/[&]/g'  #已匹配字符串标记使用&匹配找到的内容并加上[ ], [this] [is] [an] [example]

    echo this is digit 7 in a number | sed 's/digit ([0-9])/1/'  #this is 7 in a number      命令中 digit 7,被替换成了 7。样式匹配到的子串是 7,(..) 用于匹配子串,对于匹配到的第一个子串就标记为 1,依此类推匹配到的第二个结果就是 2,例如:
    echo aaa BBB | sed 's/([a-z]+) ([A-Z]+)/2 1/'     #BBB aaa


    awk

    awk 'BEGIN {pring "start"} pattern { commands} END { print "end"}' file

    工作流程:

    1.执行begin{commands}语句快

    2.从文件或stdin中读取一行,执行pattern{ commands }。重复这个过程,知道文件全部读取完。

    3.最后执行end{commands}语句块

    拼接字符串

    echo | awk '{var1="v1";var2="v2";var3="v3"; print var1"-"var2"-"var3;}'  #v1-v2-v3

    特殊变量:

    NR: 表示记录数量,在执行过程中对应于行号
    NF:表示字段数量,在执行过程中对应于当前行的字段数
    $0: 这个变量包含执行过程中当前行的文本内容
    $1: 第一个字段的文本内容
    $2: 第二个字段的文本内容

    echo -e "line1 f2 f3 line2 f4 f5 line3 f6 f7"| awk '{  print "Line no:"NR",No of fields:"NF, "$0="$0,"$1="$1,"$2="$2,"$3="$3  }' 

    结果:

    Line no:1,No of fields:3 $0=line1 f2 f3 $1=line1 $2=f2 $3=f3

    Line no:2,No of fields:3 $0=line2 f4 f5 $1=line2 $2=f4 $3=f5

    Line no:3,No of fields:3 $0=line3 f6 f7 $1=line3 $2=f6 $3=f7

     

    awk运算:

    seq 5 | awk 'BEGIN{ sum=0;print "Summation:"}{print $1"+";sum+=¥1}END{print "==";print sum}'

    结果:

    Summation:

    1+

    2+

    3+

    4+

    5+

    ==

    15

     

    例子:

    awk 'END{print FILENAME}' awk.txt 打印文件名

     

    例子:

    cat section.txt
    line with pattern1
    line with pattern2
    line with pattern3
    line end with pattern4
    line with pattern5


    awk */pa.*3/, /end/* section.txt
    输出结果是:
    line with patern3
    line end with pattern4

     

     

     -----------------------------------------------------------------------------------

    查看filename中含有abc所在行后4行内容

    cat filename | grep abc -A4

    查看filename中含有abc所在行前4行内容

    cat filename  | grep abc  -B4

    2种方法打印匹配行的前后5行

    grep -5 '关键字' 文件名
    grep -C 5 '关键字' 文件名

    ------------------------------------------------------------------


     svn碰到中文名目录问题:

    apt-get install locales-all

    locale     #显示现在的locale

    locale -a    #显示支持的locale

    locale-gen en_US.UTF-8    

    -------------------------------------------------------------------------------------------------------

    使用 curl获取本机ip

      1. curl ifconfig.me
      2.  
        curl icanhazip.com
      3.  
        curl curlmyip.com
      4.  
        curl ip.appspot.com
      5.  
        curl ipinfo.io/ip
      6.  
        curl ipecho.net/plain
      7.  
        curl www.trackip.net/i
  • 相关阅读:
    HTML DOM 12 表格排序
    HTML DOM 10 常用场景
    HTML DOM 10 插入节点
    HTML DOM 09 替换节点
    HTML DOM 08 删除节点
    HTML DOM 07 创建节点
    022 注释
    024 数字类型
    005 基于面向对象设计一个简单的游戏
    021 花式赋值
  • 原文地址:https://www.cnblogs.com/alexhjl/p/7207165.html
Copyright © 2011-2022 走看看