Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: detect pipe



On 2021-01-27 3:59 p.m., Bart Schaefer wrote:
[[ -p /dev/fd/0 ]]
Nuts, using that it turns out that the issue is that my functions tend to be called via aliases:

    alias test2='noglob test1'

   test1 ()
   {
        if [[ -p /dev/fd/0 ]]; then
            echo "PIPES ARE AS BAD AS CIGARETTES"
            grep --color=always "$1"
        else
            echo "NO, THEY AREN'T"
            grep --color=always "$1" "$2"
        fi
   }


Testing it:

   $ . test; echo "Now is the time for all good men" | test1 'the time'
   PIPES ARE AS BAD AS CIGARETTES
   Now is the time for all good men

   $ . test; echo "Now is the time for all good men" | test2 'the time'
   NO, THEY AREN'T
   grep: : No such file or directory

   $ . test; test1 'the time' junk    # junk contains the same string
   NO, THEY AREN'T
   Now is the time for all good men

   $ . test; test2 'the time' junk
   NO, THEY AREN'T
   Now is the time for all good men

... so the pipe test is fooled by the alias. Strange tho that it used to work and still does *sometimes*.  Very flaky. Dunno why an alias would ... well that's just it, I don't know.







Messages sorted by: Reverse Date, Date, Thread, Author