number of tokens in bash variable

前端 未结 7 2186
挽巷
挽巷 2020-12-09 08:41

how can I know the number of tokens in a bash variable (whitespace-separated tokens) - or at least, wether it is one or there are more.

7条回答
  •  天涯浪人
    2020-12-09 08:47

    For a robust, portable sh solution, see @JoSo's functions using set -f.

    (Simple bash-only solution for answering (only) the "Is there at least 1 whitespace?" question; note: will also match leading and trailing whitespace, unlike the awk solution below:

     [[ $v =~ [[:space:]] ]] && echo "\$v has at least 1 whitespace char."
    

    )

    Here's a robust awk-based bash solution (less efficient due to invocation of an external utility, but probably won't matter in many real-world scenarios):

    # Functions - pass in a quoted variable reference as the only argument.
    # Takes advantage of `awk` splitting each input line into individual tokens by
    # whitespace; `NF` represents the number of tokens.
    # `-v RS=$'\3'` ensures that even multiline input is treated as a single input 
    # string.
    countTokens() { awk -v RS=$'\3' '{print NF}' <<<"$1"; }
    hasMultipleTokens() { awk -v RS=$'\3' '{if(NF>1) ec=0; else ec=1; exit ec}' <<<"$1"; }
    
    # Example: Note the use of glob `*` to demonstrate that it is not 
    # accidentally expanded.
    v='I am *'
    
    echo "\$v has $(countTokens "$v") token(s)."
    
    if hasMultipleTokens "$v"; then
      echo "\$v has multiple tokens."
    else
      echo "\$v has just 1 token."
    fi
    

提交回复
热议问题