From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18942 invoked from network); 8 Mar 2007 21:20:08 -0000 X-Spam-Checker-Version: SpamAssassin 3.1.8 (2007-02-13) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00,FORGED_RCVD_HELO autolearn=ham version=3.1.8 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 8 Mar 2007 21:20:08 -0000 Received-SPF: none (ns1.primenet.com.au: domain at sunsite.dk does not designate permitted sender hosts) Received: (qmail 67320 invoked from network); 8 Mar 2007 21:20:02 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 8 Mar 2007 21:20:02 -0000 Received: (qmail 3130 invoked by alias); 8 Mar 2007 21:19:59 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 23209 Received: (qmail 3114 invoked from network); 8 Mar 2007 21:19:58 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 8 Mar 2007 21:19:58 -0000 Received: (qmail 66924 invoked from network); 8 Mar 2007 21:19:58 -0000 Received: from nf-out-0910.google.com (64.233.182.190) by a.mx.sunsite.dk with SMTP; 8 Mar 2007 21:19:53 -0000 Received: by nf-out-0910.google.com with SMTP id m19so735809nfc for ; Thu, 08 Mar 2007 13:19:53 -0800 (PST) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=J6T9K/T78z+61O7IwK5nnO2zddMI2uvTzY9538U0FHgbpOrQLqsIn0IYYfyVGpYqiZKtY/EiX6fvvKu6X5YuR0tPaO0VbwfcMEhL49lZTfeKhYIHL3mBlXNKj5HSzpdp1oNtzlJ6Cyf4LaZhOZX/Wr1thTt0vbHcwszwr3ST0QU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=GPJTppnaTmUj/2ftscd/gWBXVjkBNBeRagOJH8vJjDMy++hQX7r2z7eYVC/yQjjkZba+vA482QhD7V4HYoFf/lPmcS3cdaHqqJUBpbZ9BjUdysaTraek3gEPeqZtyxdyY/B1mc6ScuAswElPc2vucinbqJTij8UvTpfHQYScMzc= Received: by 10.82.154.2 with SMTP id b2mr1550837bue.1173388792807; Thu, 08 Mar 2007 13:19:52 -0800 (PST) Received: by 10.82.161.13 with HTTP; Thu, 8 Mar 2007 13:19:52 -0800 (PST) Message-ID: <17393e3e0703081319r7a77aaf0w24f856abc5c7ea5c@mail.gmail.com> Date: Thu, 8 Mar 2007 16:19:52 -0500 From: "Matt Wozniski" To: zsh-workers@sunsite.dk, zsh-users@sunsite.dk Subject: Re: "jobs" command within substitution In-Reply-To: <45F070A9.5060502@cowan.name> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <45F0638B.6090203@cowan.name> <17393e3e0703081155o7a240628t49fae220ac9f49ae@mail.gmail.com> <45F070A9.5060502@cowan.name> On 3/8/07, Micah Cowan wrote: > Matt Wozniski wrote: > > On 3/8/07, Micah Cowan wrote: > >> Hello, all. This is my first post to this group. > >> > >> I scoured the manpages and list archives, but could not find the answer > >> I seek. > >> > >> My question, in a nutshell, is: How can I effectively use the "jobs" > >> builtin command in producing the interactive prompt? > > > > Rather than trying to parse the output of the 'jobs' command, you > > might find yourself better suited by manipulating the variables > > $jobdirs, $jobstates, and $jobtexts. You correctly identified the > > problem that you're hitting - $(jobs) is running in a subshell that > > doesn't have any jobs in its job table. I agree, however, that (jobs) > > should also be blank, since it's also running in a subshell. If, > > however, you're dead-set on parsing the output of 'jobs', you could > > use a syntax like 'jobs > >(read jobtext; echo $jobtext)', which is a > > clever way to run jobs in the current shell and do the parsing in a > > subshell via process substitution. > > Myself, I'd prefer to see both subshells produce the same output as the > "current shell", as bash, pdksh and ksh do. However, dash goes the other > way and makes ( jobs ) emit nothing (for all its claims of being a POSIX > shell, though, dash is fairly broken in some respects, such as broken > arithmetic expansion and lack of a line-editor [which POSIX requires]). > I'd be interested in seeing what the OpenGroup committee has to say > about it, since the standard is far from clear on the subject. > > I was not familiar with the variables you mention above. However, I'm > not sure they solve the problem, as yet again, invoking them within a > command-substitution will produce no information. The same problem would > be true of using the other syntax you describe: I still have no > available means to run the commands every time the prompt is issued, > apart from within command substitution, which will kill the job-state > info. Is there any way to get what I want executing in the "current" shell? > > Also, had you meant to post this reply to the list? It appears to have > been sent to me only. Yes, of course, I had meant to send it to the list. My mistake. *sheepish grin*. So, you're right - the special variable $jobtexts wouldn't match up in the subshell, but you could do something like this: $ function precmd { export jt="" for i in ${(kv)jobtexts}; jt="$jt:${i%% *}" jt=${jt#:} } $ echo $jt 1:find:2:sleep $ echo $(echo $jt) 1:find:2:sleep And then you have a not-special, not-array version of jobtexts exported into the environment of subshells that you can manipulate however you want - and it even lets you remove the ugly dependence on awk. ;-) (precmd is a function that gets called every time the editor is about to display a prompt, FYI). Like I said, though - even though this should take no effort whatsoever to do with zsh - no more than 5 lines or so - it would be difficult to maintain compatibility with bash. The best way I can think of to do what you want, off the top of my head, is the following: function precmd { psvar[1]="" # For each key and each value in jobtexts for i in ${(kv)jobtexts}; do # Come up with a separator between psvar[1] and this text if [[ $sep == " " ]]; then sep=":" else sep=" " fi # Then tack it on to the end of psvar[1] - Removing from # the first space to the end of the element, if it has a space psvar[1]="${psvar[1]}$sep${i%% *}" done # Remove leading space we accidentally inserted psvar[1]=${psvar[1]# } } and # Tell the prompt to reference psvar[1] (%?v == psvar[?]) PS1='micah(%1v)' You don't even need to use prompt_subst. Frankly, I'd just make your script check for ZSH right off the bat and do it the simple way, leaving the complicated stuff for shells that don't give you an elegant solution. ~Matt