Skip to main content
Copy edited.
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 109
  • 132

forFor a standard shell (without bashisms) using only builtins:

uppers=ABCDEFGHIJKLMNOPQRSTUVWXYZ  
lowers=abcdefghijklmnopqrstuvwxyz  

lc(){ #usage: lc "SOME STRING" -> "some string"
i=0    i=0
    while ([ $i -lt ${#1} ]) do 
  
       CUR=${1:$i:1}
        case $uppers in
            *$CUR*)CUR=${uppers%$CUR*};OUTPUT="${OUTPUT}${lowers:${#CUR}:1}";;
            *)OUTPUT="${OUTPUT}$CUR";;
        esac
        i=$((i+1))
    done
    echo "${OUTPUT}"
}

andAnd for upper case:

uc(){ #usage: uc "some string" -> "SOME STRING"
    i=0
    while ([ $i -lt ${#1} ]) do
        CUR=${1:$i:1}
        case $lowers in
            *$CUR*)CUR=${lowers%$CUR*};OUTPUT="${OUTPUT}${uppers:${#CUR}:1}";;
            *)OUTPUT="${OUTPUT}$CUR";;
        esac
        i=$((i+1))
    done
    echo "${OUTPUT}"
}

for a standard shell (without bashisms) using only builtins

uppers=ABCDEFGHIJKLMNOPQRSTUVWXYZ  
lowers=abcdefghijklmnopqrstuvwxyz  

lc(){ #usage: lc "SOME STRING" -> "some string"
i=0  
while ([ $i -lt ${#1} ]) do  
     CUR=${1:$i:1}
    case $uppers in
        *$CUR*)CUR=${uppers%$CUR*};OUTPUT="${OUTPUT}${lowers:${#CUR}:1}";;
        *)OUTPUT="${OUTPUT}$CUR";;
    esac
    i=$((i+1))
done
echo "${OUTPUT}"
}

and for upper case

uc(){ #usage: uc "some string" -> "SOME STRING"
i=0
while ([ $i -lt ${#1} ]) do
    CUR=${1:$i:1}
    case $lowers in
        *$CUR*)CUR=${lowers%$CUR*};OUTPUT="${OUTPUT}${uppers:${#CUR}:1}";;
        *)OUTPUT="${OUTPUT}$CUR";;
    esac
    i=$((i+1))
done
echo "${OUTPUT}"
}

For a standard shell (without bashisms) using only builtins:

uppers=ABCDEFGHIJKLMNOPQRSTUVWXYZ
lowers=abcdefghijklmnopqrstuvwxyz

lc(){ #usage: lc "SOME STRING" -> "some string"
    i=0
    while ([ $i -lt ${#1} ]) do 
        CUR=${1:$i:1}
        case $uppers in
            *$CUR*)CUR=${uppers%$CUR*};OUTPUT="${OUTPUT}${lowers:${#CUR}:1}";;
            *)OUTPUT="${OUTPUT}$CUR";;
        esac
        i=$((i+1))
    done
    echo "${OUTPUT}"
}

And for upper case:

uc(){ #usage: uc "some string" -> "SOME STRING"
    i=0
    while ([ $i -lt ${#1} ]) do
        CUR=${1:$i:1}
        case $lowers in
            *$CUR*)CUR=${lowers%$CUR*};OUTPUT="${OUTPUT}${uppers:${#CUR}:1}";;
            *)OUTPUT="${OUTPUT}$CUR";;
        esac
        i=$((i+1))
    done
    echo "${OUTPUT}"
}
Source Link
technosaurus
  • 7.7k
  • 1
  • 31
  • 53

for a standard shell (without bashisms) using only builtins

uppers=ABCDEFGHIJKLMNOPQRSTUVWXYZ  
lowers=abcdefghijklmnopqrstuvwxyz  

lc(){ #usage: lc "SOME STRING" -> "some string"
i=0  
while ([ $i -lt ${#1} ]) do  
    CUR=${1:$i:1}
    case $uppers in
        *$CUR*)CUR=${uppers%$CUR*};OUTPUT="${OUTPUT}${lowers:${#CUR}:1}";;
        *)OUTPUT="${OUTPUT}$CUR";;
    esac
    i=$((i+1))
done
echo "${OUTPUT}"
}

and for upper case

uc(){ #usage: uc "some string" -> "SOME STRING"
i=0
while ([ $i -lt ${#1} ]) do
    CUR=${1:$i:1}
    case $lowers in
        *$CUR*)CUR=${lowers%$CUR*};OUTPUT="${OUTPUT}${uppers:${#CUR}:1}";;
        *)OUTPUT="${OUTPUT}$CUR";;
    esac
    i=$((i+1))
done
echo "${OUTPUT}"
}