Translate

Archives

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)
Image of Beginning Google Maps API 3

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

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

Bash XOR A String

Here is an example of how to XOR a string variable using the Bash shell. Each character in the plaintext string is XOR’ed with decimal 90. #!/usr/local/bin/bash plaintext="abcdefg" echo "Plaintext: $plaintext" cyphertext="" for ((i=0; i < ${#plaintext}; i++ )) do ord=$(printf "%d" "’${plaintext:$i:1}") tmp=$(printf \$(printf ‘%03o’ $((ord ^ 90)) )) ciphertext="${ciphertext}${tmp}" done echo "Ciphertext: $ciphertext" # now XOR again and we should get the original string back plaintext="" for ((i=0; i < ${#ciphertext}; i++ )) do ord=$(printf "%d" "’${ciphertext:$i:1}") tmp=$(printf \$(printf ‘%03o’ $((ord ^ 90)) )) plaintext="${plaintext}${tmp}" done echo "Plaintext: $plaintext" The key lines are the ord= and the tmp=

Porting KSH93 to Windows 7 SUA

This post shows you how to download, build and install the latest version of the Korn shell in the Windows 7 SUA subsystem.