scripts

My personal collection of scripts
git clone git://src.gearsix.net/scriptsscripts.zip
Log | Files | Refs | Atom | README | LICENSE

strlen.sh (raw) (778B)


   1 # DEPRECIATED: just use (str="string") "echo ${#str}" or "wc -c $str", etc
   2 
   3 #!/bin/sh
   4 # strlen (string length)
   5 # description: echo's the length of a string ($1)
   6 # e.g.$ strlen "ma di-"
   7 #
   8 # SEE: https://stackoverflow.com/questions/17368067/length-of-string-in-bash
   9 
  10 if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
  11 	echo "Usage: strlen STRING"
  12 	echo ""
  13 	echo "print the number of characters and bytes for each word in STRING"
  14 	exit
  15 fi
  16 
  17 strU8DiffLen () { 
  18     local bytlen oLang=$LANG oLcAll=$LC_ALL
  19     LANG=C LC_ALL=C
  20     bytlen=${#1}
  21     LANG=$oLang LC_ALL=$oLcAll
  22     return $(( bytlen - ${#1} ))
  23 }
  24 
  25 for string in $@; do
  26     strU8DiffLen "$string"
  27     printf "%-$((14+$?))s is %2d chars length and uses %2d bytes\n" \
  28         "'$string'" ${#string} $((${#string}+$?))
  29 done 
  30