shell基础编程

Introduction

Shell编程从入门到放弃学习笔记。

Materials

Variable

  • $i

  • $*

  • $#

  • $?

  • Array

    A=(test1 test2 test3)
    echo ${A[0]}
    echo ${A[@]}
    echo ${#A[@]}
    
    ${A[@]/test2/test5}
    
    unset A[2]
    

Syntax

  • if

    if expression1; then
    	statement1
    elif expression2; then
    	statement2
    else
    	statement3
    fi
    
  • for

    for var in iterobj
    do
    	statement
    done
    
    for ((i=1; i<=10; i++))
    do
    	statement
    done
    
  • while

    while condition_statement
    do
    	statement
    done
    
  • until

    until condtion_statement
    do
    	statement
    done
    
  • case select

    
    	
    PS3="...?"
    select i in CenOS RedHat Ubuntu
    do
    	#statement
    	case $arg in 
    	pattern1)
    	statement1
    	;;
    	pattern2)
    	statement2
    	;;
    	*)
    	statement3
    	;;
    done
    
  • function

    function command()
    {
    	statements;	
    }
    
    command
    
  • awk

    cat test.txt |awk '{print $i}'
    cat test.txt |awk '{print $NF}'
    
    cat test.txt |awk -F: {print $i}
    
    df -h|grep "/"|awk '{print $5}'|sed 's/%//g'
    
  • grep

    grep -v "^$" test.txt
    
    grep "^10" test.txt
    grep "11$" test.txt
    
    grep [-E] "([0-9]{1, 3}\.){3}[0-9]{1,3}"
    
    egrep "11|wuguangke" test.txt
    
  • sed

    sed [-i] 's/192.10/192.168/g' test.txt
    sed 's/^/& /g' test.txt
    
    sed '/wuguangke/a test007' test.txt
    sed '/wuguangke/i test007' test.txt
    
    sed -n '/wuguangke/p' test.txt
    sed -n '1,5p' test.txt
    sed -n '1p;$p' test.txt
    
  • find

    find / -name "test.txt"
    find . -name "test.txt"
    find . -maxdepth 1 -name "test.txt"
    
    find . -maxdepth 1 -type f -name "*.txt"
    
    find . -maxdepth 1 -type f -name "*.txt" -mtime +30
    find . -maxdepth 1 -type f -name "*.txt" -mtime -1
    
    find . -maxdepth 1 -type f -name "*.txt" -mtime -5 -exec rm -rf {} \;
    find . -maxdepth 1 -type f -name "*.txt" -mtime -5 -exec cp {} /tmp/ \;
    find . -maxdepth 1 -type f -name "*.txt" -mtime -5 |xargs rm -rf {} \;
    
    find . -size +20M
    find . -maxdepth 1 -size +20M -type f
    find . -maxdepth 1 -size +20M -type f -exec mv {} /tmp/ \; 
    

Examples

  • get current directory
    CUR_DIR=$(cd $(dirname $0) && pwd)
    echo $CUR_DIR
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值