• Shell实例:字符串操作 逻辑判断


    #!/bin/bash
    echo "enter a number"

    #read input value
    read ans

    #case
    case $ans in
            1)
            echo "your number is $ans"
            ;;
            2)
            echo "your number is 2"
            ;;
            [3-9])
            echo "your number is $ans"
    esac


    echo "Parameter numbers:$#"
    echo "Shell name:$0"
    echo "Shell all parameters:$*"
    echo "Shell PID:$$"
    echo "Shell PPID:$!"
    echo "Before command return value:$?"

    #echo "get command return:$(ls /home/provision)"

    #if : -eq(==) -ne(!=) -lt(<) -gt(>) -ge(>=) -le(<=)
    if [ "22" -lt "33" ]
    then
            echo "22 less than 33"
    else
            echo "22 not less than 33"
    fi

    #while
    #no space left or right =
    num=1
    echo "init num = $num"
    #need space after [ ,before ]
    while [ $num -le 10 ]
    do
            echo "num=$num"
            let num=num+1

    done


    #file exist
    fileName="case_test.sh"
    if [ -e $fileName ]
    then
            echo "$fileName is exist:"
    else
            echo "$fileName is not exist:"
    fi

    #is directory
    if [ -d $fileName ]
    then
            echo "$fileName is directory"
    else
            echo "$fileName is not directory"
    fi

    if [ -z $fileName ]
    then
            echo "fileName length is 0"
    else
            echo "fileName length is not 0"
    fi

    if [ -n $fileName ]
    then
            echo "fileName length is not 0"
    else
            echo "fileName length is 0"
    fi

    #
    for filename in `ls`
    do
            cat $filename
    done

    for((i=0; i<10; i++))
    do
            echo "--$i"
    done

    echo "Name:${name:-huangxiaobing}"

    #function return only number cannot return string
    getName(){
        return 100
    }

    getName
    echo "getName:$?"


    #connect two string
    firstname="huang"
    lastname="xiaobing"
    allname=`printf "%s%s" "$firstname" "$lastname"`
    echo "All Name:$allname"

    #get the char count of string
    countChar=`echo "$firstname"|wc -m`
    echo "$firstname count:$countChar"

    #return nor number char: return null
    echo "12345"|sed 's/[0-9]//g'
    #return A
    echo "123A45"|sed 's/[0-9]//g'
    #return huang
    echo ${allname%%xiaobing}
    echo ${allname:0:5}
    echo ${allname:2:7}

  • 相关阅读:
    用户价值和RFM模型
    产品生命周期(Product Life Circle,PLC)
    金字塔原理(Pyramid Principle)
    docker 技术
    网易实战+scrapy-redis配置
    uiautomator工具使用(7)
    adb命令行工具(6)
    Android 开发工具安装(5)
    appium 移动端自动化测试工具(4)
    mitmdump 详解(3)
  • 原文地址:https://www.cnblogs.com/zhangfei/p/1906471.html
Copyright © 2020-2023  润新知