NAME
shellia - library for interactive shell scriptsSYNOPSIS
.
/usr/share/shellia/ia[.interactive|.check|.debug|]
eval "$ia_init"
[[ ia_nocheck [-f]] | [[ia_stdout|ia_ignore regex]
...]]
ia_add command-sequence
dbg [debug-level [debug-topic ...]] debug-output
ia [-c|-C] [-x]
ENVIRONMENT
If ia_logfile contains the path to a
writeable file, logfile output as seen by ia is appended to that
file.
DESCRIPTION
shellia is a library that allows to run shell scripts silently or interactive and helps to check errors of commands that are combined in steps.OPTIONS
- . /usr/share/shellia/ia[.interactive|.check|.debug|]
- To only load the shellia interactive library for basic interactive features use . /usr/share/shellia/ia.interactive. To only load shellia check library to check stderr, stdout and exit-value of steps use . /usr/share/shellia/ia.check. TO only load the shellia debug library, to add debug-output to a shell script use . /usr/share/shellia/ia.debug. To source all shellia libraries listed above is the suggested method and can be done with . /usr/share/shellia/ia.
- ia_nocheck
- This command can be used, if the following ia command has enabled check-mode with option [-c|-C] for its group of steps. This will disable the check of stderr and stdout in the next single ia_add step. With opiton -f added, it will also disable checking the exit value of the next single step. Also if a step may exit the script, ia_nocheck can be used to prevent the message ERROR: ia premature exit of ....
- ia_stdout regex
- to use this command, the following ia command has to be used with option -c. regex is a regualar expressions as defined in gawk(1), to define which output of the following step defined with ia_add will be standard output of the step. Each step can be preceded with any number of ia_stdout regex commands. Because the user can not suppress output allowed with ia_stodut with the option -s, ia_stdout should only be used, if the output is intendet to be piped to another programm. regex only needs to handle text without color-control-codes.
- ia_ignore regex
- to use this command, the following ia command has to be used with either option -c or -C. regex is a regualar expressions as defined in gawk(1), to define which output of the following step defined with ia_add will be ignored. Each step can be preceded with any number of ia_ignore regex commands. regex only needs to handle text without color-control-codes.
- -c
- if this option is given to ia all steps previous added with ia_add will be checked with check-mode option -c (see check-mode below)
- -C
- if this option is given to ia all steps previous added with ia_add will be checked with check-mode option -C. (see check-mode below)
- -x
- if this option is given to ia all steps previous added with ia_add will use trace-mode. (see trace-mode below)
USAGE
The examples listed below try to show the technical features of shellia in very small examples.Basic features
A script using the basic features to print hello world may look like:01 #!/bin/sh 02 . /usr/share/shellia/ia.interactive 03 eval "$ia_init" 04 ia_add "echo \"hello\"" 05 ia_add "echo \"world\"" 06 ia
Interactive-mode
A script using a function in interactive-mode may look like:01 . /usr/share/shellia/ia.interactive 02 say_hello() 03 { 04 local name 05 eval "$ia_init" 06 ia_add "name=\"$1\"" 07 ia_add "echo \"hello \$name\"" 08 ia 09 } 10 eval "$ia_init" 11 ia_add "say_hello <-i> -- \"$1\"" 12 ia
check-mode
Before demonstrating how to use the check-mode option -c or -C we first start with the following script, without any check-mode option:01 . /usr/share/shellia/ia 02 say_hello() 03 { 04 echo "hello $1" 05 echo "end of function say_hello" >&2 06 return 5 07 } 08 eval "$ia_init" 09 ia_add "say_hello \"$1\"" 10 ia
01 $ ./hello_world_check "shellia user" 02 hello shellia user 03 end of function say_hello 04 $ echo $? 05 5
- •
- the output "end of function say_hello" makes no sense for the user. We know it is harmless, and we just want to suppress it.
- •
- output starting with "hello" is what we want. But any other output, that we may not have seen until now, should be displayed as error.
- •
- We want to see every nonzero exit value as an error. Only an exit value of 5 seems to be ok and should not be displayed as error.
check-mode -c
To get the wanted behaviour described in previous chapter with check-mode option -c we change script line 10 to:10 ia -c
01 $ ./hello_world_check "shellia user" 02 --- output with ERROR from <hello_world_check> running <say_hello "shellia user"> --- 03 e:hello 04 e:end of function say_hello 05 e:exit=5 06 --- (q)uit (r)edo (i)gnore (s)witch interactive --- 07 ?
+a ia_stdout "^hello " +b ia_ignore "end of function say_hello$" +c ia_ignore "^exit=5$" 09 ia_add "say_hello \"$1\""
check-mode -C
A file that starts to fix say_hello with check-mode option -C is provided as example and the usage is explaind in shellia(1). On debian systems this script may be found at /usr/share/doc/shellia/examples/hello_world_error. The changed script line from the example in chapter check-mode is:10 ia -C
01 $ ./hello_world_error "shellia user" 02 --- output with ERROR from <hello_world_error> running <say_hello "shellia user"> --- 03 e:end of function say_hello 04 e:exit=5 05 s:hello --- (q)uit (r)edo (i)gnore (s)witch interactive --- ?
+a ia_ignore "end of function say_hello$" +b ia_ignore "^exit=5$" 09 ia_add "say_hello \"$1\""
debug-mode
The library ia.debug includes functions to add debug-output to a shell script. The script.using.shellia can then be called with a debug-runtime-config to define the debug-level and to select debug-topics. See shellia(1) debug-mode to learn about debug-runtime-config.01 . /usr/share/shellia/ia 02 say_hello_world() 03 { 04 dbg "say_hello function start" 05 echo "hello world" 06 dbg "say_hello function end" 07 } 08 eval "$ia_init" 09 ia_add "dbg \"main program\"" 10 ia_add say_hello_world 11 ia
04 dbg 3 "say_hello function start" 06 dbg 3 "say_hello function end"
04 dbg 3 start "say_hello function start" 06 dbg 3 end "say_hello function end"
log-mode
The library ia.log includes functions to add logging to a shell script.01 ia_logfile="$(mktemp)" 02 export ia_logfile 03 . /usr/share/shellia/ia 04 say_hello() 05 { 06 local name 07 eval "$ia_init" 08 ia_add "dbg \"function say_hello called with $# arguments\"" 09 ia_add "name=\"$1\"" 10 ia_stdout "^hello" 11 ia_add "echo \"hello \$name\"" 12 ia -c 13 } 14 eval "$ia_init" 15 ia_add "dbg \"main program begin\"" 16 ia_nocheck # do not check the already checked output of next line 17 ia_add "say_hello <-i> -- \"$1\"" 18 ia -c
trace-mode
Trace-mode can be enabled with ia -x for a group of steps. When tracemode is enabled, before each step set -x is called and after each stet set +x is called. This way, no shellia internal commands are traced.01 . /usr/share/shellia/ia 02 write_file() { 03 eval "$ia_init" 04 ia_add "echo \"hello $2\" >$1" 05 ia -c -x 06 } 07 show_file() { 08 eval "$ia_init" 09 ia_stdout "^hello" 10 ia_add "cat $1" 11 ia -c -x 12 } 13 eval "$ia_init" 14 ia_add "file=\"\$(mktemp)\"" 15 ia_add "write_file <-i> -- \"\$file\" \"$1\"" 16 ia_stdout "." 17 ia_add "show_file <-i> -- \"\$file\"" 18 ia_add "rm -f \"\$file\"" 19 ia -c -x
$ /usr/share/doc/shellia/examples/hello_world_trace "shellia user" + mktemp + file=/tmp/tmp.zCDaHg9dFP + echo hello shellia user + write_file -- /tmp/tmp.zCDaHg9dFP shellia user + cat /tmp/tmp.zCDaHg9dFP + show_file -- /tmp/tmp.zCDaHg9dFP hello shellia user + rm -f /tmp/tmp.zCDaHg9dFP
|hello_world_trace |+ mktemp |+ file=/tmp/tmp.zCDaHg9dFP ||hello_world_trace/write_file ||+ echo hello shellia user |+ write_file -- /tmp/tmp.zCDaHg9dFP shellia user ||hello_world_trace/show_file ||+ cat /tmp/tmp.zCDaHg9dFP ||s:hello shellia user |+ show_file -- /tmp/tmp.zCDaHg9dFP |s:hello shellia user |+ rm -f /tmp/tmp.zCDaHg9dFP
variable in Interactive-mode
If you want to enable the user to see the value of a variable, before it is used, with key press v, you can write $<var> instead of \$var or \${var}.HIGH LEVEL FUNCTIONS
ia_ssh
ia_add "ia_ssh
ssh-options [ --fd fd] [--vars vars]
destination shellia-script"
<ssh-options> ssh options described in ssh(1) <fd> the filedescriptors <fd> will be transfered <vars> the variables <vars> will be provided <destination> ssh destination described in ssh(1) <shellia-script> script that can use the transferred filedescriptors and variables
PREFIX
Commands or global variables brought by shellia normally start with ia (see NAME in shellia(7)). If ia.debug is used there will also be commands and global variables that start with dbg. But if you already use one or both of this prefixes, you can change them, while loading the library.ia_prefix=mycmd dbg_prefix=myprnt . /usr/share/shellia/ia
WARNING
It is not always possible to separate sellia's internal variables from script variables. For this reason variable names starting with ia_ should not be used in scripts.SEE ALSO
shellia(1), shellia(7), gawk(1)AUTHOR
[email protected]COPYRIGHT
Bernd Schumacher <[email protected]> (2007-2020)2020-08-07 | 0.1 |