Translate

Archives

First Letter Capitalization in Bash and Korn Shell

Bash does not have a built-in facility to first letter capitalize a string but Bash 4.0 and later has the necessary tools to do so as shown by the following example: #!/bin/bash Firstcap() { lower=${1,,} echo ${lower^} } Firstcap lINUX >>> OUTPUT IS: Linux This function not only uppercases the first letter of the argument passed to it but also lowercases the remaining letters of the argument. Here is how to do the same thing in ksh93: #!/bin/ksh Firstcap() { typeset -u f f=${1:0:1} typeset -l r r=${1:1} echo “${f}${r}” } Firstcap lINUX >>> OUTPUT IS: Linux

Bash and Korn Shell Let Keyword

Recently a colleague of mine recommended using the let keyword in shell scripts to perform arithmetic evaluation. That prompted me to write this post about the let keyword. Here is what the bash manpage says about the let keyword: let arg [arg …] Each arg is an arithmetic expression to be evaluated. If the last arg evaluates to 0, let returns 1; 0 is returned otherwise. And here is a simple example of it’s use: $ a=2 $ b=5 $ let c=$a+$b $ echo $c 7 $ let c=$a + $b $ echo $c 2 As you can see from

Korn Shell Multidimensional Arrays

The bash shell only supports single dimension arrays. Korn Shell 93 (ksh93), on the other hand, supports multidimensional arrays although this feature is poorly documented. Here is a simple example which demonstrates how to create and use a multidimensional array: #!/bin/ksh93 for i in 1 2 3 do for j in 4 5 6 do for k in 7 8 9 do array[$i][$j][$k]=$(( i + j + k )) # echo ${array[$i][$j][$k]} done done done for i in 1 2 3 do echo ${array[$i][4][7]} done It outputs: 12 13 14 Note – multidimensional associative arrays are not supported.

Korn Shell Ordinal Number Conversion

It is useful to be able to convert to and from ASCII ordinal numbers in scripts. Here is one way of doing it in Korn shell scripts. To convert the character ‘F’ to it’s ASCII ordinal value: $ print $((‘F’)) 70 To display the ASCII character associated with the ordinal number 70: $ typeset -i8 c=70 $ printf “\${c#*#}n” F

Using Git to Build Latest Korn Shell Sources

Up until a couple of months ago, if you wanted to build ksh93 (Korn Shell) from the source, you had to download the latest source code tarball from AT&T Labs Research. Such tarballs were released several times a year. Now you can use git to clone the latest sources for ast-open which includes ksh93: $ git clone http://I.accept.www.opensource.org.licenses.eclipse:.@www2.research.att.com/sw/git/ast-open Cloning into ‘ast-open’… remote: Counting objects: 41554, done. remote: Compressing objects: 100% (5477/5477), done. remote: Total 41554 (delta 35246), reused 41554 (delta 35246) Receiving objects: 100% (41554/41554), 37.07 MiB | 934 KiB/s, done. Resolving deltas: 100% (35246/35246), done. $ To build ast-open: