Skip to content

proc

This section contains functions related to handling co-processes. The Bash builtin coproc is missing features, in particular there may be only one coproc open at any time.

This library comes with L_proc_popen which allows to open any number of child processes for writing and reading.

#!/bin/bash
. L_lib.sh
childs=()
for script in 'sleep 1 && exit 1' 'sleep 2; exit 2' 'sleep 3; exit 3'; do
  L_proc_popen tmp bash -c "$script"
  childs+=("$(L_array_to_string tmp)")
done
for i in "${childs[@]}"; do
  L_array_from_string child "$i"
  declare -p child
  L_proc_wait child
  echo "Child [$(L_proc_get_cmd child)] pid $(L_proc_get_pid child) exited with $(L_proc_get_exitcode child)"
done

proc

Allows to open multiple processes connected via pipe.

L_is_fd_open

Check if file descriptor is open.

Argument: $1 file descriptor

Shellcheck disable= SC2188

L_get_free_fd

Get free file descriptors

Argument: $@ variables to assign with the file descriptor numbers

L_pipe

Open two connected file descriptors.

This intenrally creates a temporary file with mkfifo The result variable is assigned an array that: - [0] element is input from the pipe, - [1] element is the output to the pipe. This is meant to mimic the pipe() C function.

Arguments:

  • $1 <var> variable name to assign result to
  • $2 <str> template temporary filename, default: L_pipe_XXXXXXXXXX

Shellcheck disable= SC2094

L_proc_popen

Process open. Coproc replacement.

The input/output options are in three groups:

  • -I and -i for stdin,
  • -O and -o for stdout,
  • -E and -e for stderr.

Uppercase letter option specifies the mode for the file descriptor.

There are following modes available that you can give to uppercase options -I -O and -E:

  • null - redirect to or from /dev/null
  • close - close the file descriptor >&-
  • input - -i specifies the string to forward to stdin. Only allowed for -I.
  • stdout - connect file descriptor to stdout. -o or -e value are ignored.
  • stderr - connect file descriptor to stderr. -o or -e value are ignored.
  • pipe - create a fifo and connect file descriptor to it. -i -o or -e option specifies part of the temporary filename.
  • file - connect file descriptor to file specified by -i -o or -e option
  • fd - connect file descriptor to another file descriptor specified by -i -o or -e option

There first argument specifies an output variable that will be assigned as an array with the following indexes:

  • [0] - if -Ipipe will store the file descriptor connected to stdin of the program, otherwise empty.
  • [1] - if -Opipe will store the file descriptor connected to stdout of the program, otherwise empty.
  • [2] - if -Epipe will store the file descriptor connected to stderr of the program, otherwise empty.
  • [3] - stores the pid of the program.
  • [4] - stores the generated command.
  • [5] - stores exitcode.

You should use getters L_proc_get_* to extract the data from proc array elements.

Example

  L_proc_popen -Ipipe -Opipe proc sed 's/w/W/g'
  L_proc_printf proc "%s\n" "Hello world"
  L_proc_read proc line
  L_proc_wait -c -v exitcode proc
  echo "$line"
  echo "$exitcode"

Options:

  • -I <str> stdin mode
  • -i <str> string for -Iinput, file for -Ifile, fd for -Ifd
  • -O <str> stdout mode
  • -o <str> file for -Ifile, fd for -Ifd
  • -E <str> stderr mode
  • -e <str> file for -Efile, fd for -Efd
  • -p <int> Open a pipe for additional file descriptors. (TODO)
  • -n Dryrun mode. Do not execute the generated command. Instead print it to stdout.

Arguments:

  • $1 variable name to store the result to.
  • $@ command to execute.

L_proc_get_stdin

Get file descriptor for stdin of L_proc.

Option: -v <var> variable to set

Argument: $1 L_proc variable

L_proc_get_stdin_v

L_proc_get_stdout

Get file descriptor for stdout of L_proc.

Option: -v <var> variable to set

Argument: $1 L_proc variable

L_proc_get_stdout_v

L_proc_get_stderr

Get file descriptor for stderr of L_proc.

Option: -v <var> variable to set

Argument: $1 L_proc variable

L_proc_get_stderr_v

L_proc_get_pid

Get PID of L_proc.

Option: -v <var> variable to set

Argument: $1 L_proc variable

L_proc_get_pid_v

L_proc_get_cmd

Get command of L_proc.

Option: -v <var> variable to set

Argument: $1 L_proc variable

L_proc_get_cmd_v

L_proc_get_exitcode

Get exitcode of L_proc.

Option: -v <var> variable to set

Argument: $1 L_proc variable

L_proc_get_exitcode_v

L_proc_printf

Write printf formatted string to coproc.

Arguments:

  • $1 L_proc variable
  • $@ any printf arguments

L_proc_read

Exec read bultin with -u file descriptor of stdout of coproc.

Arguments:

  • $1 L_proc variable
  • $@ any builtin read options

L_proc_read_stderr

Exec read bultin with -u file descriptor of stderr of coproc.

Arguments:

  • $1 L_proc variable
  • $@ any builtin read options

See: L_proc_read

L_proc_close

Close stdin, stdout and stderr of L_proc

Argument: $1 L_proc variable

L_proc_close_stdin

Close stdin of L_proc.

Does nothing if already closed or not started with -Opipe.

Argument: $1 L_proc variable

L_proc_close_stdout

Close stdout of L_proc.

Does nothing if already closed or not started with -Opipe

Argument: $1 L_proc variable

L_proc_close_stderr

Close stderr of L_proc.

Does nothing if already closed or not started with -Epipe.

Argument: $1 L_proc variable

L_proc_poll

Check if L_proc is finished.

Argument: $1 L_proc variable

Exit: 0 if L_proc is running, 1 otherwise

L_proc_wait

Wait for L_proc to finish.

If L_proc has already finished execution, will only evaluate -v option.

Options:

  • -t <int> Timeout in seconds. Will try to use waitpid, tail --pid or busy loop with sleep.
  • -v <var> Assign exitcode to this variable.
  • -c Close L_proc file descriptors before waiting.

Argument: $1 L_proc variable

Exit: 0 if L_proc has finished, 1 if timeout expired

L_read_fds

Read from multiple file descriptors at the same time.

Arguments:

  • -t <timeout> Timeout in seconds.
  • -p <timeout> Poll timeout. The read -t argument value. Default: 0.01
  • $1 <int> file descriptor to read from
  • $2 <var> variable to assign the output of
  • $@ Pairs of variables and file descriptors.

L_proc_communicate

Communicate with L_proc.

Options:

  • -i <str> Send string to stdin.
  • -o <var> Assign stdout to this variable.
  • -e <var> Assign stderr to this variable.
  • -t <int> Timeout in seconds.
  • -k Kill L_proc after communication.
  • -v <var> Assign exitcode to this variable.

Argument: $1 L_proc variable

Exit: 0 on success. 2 on invalid options. 128 on timeout when reading output. 130 on timeout when waiting for process to terminate.

L_proc_send_signal

Send signal to L_proc.

Argument: $1 L_proc variable

L_proc_terminate

Terminate L_proc.

Argument: $1 L_proc variable

L_proc_kill

Kill L_proc.

Argument: $1 L_proc variable