help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to use redirection in a function so that the function callers don't


From: Peng Yu
Subject: How to use redirection in a function so that the function callers don't need to know such details?
Date: Mon, 6 Apr 2020 15:53:27 -0500

https://lists.gnu.org/archive/html/help-bash/2020-03/msg00071.html

I am experimenting how to use redirection in the pipeline due to the
above reason.

The problem that I encounter is that if the redirection occurs in a
function, the caller of the function must know what redirection is
used within the function. See the following as an example.

Is there a smart way to solve this kind of conflict so that the
callers of the function don't need to know the details of the
redirection within the function? Thanks.

$ cat ./main.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

set -v
function f {
        cat "$1" | {
                paste /dev/fd/3 "$2" "$3"
        } 3<&0
}
f <(seq 3) <(seq 11 13) <(seq 21 23)
seq 3 | {
        seq 11 13 | {
                seq 21 23 | {
                        f /dev/fd/4 /dev/fd/3 /dev/fd/5
                } 5<&0
        } 3<&0
} 4<&0
$  ./main.sh
function f {
        cat "$1" | {
                paste /dev/fd/3 "$2" "$3"
        } 3<&0
}
f <(seq 3) <(seq 11 13) <(seq 21 23)
1       11      21
2       12      22
3       13      23
seq 3 | {
        seq 11 13 | {
                seq 21 23 | {
                        f /dev/fd/4 /dev/fd/3 /dev/fd/5
                } 5<&0
        } 3<&0
} 4<&0
1               21
2               22
3               23


-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]