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
    
     
     
     
  • 相关阅读:
    CF1264E Beautiful League 解题报告
    CF1411G No Game No Life 解题报告
    Data structure on Bitcoin
    bitcoin Cryptography
    弹性布局Flex的基本语法
    Linq操作list
    dt某字段赋值
    List 添加数据
    dt 转 json 转实体
    队列
  • 原文地址:https://www.cnblogs.com/byfboke/p/14272195.html
Copyright © 2011-2022 走看看