Re: Issus with popd and pushd
Thanks I was being a bit dense, didn't realize that the format you gave causes the commands to run in the parent shell. It works as it should! -Pete On Fri, Apr 21, 2017 at 10:52 AM, Chet Rameywrote: > On 4/21/17 1:36 PM, Chet Ramey wrote: > > > I think the key takeaway is that this is trivial to do with a shell > > function, even if you want to add argument parsing (use getopts) and > > pass all the option arguments except -v to the builtins. > > On second thought, don't use getopts. > > -- > ``The lyf so short, the craft so long to lerne.'' - Chaucer > ``Ars longa, vita brevis'' - Hippocrates > Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~ > chet/ >
Re: Issus with popd and pushd
On 4/21/17 1:36 PM, Chet Ramey wrote: > I think the key takeaway is that this is trivial to do with a shell > function, even if you want to add argument parsing (use getopts) and > pass all the option arguments except -v to the builtins. On second thought, don't use getopts. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/
Re: Issus with popd and pushd
On 4/21/17 11:48 AM, Pete Smith wrote: > It appears to me that the sub shell does inherit the dirs stack, but any > changes that are made to the dir stack remain > in the sub shell and do not propagate back to the parent shell. Correct; it's a separate process. > Does the exchanges of your implementation comments mean the possibility of > the addition of the -v option to pushd and > popd are in the realm of possibility? I think the key takeaway is that this is trivial to do with a shell function, even if you want to add argument parsing (use getopts) and pass all the option arguments except -v to the builtins. I would pursue that implementation first. It's much simpler and can be done more quickly. Chet -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/
Re: Issus with popd and pushd
Am clearly out of my league in these exchanges about possible implementation. Nevertheless am heartened by seeing these exchanges. As to the question that came up in the dialogue: > Um... don't you mean to use the alias command somewhere, like: > > alias popd='builtin popd|tr " " "\n"' I defined the alias in ~/.bash_aliases >From one of your initial explanations: that points out that the alias would be executed in a sub shell, it makes sense to me that, that approach won't work. It appears to me that the sub shell does inherit the dirs stack, but any changes that are made to the dir stack remain in the sub shell and do not propagate back to the parent shell. I was thrown off track by: alias dirs='dirs -v' But after you explanation of the sub shell execution environment, this makes sense as dirs does not make any changes to the dirs stack. Does the exchanges of your implementation comments mean the possibility of the addition of the -v option to pushd and popd are in the realm of possibility? -Pete On Tue, Apr 18, 2017 at 11:17 AM, Chet Rameywrote: > On 4/18/17 2:13 PM, L A Walsh wrote: > > Chet Ramey wrote: > >> On 4/18/17 9:35 AM, Eduardo Bustamante wrote: > >> > >> > >>> Or now that I think about it, you can get away with these functions: > >>> > >>> # masked builtins > >>> dualbus@debian:~/foo/bar/baz$ pushd() { builtin pushd "$@" >/dev/null; > >>> dirs -v; }; popd(){ builtin popd "$@" >/dev/null; dirs -v; } > >>> > >> > >> This would be the preferable alternative, since it's so trivial. The > one > >> change I would suggest would be to make the `;' a `&&': > >> > >> pushd() > >> { > >> builtin pushd "$@" >/dev/null && dirs -v > >> } > >> > >> > > Maybe add 'builtin' before "dirs" since we're redefining builtins > > (i.e. get into habit?) > > Sure, if there's a chance there's a function named `dirs' and you *don't* > want to use it, this is good practice. > > -- > ``The lyf so short, the craft so long to lerne.'' - Chaucer > ``Ars longa, vita brevis'' - Hippocrates > Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~ > chet/ >
Re: Issus with popd and pushd
On 4/18/17 2:13 PM, L A Walsh wrote: > Chet Ramey wrote: >> On 4/18/17 9:35 AM, Eduardo Bustamante wrote: >> >> >>> Or now that I think about it, you can get away with these functions: >>> >>> # masked builtins >>> dualbus@debian:~/foo/bar/baz$ pushd() { builtin pushd "$@" >/dev/null; >>> dirs -v; }; popd(){ builtin popd "$@" >/dev/null; dirs -v; } >>> >> >> This would be the preferable alternative, since it's so trivial. The one >> change I would suggest would be to make the `;' a `&&': >> >> pushd() >> { >> builtin pushd "$@" >/dev/null && dirs -v >> } >> >> > Maybe add 'builtin' before "dirs" since we're redefining builtins > (i.e. get into habit?) Sure, if there's a chance there's a function named `dirs' and you *don't* want to use it, this is good practice. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/
Re: Issus with popd and pushd
Chet Ramey wrote: On 4/18/17 9:35 AM, Eduardo Bustamante wrote: Or now that I think about it, you can get away with these functions: # masked builtins dualbus@debian:~/foo/bar/baz$ pushd() { builtin pushd "$@" >/dev/null; dirs -v; }; popd(){ builtin popd "$@" >/dev/null; dirs -v; } This would be the preferable alternative, since it's so trivial. The one change I would suggest would be to make the `;' a `&&': pushd() { builtin pushd "$@" >/dev/null && dirs -v } Maybe add 'builtin' before "dirs" since we're redefining builtins (i.e. get into habit?) (Ms. "Shoot-1st, then aim")
Re: Issus with popd and pushd
L A Walsh wrote: Pete Smith wrote: The problem with: dirs, pushd, popd ... Using an alias solution: popd | sed 's/\s/\n/g' | nl Um... don't you mean to use the alias command somewhere, like: alias popd='builtin popd|tr " " "\n"' Then it seems to work. Is that what you mean by using an alias? scratch that... just focused on output, not on function. *sigh*...need coffee!
Re: Issus with popd and pushd
Pete Smith wrote: The problem with: dirs, pushd, popd ... Using an alias solution: popd | sed 's/\s/\n/g' | nl Um... don't you mean to use the alias command somewhere, like: alias popd='builtin popd|tr " " "\n"' Then it seems to work. Is that what you mean by using an alias?
Re: Issus with popd and pushd
On 4/17/17 5:57 PM, Pete Smith wrote: > Ahh that makes sense: > >"That will never change the current directory, since the popd is run in > a subshell." > > So what's the possibility of adding -v option to popd and pushd??? I'd advise doing it with a shell function first; that's much easier to play around with. Start with something similar to what Eduardo or I sent and add whatever option parsing you want. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/
Re: Issus with popd and pushd
On 4/18/17 9:35 AM, Eduardo Bustamante wrote: > Or now that I think about it, you can get away with these functions: > > # masked builtins > dualbus@debian:~/foo/bar/baz$ pushd() { builtin pushd "$@" >/dev/null; > dirs -v; }; popd(){ builtin popd "$@" >/dev/null; dirs -v; } This would be the preferable alternative, since it's so trivial. The one change I would suggest would be to make the `;' a `&&': pushd() { builtin pushd "$@" >/dev/null && dirs -v } -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/
Re: Issus with popd and pushd
On Mon, Apr 17, 2017 at 4:57 PM, Pete Smithwrote: [...] > So what's the possibility of adding -v option to popd and pushd??? Feel free to send patches. Under the hood, the pushd/popd builtins call the dirs builtin with no arguments after their execution, so it's just a matter of adding to pushd/popd the ability to interpret the -v flag, and then pass this flag back to the dirs_builtin execution. In the below patch I just pass the flag unconditionally, but it should give you an idea of what you need to do. # patched bash-4.4$ pwd /home/dualbus/foo/bar/baz bash-4.4$ pushd a/ 0 ~/foo/bar/baz/a 1 ~/foo/bar/baz bash-4.4$ pushd bb/ 0 ~/foo/bar/baz/a/bb 1 ~/foo/bar/baz/a 2 ~/foo/bar/baz bash-4.4$ popd 0 ~/foo/bar/baz/a 1 ~/foo/bar/baz Or now that I think about it, you can get away with these functions: # masked builtins dualbus@debian:~/foo/bar/baz$ pushd() { builtin pushd "$@" >/dev/null; dirs -v; }; popd(){ builtin popd "$@" >/dev/null; dirs -v; } dualbus@debian:~/foo/bar/baz$ pushd a/ 0 ~/foo/bar/baz/a 1 ~/foo/bar/baz dualbus@debian:~/foo/bar/baz/a$ pushd bb/ 0 ~/foo/bar/baz/a/bb 1 ~/foo/bar/baz/a 2 ~/foo/bar/baz # patch dualbus@debian:~/src/gnu/bash$ PAGER= git diff -- builtins diff --git a/builtins/pushd.def b/builtins/pushd.def index 6579e4c8..bd41fcca 100644 --- a/builtins/pushd.def +++ b/builtins/pushd.def @@ -178,6 +178,8 @@ pushd_builtin (list) int j, flags, skipopt; intmax_t num; char direction; + WORD_DESC *wd; + WORD_LIST *wl; orig_list = list; @@ -300,7 +302,11 @@ pushd_builtin (list) if (j == EXECUTION_SUCCESS) { add_dirstack_element ((flags & NOCD) ? savestring (list->word->word) : current_directory); - dirs_builtin ((WORD_LIST *)NULL); + + wd = make_word ("-v"); + wl = make_word_list (wd, NULL); + + dirs_builtin (wl); if (flags & NOCD) free (current_directory); return (EXECUTION_SUCCESS); @@ -324,6 +330,8 @@ popd_builtin (list) int flags; char direction; char *which_word; + WORD_DESC *wd; + WORD_LIST *wl; CHECK_HELPOPT (list); @@ -400,7 +408,10 @@ popd_builtin (list) pushd_directory_list[i] = pushd_directory_list[i + 1]; } - dirs_builtin ((WORD_LIST *)NULL); + wd = make_word ("-v"); + wl = make_word_list (wd, NULL); + + dirs_builtin (wl); return (EXECUTION_SUCCESS); }
Re: Issus with popd and pushd
Ahh that makes sense: "That will never change the current directory, since the popd is run in a subshell." So what's the possibility of adding -v option to popd and pushd??? -Pete On Mon, Apr 17, 2017 at 1:33 PM, Chet Rameywrote: > On 4/17/17 2:05 PM, Pete Smith wrote: > > > Using an alias solution: > > > > popd | sed 's/\s/\n/g' | nl > > > > doesn't work, probably because they are shell built-ins. > > That will never change the current directory, since the popd is run in > a subshell. > > -- > ``The lyf so short, the craft so long to lerne.'' - Chaucer > ``Ars longa, vita brevis'' - Hippocrates > Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~ > chet/ >
Re: Issus with popd and pushd
On 4/17/17 2:05 PM, Pete Smith wrote: > Using an alias solution: > > popd | sed 's/\s/\n/g' | nl > > doesn't work, probably because they are shell built-ins. That will never change the current directory, since the popd is run in a subshell. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/
Re: Issus with popd and pushd
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Hi, Am 17.04.2017 um 20:05 schrieb Pete Smith: > The problem with: dirs, pushd, popd is that they output a single line of > paths that's difficult to parse visually quickly, especially when there are > many paths in the dir stack and the path > names are long. > > dirs offers a reasonable solution with the -v option > > Unfortunately popd and pushd do NOT offer this option. > > Using an alias solution: > > popd | sed 's/\s/\n/g' | nl > > doesn't work, probably because they are shell built-ins. It's a question whether it's a feature or a bug: if stdout is redirected for it, the directory isn't changed any longer AFAICS. - -- Reuti > I'd recommend the addition of the -v option to popd and pushd, or a fix so > that an alias like the one outline above, works. > > Appreciate your comments and feedback on this. > > -Pete -BEGIN PGP SIGNATURE- Comment: GPGTools - https://gpgtools.org iEYEARECAAYFAlj1JWgACgkQo/GbGkBRnRofagCfUNXHWBVDDYoKZUS73A5+YGkp JP8An0NkPu2i5U0rI3tUcbrgXbB8sTKF =8mly -END PGP SIGNATURE-