From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 1562 invoked by alias); 26 Oct 2015 04:09:18 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 20823 Received: (qmail 5789 invoked from network); 26 Oct 2015 04:09:18 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.0 X-Authority-Analysis: v=2.1 cv=X+5rdgje c=1 sm=1 tr=0 a=Yb8cCOgIQM/f+1S+w5Li9g==:117 a=Yb8cCOgIQM/f+1S+w5Li9g==:17 a=IkcTkHD0fZMA:10 a=EOLrCk3c_CyTYQkf8pgA:9 a=QEXdDO2ut3YA:10 Message-id: <562DA061.2080109@eastlink.ca> Date: Sun, 25 Oct 2015 20:39:13 -0700 From: Ray Andrews User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Icedove/31.7.0 MIME-version: 1.0 To: zsh-users@zsh.org Subject: Re: greps pipes and eval bad patterns References: <562D31C3.9030705@eastlink.ca> <151025180235.ZM30558@torch.brasslantern.com> In-reply-to: Content-type: text/plain; charset=utf-8; format=flowed Content-transfer-encoding: 7bit On 10/25/2015 06:17 PM, Kurtis Rader wrote: > In addition to what Bart said I'll point out that it's usually easier and > safer to avoid the eval by using an if/else block. Getting the quoting > right when using eval can be very difficult. Especially if you don't have > direct control over the text being eval'd; e.g., if some of it is supplied > by the user; or even just built up from earlier parts of the function. > > I have several functions where 99% of the time I want the output > automatically piped into my pager (e.g., /usr/bin/less). But if the output > of the function is redirected away from my tty I don't want the pager in > the pipeline. So instead of using a variable that would normally contain "| > $PAGER" and sometimes be empty (i.e., using Ray's approach) I simply spell > it out: > > if [[ -t 1 ]] ; then > grep -h -E $case_insensitive -- "$search_for" $files | $PAGER > else > grep -h -E $case_insensitive -- "$search_for" $files > fi That's exactly the sort of thing I did before in this sort of situation. I take your caveats seriously tho. Mostly I was just interested in the problem for my education. I can quite see that eval could do some weird stuff if the input was uncertain. What matters most is that the code is clear both to read and to execute. Having a pipe inside a variable seems unsound.