libStrings

Functions to process shell strings.

Overview

Functions to process and manipulate shell strings.

Contains the following:

  • ksl::strlen()

  • ksl::strlenR()

  • ksl::isEmpty()

  • ksl::isEmptyR()

  • ksl::startsWith()

  • ksl::endsWith()

  • ksl::trimRight()

  • ksl::trimLeft()

  • ksl::trimWhitespace()

  • ksl::contains()

  • ksl::toLower()

  • ksl::toUpper()

  • ksl::capitalize()

  • ksl::isAlphNum()

  • ksl::isAlpha()

  • ksl::# isAscii()

  • ksl::isBlank()

  • ksl::isCntrl()

  • ksl::isDigit()

  • ksl::isGraph()

  • ksl::isLower()

  • ksl::isInteger()

  • ksl::isPrint()

  • ksl::isPunct()

  • ksl::isSpace()

  • ksl::isUpper()

  • ksl::# isWord()

  • ksl::isXdigit()


Index

ksl::strlen

Returns the number of characters in string.

Passes string by value. See also strlenR() next.

Example

x=$(ksl::strlen "dinosaur")

Arguments

  • $1 (…): the string of interest

Exit codes

  • 0: in all cases

Output on stdout

  • the string length

    image1

ksl::strlenR

Returns the number of characters held in string variable.

Passes string by reference. See also strlen() previous.

Example

ANIMAL=dinosaur
x=$(ksl::strlenR ANIMAL)

Arguments

  • $1 (…): the variable name holding a string

Exit codes

  • 0: in all cases

Output on stdout

  • the string length

    image2

ksl::isEmpty

Returns true if string is empty, otherwise false.

Passes string by value. See also isEmptyR() next.

Example

ANIMAL=dinosaur
if ksl::isEmpty $ANIMAL; then echo "yes"; fi

Arguments

  • $1 (…): the string of interest

Exit codes

  • 0: if string is empty (true)

  • 1: if string is non-empty (false)

    image3

ksl::isEmptyR

Returns true if string variable holds a non-zero string.

Passes string by reference. See also isEmpty() previous.

Example

ANIMAL=dinosaur
if ksl::isEmptyR ANIMAL; then echo "yes"; fi

Arguments

  • $1 (…): the variable name holding a string

Exit codes

  • 0: if string variable is empty (true)

  • 1: if string variable is non-empty (false)

    image4

ksl::startsWith

Returns true if $1 string starts with $2 string.

Example

ANIMAL="old dinosaur"
if ksl::startsWith $ANIMAL "old"; then echo "yes"; fi

Arguments

  • $1 (…): the major string to test

  • $2 (…): the minor string to look for

Exit codes

  • 0: the major string starts with the minor string (true)

  • 1: the major string does not start with the minor string (false)

    image5

ksl::endsWith

Returns true if $1 string ends with $2 string.

Example

ANIMAL="old dinosaur"
if ksl::endsWith $ANIMAL "dinosaur"; then echo "yes"; fi

Arguments

  • $1 (…): the major string to test

  • $2 (…): the minor string to look for

Exit codes

  • 0: the major string ends with the minor string (true)

  • 1: the major string does not end with the minor string (false)

    image6

ksl::contains

Returns true if $1 string contains the string in $2.

Example

ANIMAL="old dinosaur"
if ksl::contains $ANIMAL "dinosaur"; then echo "yes"; fi

Arguments

  • $1 (…): the major string to test

  • $2 (…): the minor string to look for

Exit codes

  • 0: the major string contains the minor string (true)

  • 1: the major string does not contain the minor string (false)

    image7

ksl::trimLeft

Returns a copy of $1 with the matching string in $2 removed from the front of $1.

If $2 is not given then defaults to removing whitespace. $2 argument is a prefix.

Example

ANIMAL="old dinosaur"
echo ksl::trimLeft $ANIMAL "old "
outputs: "dinosaur"

Arguments

  • $1 (…): the major string to operate on

  • $2 (…): the minor string to look for

Exit codes

  • 0: in all cases

Output on stdout

  • the resulting string

    image8

ksl::trimRight

Returns a copy of $1 with the matching string in $2 removed from the end of $1.

If $2 is not given then defaults to removing whitespace. $2 argument is a prefix.

Example

ANIMAL="old dinosaur"
echo ksl::trimRight $ANIMAL "dinosaur"
outputs: "old "

Arguments

  • $1 (…): the major string to operate on

  • $2 (…): the minor string to look for

Exit codes

  • 0: in all cases

Output on stdout

  • the resulting string

    image9

ksl::trimWhitespace

Returns a copy of $1 with the whitspace removed from both the start and end of $1.

Example

ANIMAL="  old dinosaur\t"
echo ksl::trimWhitespace $ANIMAL
outputs: "old dinosaur"

Arguments

  • $1 (…): the string to operate on

Exit codes

  • 0: in all cases

Output on stdout

  • the resulting string

    image10

ksl::toLower

Returns a copy of $1 string converted to lower case.

Example

ANIMAL="Old Dinosaur"
echo ksl::toLower $ANIMAL
outputs: "old dinosaur"

Arguments

  • $1 (…): the string to operate on

Exit codes

  • 0: in all cases

Output on stdout

  • the resulting string

    image11

ksl::toUpper

Returns a copy of $1 string converted to upper case.

Example

ANIMAL="Old Dinosaur"
echo ksl::toUpper $ANIMAL
outputs: "OLD DINOSAUR"

Arguments

  • $1 (…): the string to operate on

Exit codes

  • 0: in all cases

Output on stdout

  • the resulting string

    image12

ksl::capitalize

Returns a copy of $1 string with first character capitalized and the rest left alone.

Example

ANIMAL="old dinosaur"
echo ksl::capitalize $ANIMAL
outputs: "Old dinosaur"

Arguments

  • $1 (…): the string to operate on

Exit codes

  • 0: in all cases

Output on stdout

  • the resulting string

    image13

ksl::isAlphNum

Returns true if all characters in $1 are alphanumeric as defined by the POSIX standard.

Example

ANIMAL="dinosaur"
if ksl::isAlphaNum $ANIMAL; then echo "yes"; fi

Arguments

  • $1 (…): the string to operate on

Exit codes

  • 0: the string contains only alpha numeric characters (true)

  • 1: the string contains more than just alpha numeric characters (false)

    image14

ksl::isAlpha

Returns true if all characters in $1 are alpha as defined by the POSIX standard.

Example

ANIMAL="dinosaur"
if ksl::isAlpha $ANIMAL; then echo "yes"; fi

Arguments

  • $1 (…): the string to operate on

Exit codes

  • 0: the string contains only alpha characters (true)

  • 1: the string contains more than just alpha characters (false)

    image15

ksl::isAscii

Returns true if all characters in $1 are ASCII characters as defined by the POSIX standard.

ASCII is not currently working. Need to investigate.

Example

ANIMAL="dinosaur"
if ksl::isAscii $ANIMAL; then echo "yes"; fi

Arguments

  • $1 (…): the string to operate on

Exit codes

  • 0: the string contains only ASCII characters (true)

  • 1: the string contains more than just ASCII characters (false)

    image16

ksl::isBlank

Returns true if all characters in $1 are blank as defined by the POSIX standard.

Example

ANIMAL="dinosaur"
if ksl::isBlank $ANIMAL; then echo "yes"; fi

Arguments

  • $1 (…): the string to operate on

Exit codes

  • 0: the string contains only blank characters (true)

  • 1: the string contains more than just blank characters (false)

    image17

ksl::isCntrl

Returns true if all characters in $1 are control characters as defined by the POSIX standard.

Example

ANIMAL="dinosaur"
if ksl::isCntrl $ANIMAL; then echo "yes"; fi

Arguments

  • $1 (…): the string to operate on

Exit codes

  • 0: the string contains only control characters (true)

  • 1: the string contains more than just control characters (false)

    image18

ksl::isPrint

Returns true if $1 contains only characters that are printable as defined by POSIX standard.

Example

ANIMAL="dinosaur"
if ksl::isPrint $ANIMAL; then echo "yes"; fi

Arguments

  • $1 (…): the string to operate on

Exit codes

  • 0: the string contains only printable characters (true)

  • 1: the string contains more than just printable characters (false)

    image19

ksl::isGraph

Returns true if all characters in $1 are in the graph class (displayable on a screen) as defined by the POSIX standard.

Example

ANIMAL="dinosaur"
if ksl::isGraph $ANIMAL; then echo "yes"; fi

Arguments

  • $1 (…): the string to operate on

Exit codes

  • 0: the string contains only digit characters (true)

  • 1: the string contains more than just digit characters (false)

    image20

ksl::isLower

Returns true if all characters in $1 are lower case as defined by the POSIX standard.

Example

ANIMAL="dinosaur"
if ksl::isLower $ANIMAL; then echo "yes"; fi

Arguments

  • $1 (…): the string to operate on

Exit codes

  • 0: the string contains only lowercase characters (true)

  • 1: the string contains more than just lowercase characters (false)

    image21

ksl::isUpper

Returns true if all characters in $1 are upper case as defined by the POSIX standard.

Example

ANIMAL="dinosaur"
if ksl::isUpper $ANIMAL; then echo "yes"; fi

Arguments

  • $1 (…): the string to operate on

Exit codes

  • 0: the string contains only upper case characters (true)

  • 1: the string contains more than just upper case characters (false)

    image22

ksl::isPunct

Returns true if $1 contains only characters that are punctuations as defined by the POSIX standard.

Example

ANIMAL="dinosaur"
if ksl::isPunct $ANIMAL; then echo "yes"; fi

Arguments

  • $1 (…): the string to operate on

Exit codes

  • 0: the string contains only punctuation characters (true)

  • 1: the string contains more than just punctuation characters (false)

    image23

ksl::isSpace

Returns true if $1 contains only characters that are spaces as defined by the POSIX standard.

Example

ANIMAL="dinosaur"
if ksl::isSpace $ANIMAL; then echo "yes"; fi

Arguments

  • $1 (…): the string to operate on

Exit codes

  • 0: the string contains only space characters (true)

  • 1: the string contains more than just space characters (false)

    image24

ksl::isWord

Returns true if all characters in $1 are considered a word - containing only letters, digits, and the character _.

isWord is not currently working. Need to investigate.

Example

ANIMAL="dinosaur"
if ksl::isWord $ANIMAL; then echo "yes"; fi

Arguments

  • $1 (…): the string to operate on

Exit codes

  • 0: the string contains a valid word (true)

  • 1: the string contains more than a word (false)

    image25

ksl::isDigit

Returns true if all characters in $1 are digits as defined by the POSIX standard.

Note that “+” “-” “.” are not digits.

Example

ANIMAL="dinosaur"
if ksl::isDigit $ANIMAL; then echo "yes"; fi

Arguments

  • $1 (…): the string to operate on

Exit codes

  • 0: the string contains only digit characters (true)

  • 1: the string contains more than just digit characters (false)

    image26

ksl::isInteger

Returns true if $1 forms a valid integer meaning all digits with an optional preceding +/-.

Does not check for length.

Example

ANIMAL="dinosaur"
if ksl::isInteger $ANIMAL; then echo "yes"; fi

Arguments

  • $1 (…): the string to operate on

Exit codes

  • 0: the string contains a valid integer (true)

  • 1: the string contains more than just an integer (false)

    image27

ksl::isNumber

Returns true if $1 forms a valid number.

Example

ANIMAL="dinosaur"
if ksl::isNumber $ANIMAL; then echo "yes"; fi

Arguments

  • $1 (…): the string to operate on

Exit codes

  • 0: the string contains a valid number (true)

  • 1: the string contains more than just a number (false)

    image28

ksl::isFloat

Returns true if $1 forms a valid floating point number.

Example

ANIMAL="dinosaur"
if ksl::isFloat $ANIMAL; then echo "yes"; fi

Arguments

  • $1 (…): the string to operate on

Exit codes

  • 0: the string contains a valid floating point number (true)

  • 1: the string contains more than just a floating point number (false)

    image29

ksl::isXdigit

Returns true if all characters in $1 are valid hexadecimal digits.

Example

ANIMAL="dinosaur"
if ksl::isXdigit $ANIMAL; then echo "yes"; fi

Arguments

  • $1 (…): the string to operate on

Exit codes

  • 0: the string contains only hexadecimal characters (true)

  • 1: the string contains more than just hexadecimal characters (false)

    image30