bash

来源:互联网 发布:mysql 5.17 ngram 编辑:程序博客网 时间:2024/06/14 22:48
printf "%-5s %-10s %s" no name mark  格式化字符串cmd1 ; cmd2 相当于$: cmd1$: cmd2pgrep gedit 得到gedit的进程idcat /proc/pid/environ 得到pid的运行时的环境变量var="value"echo $var<PATH> 环境变量echo $PATH 显示PATH="$PATH:/xxx/xxx" 增加环境变量echo $PATH | tr ':' '\n' 换行显示 tr替换var=384787487287323echo ${#var}显示<字符串长度>echo "4*2" | <bc> 显示8 表示数值运算result=`echo "4*2" | bc`result等于8echo "scale=2;3/8" |bc 输出两位小数echo "xxx" >  temp.txt 会清空temp.txtecho "xxx" >> temp.txt 追加在temp.txt中> /dev/null 即没有输出命令运行成功后会返回0,不成功则非0,<退出状态>可以从$?获得cmd < file将文件重定向到命令数组和关联数组\cmd 对别名进行转义,即用本身的含义<date>:     date --date 用于提供日期串作为输入     date --date "Jan 20 2001" +%A     Saturday     date +%a 星期几            b 月份            y 年            D 标准格式 10/28/16            I 小时            M 分钟            S 秒            s unix纪元(秒)bash -x script.sh <调试>脚本#!/bin/bash -xv 增加调试功能<fun>(){ segment;}   fun x xx $1 第一个参数$@ 列表形式打印所有参数echo "$@"递归函数  <fork>炸弹:                   :(){ :|:&  };:<read> :      read -n 2 var 从输入中读取2个字符给var      read -s var   不会显读取密码      read -p "please input ..." var 提示信息      read -t 2 var 在两秒内输入给var      read -d ":" var 以:结束换行<IFS> (Internal Field Separator) 内部字段分隔符IFS默认为空白字符$IFS   IFS=,data="1,3,4,5"for item in $data; do echo $item; done打印出  1        2        3        4<循环>:    for i in items;    do        cmd;    doneecho {1..30} 生成一个从1到30的数字列表echo {a..z}  echo{z..a}for((i=0;i<10;i++)){    cmd;}   C中的循环<while> conditiondo cmd;done<let> x++; [$x -eq 9]<if> conditons;   thencmds;fiif condition;thencmds;elif condition;then   cmds;else   cmds;   fi[condition] && action; 如果条件为真,执行action[condition] || action; 如果条件为假,执行action[$var -eq 0] -gt 大于  -lt 小于-ge 大于或等于-le 小于或等于对文件的测试:                   -f  路径或文件名                -x 可执行                -d 目录                -c 字符设备文件                -b 块设备文件                -w 可写                -r 可读                -L 符号链接[-e $var] 字符串比较:            [[ $str1 == $str2 ]]           [[ $str1 != $str2 ]]           [[ $str1 > $str2 ]]           [[ -z $str ]]  空字符串  -n 非空cat :     cat file1 file2 file3 连接文件进行输出,文件本身并不改变     cat -s file 压缩连续的空白行     cat -n file 为每一行加上行号录制回访终端会话:            script -t 2> test.time -a test.txt            以exit结束            scriptreplay test.time test.txtfind :       沿着文件层次结构向下遍历,匹配符合条件的文件,并执行相应的操作       find   find .   find /       find -iname "*.txt"          find  . \( -name "*.txt" -o -name "*.pdf" \) -print       find -regex (-iregex) ".*\(\.py\|\.sh\)$" 匹配*.py or *.sh       \为转义       find -type d 目录                  f 文件                  l 符号链接                  s 套接字linux 下文件的3种时间戳:     访问时间(用户最后一次访问) -atime     修改时间(文件内容最后一次被修改) -mtime     变化时间(文件元数据,例如,权限或所有权最后一次被修改)-ctime     find . -type f -atime -7      打印在最近七天内被访问过的文件     find . -type f -mtime 7     打印恰好在七天前被访问的文件     find . -type f -atime +7     打印访问时间超过七天的文件          find -type f -newer file.txt -print     找出比file.txt修改时间更长的所有文件         find -type f -size +2k(-2k 2k)     大于2kb的文件     find -type f -name "*.swp" -delete     删除当前目录下的所有swp文件     find -type f -perm 644 -print     打印出权限为644的文件     find -type f -user lveg -print     打印出lveg所拥有的文件     find -type f -user root -exec chown lveg {} \;     将权限为root的文件改为lveg     find -type f -name "*.c" -exac cat {} \;>all.txt     将所有C文件拼接在一起  {}表示找到的文件     find -type f -mtime +10 -name "*.txt" -exec cp {} old \;     将10天前的文本文件拷贝到old目录下     -exec ./cmds.sh {} \;     执行多个命令0---stdin 1---stdout2---stderrxargs 为命令提供不同寻常的参数tr转换:       echo "HELLO WORLD" | tr 'A-Z' 'a-z' 大写换小写       cat text | tr '\t' ''       echo "hello 133" | tr -d '0-9' 删除数字       tr -s '' 压缩空白字符       eg:           cat sum.txt           1           2           3           4           5           cat sum.txt | echo $[ $ (tr '\n' '+') 0 ]   结果为15alnum:字母和数字alpha:字母cntrl:控制字符digit:数字lower:upper:print:space:punct:标点符号xdigit:16进制                     tr '[:lower:]' '[:upper:]'校验和:        md5sum       md5sum file.txt > file.md5       md5sum -c file.md5$$ 为当前脚本或终端的进程idtemp_file="/tmp/var.$$" 临时文件命令$RANDOM返回一个随机数split 分割文件根据扩展名切分文件名:                       ${var%.*} 删除%之后的通配符 ,从右往左,得到名称,非贪婪的,贪婪的为%%                     ${var#*.} 删除#之后的通配符,从左往右,得到后缀名,非贪婪,贪婪的为##                     file="sample.jpg"                     ${file%.*}=sample                     ${file#*.}=jpg重命名.jpg .png文件:#!/bin/bashcount=1;for img in *.jpg *.png    do        new=image-$count.${img##*.}        mv "$img" "$new" 2> /dev/null        if [ $? -eq 0 ];        then            echo "renaming $img to $new"            let count++        fi    donehead filehead -n 4 filehead -n -N file 除最后几行的所有行tail filetail -n 4 filetail -f file 密切关注此文件pid=$(pidof vim) 若运行了vim ,则得到vim的id列出目录:            ls -d */            ll | grep "^d"在目录间切换:             pushd /etc/ppp 将此目录加入目录站             dirs 查看目录站             pushd +n 从左到右依次为0,1             popd 弹出第0个,将目录改为第一个     只有两个目录切换时,用cd -wc (word count)wc -l 统计行数wc -w 统计单词数wc -c 统计字符数

0 0