Translate

Image of Advanced Programming in the UNIX Environment, Second Edition (Addison-Wesley Professional Computing Series)
Image of Operating System Concepts
Image of Linux Kernel Development (3rd Edition)
Image of RHCE Red Hat Certified Engineer Linux Study Guide (Exam RH302) (Certification Press)

Dynamically Updating Xterm Title using Ksh93

While it is easy to dynamically set your shell prompt and the title of your terminal window if you are using the bash shell, it is far more difficult to do so in the Korn Shell. Typically, as in Rich Lister’s How to change the title of an xterm, the offered solution is something like the following: HOST=`hostname` HOST=${HOST%%.*} PS1=’^[]0;${USER}@${HOST}: ${PWD##${HOME}/}^Gksh$ ‘ The problem with such a solution is that it sets the prompt and the xterm window title only once. If you wish to dynamically set the xterm title and the shell prompt, you must use a Korn shell

Korn Shell DEBUG Trap

The trap shell builtin is used to change the way signals are handled by the Korn Shell 93 (ksh93) shell. In addition, a trap may be set for three ksh93 pseudo-signals: EXIT, ERR, and DEBUG. In this post we demonstrate how to use the DEBUG pseudo-signal to trap changes in the value of a variable for debugging purposes. trap [ -p ] [ action ] [ sig ] . . . The -p option causes the trap action associated with each trap as specified by the arguments to be printed with appropriate quoting. Otherwise, action will be processed as if

Convert Integer to Float in Korn Shell

There is no explicit mechanism, such as float(integer), in the Korn Shell (ksh93) to convert an integer to a float which in the Korn Shell is by default a long double. By default, dividing integer 1 by 3 produces 0: $ integer x=1 $ echo $(( x/3 )) 0 $ So how to you get the result, 0.3333 …, which you probably were expecting? You can convert x to floating point and then do the division by replacing the 3 with either 3. or, as I normally do, with 3.0: $ integer x=1 $ echo $(( x/3. )) 0.333333333333333333 $

Implementing strstr in Korn Shell

Neither Bash or the Korn Shell 93 (ksh93) provide a C-like strstr builtin. Here is a simple implementation of a strstr function in ksh93. The strstr function finds the first occurrence of s2 in s1. If s2 is an empty string, 0 is returned; if s2 occurs nowhere in s1, 0 is also returned; otherwise the offset to the first character of the first occurrence of s2 is returned. #!/bin/ksh function strstr { typeset s1=”$1″ typeset s2=”$2″ if [[ ${#s2} == 0 ]] then return 0 fi typeset len=${#s1} typeset first=${s1%%${s2}*}x typeset ndx=${#first} if (( ndx > len )) then

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