zoukankan      html  css  js  c++  java
  • shell中大写小转换

    用tr需要新增变量,用declare或typeset需要在变量赋值前或者赋值后单独声明,都有些麻烦

    此方法为bash 4.0以后新增,bash 4.0 2009年发布

    $ test="abcDEF"

    # 把变量中的第一个字符换成大写

    $ echo ${test^}
    AbcDEF

    # 把变量中的所有小写字母,全部替换为大写
    $ echo ${test^^}
    ABCDEF

    # 把变量中的第一个字符换成小写
    $ echo ${test,}
    abcDEF

    # 把变量中的所有大写字母,全部替换为小写
    $ echo ${test,,}
    abcdef


    https://stackoverflow.com/questions/2264428/how-to-convert-a-string-to-lower-case-in-bash

    The are various ways:

    POSIX standard

    tr

    $ echo "$a" | tr '[:upper:]' '[:lower:]'
    hi all
    

    AWK

    $ echo "$a" | awk '{print tolower($0)}'
    hi all
    

    Non-POSIX

    You may run into portability issues with the following examples:

    Bash 4.0

    $ echo "${a,,}"
    hi all
    

    sed

    $ echo "$a" | sed -e 's/(.*)/L1/'
    hi all
    # this also works:
    $ sed -e 's/(.*)/L1/' <<< "$a"
    hi all
    

    Perl

    $ echo "$a" | perl -ne 'print lc'
    hi all
    

    Bash

    lc(){
        case "$1" in
            [A-Z])
            n=$(printf "%d" "'$1")
            n=$((n+32))
            printf \$(printf "%o" "$n")
            ;;
            *)
            printf "%s" "$1"
            ;;
        esac
    }
    word="I Love Bash"
    for((i=0;i<${#word};i++))
    do
        ch="${word:$i:1}"
        lc "$ch"
    done
    
     
     
     
  • 相关阅读:
    学习笔记
    学习笔记
    web前端初步学习心得
    Redis学习 命令执行
    Redis笔记 info命令
    UNIX编程 GetAddrInfo笔记
    UNIX编程 TCP基础读写笔记
    日本語自然言語処理
    日本語助詞と助動詞
    UNIX编程 I/O多路转接笔记
  • 原文地址:https://www.cnblogs.com/byfboke/p/14272195.html
Copyright © 2011-2022 走看看