help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Is there a way to make unamed pipe nonexist upon error?


From: Peng Yu
Subject: Re: [Help-bash] Is there a way to make unamed pipe nonexist upon error?
Date: Thu, 26 Sep 2019 08:31:32 -0500

> What you can do is something like:
>
> gawk -v f=<(cat nosuchfile.txt || echo FAILED) '
>   BEGIN {
>     RS="^$"
>     getline content < f
>     if (content ~ /FAILED/) {
>       print "failed" > "/dev/stderr"
>       exit(1)
>     }
>   }'

https://www.iterm2.com/documentation-escape-codes.html

iterm2 has some proprietary escape codes. Are there any proprietary
escape codes that are appropriate for this purpose to signal a filed
pipe?

> In theory, instead of echo FAILED, you could add code that tries
> to identify the process(es) that are at the other end of the
> pipe and kill the one that is running awk if any. On Linux and
> with recent versions of lsof, that could be done with something
> like:
>
> gawk -v f=<(
>   cat nosuchfile || {
>     re=$'\nnpipe[^\n]*[ ,]([[:digit:]]+),gawk,[[:digit:]]+r'
>     [[ ! $(lsof -Fn -ad3 -E -p "$BASHPID") =~ $re ]] ||
>       kill "${BASH_REMATCH[1]}"
>   } 3>&1) '
>   BEGIN {
>     RS="^$"
>     r = getline content < f
>     print r, content
>   }'

I got an error in lsof. Which version of lsof do you use? My OS is Mac OS X.

$ gawk -v f=<(
        cat nosuchfile || {
                re=$'\nnpipe[^\n]*[ ,]([[:digit:]]+),gawk,[[:digit:]]+r'
                [[ ! $(lsof -Fn -ad3 -E -p "$BASHPID") =~ $re ]] ||
                kill "${BASH_REMATCH[1]}"
        } 3>&1) '
        BEGIN {
                RS="^$"
                r = getline content < f
                print r, content
        }'
cat: nosuchfile: No such file or directory
lsof: illegal option character: E
lsof 4.89
 latest revision: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/
 latest FAQ: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/FAQ
 latest man page: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/lsof_man
 usage: [-?abhlnNoOPRtUvV] [+|-c c] [+|-d s] [+D D] [+|-f[cgG]]
 [-F [f]] [-g [s]] [-i [i]] [+|-L [l]] [+|-M] [-o [o]] [-p s]
 [+|-r [t]] [-s [p:s]] [-S [t]] [-T [t]] [-u s] [+|-w] [-x [fl]] [--] [names]
Use the ``-h'' option to get more help information.
0
$ lsof -v
lsof version information:
    revision: 4.89
    latest revision: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/
    latest FAQ: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/FAQ
    latest man page: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/lsof_man
    configuration info: libproc-based
    Anyone can list all files.
    /dev warnings are disabled.
    Kernel ID check is disabled.

> but that sounds a bit overkill.
>
> awk can run the command and get its output through a pipe by
> itself, and in that case, it can retrieve its exit status, so
> that sounds like a better approach if you want awk to know that
> the command failed:
>
> CMD='cat nosuchfile' gawk '
>   BEGIN {
>     RS="^$"
>     r = ENVIRON["CMD"] | getline output
>     status = close(ENVIRON["CMD"])
>     print r, status, output
>   }'

In my case, CMD is a bash function instead of an external command.
Since a bash function can not be easily called by awk (otherwise, it
becomes too complicated). So I can not use this approach.

> Here, with my version of gawk, I get:
>
> cat: nosuchfile: No such file or directory
> 0 256
>
> Where 256 is the exit status as returned by pclose()/waitpid(),
> which is rc<<8. IMMV, but if that number is non-zero, the
> command failed (either returned with a non-zero exit code or was
> killed).

-- 
Regards,
Peng



reply via email to

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