String Functions
(NOTE: Some of the techniques shown or described on this page--marked in purple--require new features in the latest official PIKT 1.19.0 release (pikt-current.tar.gz) that are unavailable in any previous version.)
Pikt offers a wide variety of built-in functions, both string and numerical. An unusual feature of Pikt functions is that they are data-typed: their return value is signified by either the "$" (for string) or "#" (for number) prefix. (Functions never return a preceding or previous value, so no functions are of the type () or %foo().) (Note: Whitespace between the function name and the opening left parenthesis is illegal.)
Note that many of the functions have synonymous names, in some cases abbreviations, in other cases names by which they are known in other popular programming languages.
In the function descriptions below, "s" signifies any string expression; "i" any integer expression; "n" any numerical expression; "d" any datevalue; "t" any timevalue; "F" any filehandle; and "x" any expression that evaluates to either TRUE (non-zero) or FALSE (zero).
Where inappropriate in any of the following functions, a non-integer numerical argument will have the function return the string "<
For any of the hyperlinked functions, follow the links to view detail pages, with additional explanation and examples.
The string functions (functions that return a character string) include:
$alarm() $script() returns the name of the currently executing alarm script $alert() returns the current scripts's alert name $ampm() returns "AM" if the current time falls between midnight and twelve noon, else "PM" if between noon and midnight $ampm(d) returns "AM" if datevalue d's time falls between midnight and twelve noon, else "PM" if between noon and midnight $basename(s) for the path s, strips off the directory portion and returns the terminating filename $char(i) returns the char associated with the ASCII value i $checksum(-3,s) returns the output of the system cksum command for file s $checksum(-2,s) returns the output of the system sum command for file s $checksum(-1,s) returns the output of the system 'sum -r' command for file s $checksum( 0,s) returns the null checksum (0), also the number of bytes, for file s $checksum( 1,s) returns the BSD-style sum computed internally, also the number of 512-byte blocks, for file s $checksum( 2,s) returns the SysV-style sum computed internally, also the number of 512-byte blocks, for file s $checksum( 3,s) returns the POSIX cksum computed internally, also the number of bytes, for file s $checksum( 4,s) returns the MD4 checksum, also the number of bytes, for file s $checksum( 5,s) returns the MD5 checksum, also the number of bytes, for file s $chop(s,i) returns string s with i chars chopped off the end $chop(s) returns string s minus its last char $command(s) executes the command string s, returning its first line of output only; (if more than one output line is needed, uses #popen() and #read() instead) $dayname() for the current day, returns the day name ("Sunday", "Monday", ...) $dayname(i) for the given day number i (1-7), returns the day name ("Sunday", "Monday", ...) $dirname(s) for the path s, strips off the terminating filename and returns the directory portion $dquote() returns the double quote (") char $err() returns the error condition string "<Refer to the sample alarms.cfg for examples.>" $filemode(s) for path s, returns its mode in octal format $fixed(n) $str(n) $string(n) $text(n) returns #trunc(n) as a string with no decimal places $fixed(n,i) $str(n,i) $string(n,i) $text(n,i) returns n as as string with i decimal places $grname(s) $grname(i) $groupname(s) $groupname(i) for group name s, or gid i, returns the corresponding group name (as recorded in the system group file) $grpassword(s) $grpassword(i) $grouppassword(s) $grouppassword(i) for group name s, or gid i, returns the corresponding group password (as recorded in the system group file) $grmem(s) $grmem(i) $groupmem(s) $groupmem(i) $grmembers(s) $grmembers(i) $groupmembers(s) $groupmembers(i) for group name s, or gid i, returns the corresponding group members (as recorded in the system group file) $hostname() returns the system hostname $if(x,s1,s2) returns s1, if x is TRUE; else s2, if x is FALSE $if(x,s) returns s, if x is TRUE; else the string "< >", if x is false $inlin() $inline() $inputline() returns the current input line (without the trailing line-feed; also available as the data values $inlin, $inline, and $inputline) $left(s,i) returns the i left-most chars in s $level() returns the current script's severity level (EMERG, ALERT, CRIT, ERR, WARNING, NOTICE, INFO, DEBUG) $lower(s) returns s all in lower case $lpcmd() returns the current alert's lpcmd $ltrim(s) returns s stripped of any leading (leftmost) whitespace $mailcmd() returns the current alert's mailcmd $mid() $middle() [see $substring()] $monthname() for the current month, returns the month name ("January", "February", ...) $monthname(i) for month number i (1-12), returns the month name ("January", "February", ...) $newline() returns the newline ('\n') char $nil() returns the representative null string "< >" $passwdname() [see $pwname()] $passwdpassword() [see $pwpassword()] $passwdgecos() $passwdcomment() [see $pwgecos()] $passwddir() [see $pwdir()] $passwdshell() [see $pwshell()] $proper(s) returns s with every word capitalized $pwname(s) $pwname(i) $passwdname(s) $passwdname(i) for user name s, or uid i, returns the corresponding user name (as recorded in the system passwd file) $pwpassword(s) $pwpassword(i) $passwdpassword(s) $passwdpassword(i) for user name s, or uid i, returns the corresponding password (as recorded in the system passwd file) $pwgecos(s) $pwgecos(i) $passwdgecos(s) $passwdgecos(i) $pwcomment(s) $pwcomment(i) $passwdcomment(s) $passwdcomment(i) for user name s, or uid i, returns the corresponding gecos/comment (as recorded in the system passwd file) $pwdir(s) $pwdir(i) $passwddir(s) $passwddir(i) for user name s, or uid i, returns the corresponding home directory (as recorded in the system passwd file) $pwshell(s) $pwshell(i) $passwdshell(s) $passwdshell(i) for user name s, or uid i, returns the corresponding login shell (as recorded in the system passwd file) $repeat(s,i) returns s repeated i times $replace(s1,s2,i1,i2) in the string s1, replace i2 chars beginning at index position i1 with the string s2 $reverse(s) returns string s with its chars reversed $right(s,i) returns the i right-most chars in s $rtrim(s) returns s stripped of any trailing (rightmost) whitespace $script() [see $alarm()] $space() returns the space (' ') char $squote() returns the single quote (') char $status() returns the current script's status $str() $string() [see $fixed()] $substitute(s1,s2,s3,i) in the string s1, replace the string s2 with the string s3 at the ith instance of s2 $substitute(s1,s2,s3) in the string s1, replace every instance of the string s2 with the string s3 $substring(s,i1,i2) $substr(s,i1,i2) $middle(s,i1,i2) $mid(s,i1,i2) for string s, return i2 chars starting at index position i1 $substring(s,i1) $substr(s,i1) $middle(s,i1) $mid(s,i1) for string s, return all chars to the end of the string starting at index position i1 $tab() returns the tab ('\t') char $task() returns the current script's task description $text() [see $fixed()] $trim(s) returns s stripped of any leading and/or trailing whitespace $upper(s) returns s all in upper case
prev page | 1st page | next page |