Prev: why is echo different?
Next: unpack a gz file
From: Parmenides on 4 Dec 2009 12:40 The following function is another pushd. If the && were replaced by -a in the pointed line, a error would come. What's the difference between && and -a? pushd() { if [ $(echo $1 | grep '^+[0-9][0-9]*$') ]; then # case of pushd +n: rotate n-th directory to top let num=${1#+} getNdirs $num DIR_STACK="$target$stackfront$DIR_STACK" cd $target echo "$DIR_STACK" elif [ -z "$1" ]; then # case of pushd without args; swap top two directories firstdir=${DIR_STACK%% *} DIR_STACK=${DIR_STACK#* } seconddir=${DIR_STACK%% *} DIR_STACK=${DIR_STACK#* } DIR_STACK="$seconddir $firstdir $DIR_STACK" cd $seconddir echo "$DIR_STACK" else # normal case of pushd dirname dirname=$1 if [ \( -d $dirname \) -a \( -x $dirname \) ]; then if [ $dirname = $PWD ] && [ -z "$DIR_STACK" ]; then <------- DIR_STACK="$dirname" else DIR_STACK="$dirname ${DIR_STACK:-$PWD }" fi cd $dirname echo "$DIR_STACK" else echo still in "$PWD" fi fi }
From: Rakesh Sharma on 4 Dec 2009 13:08 On Dec 4, 10:40 pm, Parmenides <mobile.parmeni...(a)gmail.com> wrote: > The following function is another pushd. If the && were replaced by -a > in the pointed line, a error would come. What's the difference between > && and -a? > > pushd() > { > if [ $(echo $1 | grep '^+[0-9][0-9]*$') ]; then > # case of pushd +n: rotate n-th directory to top > let num=${1#+} > getNdirs $num > DIR_STACK="$target$stackfront$DIR_STACK" > cd $target > echo "$DIR_STACK" > elif [ -z "$1" ]; then > # case of pushd without args; swap top two directories > firstdir=${DIR_STACK%% *} > DIR_STACK=${DIR_STACK#* } > seconddir=${DIR_STACK%% *} > DIR_STACK=${DIR_STACK#* } > DIR_STACK="$seconddir $firstdir $DIR_STACK" > cd $seconddir > echo "$DIR_STACK" > else > # normal case of pushd dirname > dirname=$1 > if [ \( -d $dirname \) -a \( -x $dirname \) ]; then > if [ $dirname = $PWD ] && [ -z "$DIR_STACK" ]; then <------- > DIR_STACK="$dirname" > else > DIR_STACK="$dirname ${DIR_STACK:-$PWD }" > fi > cd $dirname > echo "$DIR_STACK" > else > echo still in "$PWD" > fi > fi > > } In the below-mentioned line we cannot simply swap && with -a > if [ $dirname = $PWD ] && [ -z "$DIR_STACK" ]; then <------- It should be written as: if [ \( "x${dirname}" = "x${PWD}" \) -a \( -z "$ {DIR_STACK}" \) ]; then
From: bsh on 10 Dec 2009 21:59 On Dec 4, 10:08 am, Rakesh Sharma <sharma...(a)hotmail.com> wrote: > On Dec 4, 10:40 pm, Parmenides <mobile.parmeni...(a)gmail.com> wrote: > > ... > In the below-mentioned line we cannot simply swap && with -a. Why? "-a" within at least the "[[ ... ]]" test condition is equivalent to "&&" and will be decomposed to "[[ ... ]] && [[ ... ]] ...", as seen in xtraced diagnostic output. > if [ $dirname = $PWD ] && [ -z "$DIR_STACK" ]; then <------- > should be written as: > if [ \( "x${dirname}" = "x${PWD}" \) -a \( -z "${DIR_STACK}" \) ]; Indeed, the POSIX deprecates the use of interstital -o and -a in preference of && and || to obviate some pathological input cases! See my post at: http://groups.google.com/group/comp.unix.shell/msg/27ed00e545a0a292 ^ firstdir=${DIR_STACK%% *} Okay, so you are using ksh(1) (or bash(1)?) A few points then: 1. POSIX functions ( "foo()" ) are deprecated for robust scripting. Use kornshell syntax ("function foo") instead. 2. Why is grep(1) _ever_ appearing in a script that is designed to be (indeed, _must_ be) a function, in a shell that has powerful and efficient regular expression capability builtin? 3. There are many, many such pushd/popd/dirs emulation function suites freely available. I have written one; DGK has written one; it seems that every third person in the world has written one. How does your version compare with these others? http://www.research.att.com/~dgk/ksh/fun/{pushd,popd,dirs} # NCA The "bash_pre_rc.ksh" function from the ksh93t distribution. My (unverified and very unformatted -- sorry!) list appears below: @,"",,http://www.dotfiles.com/files/21/285_cshrc,$0,"" @,"pushd/popd replacement.csh",2002-08-08,http://dotfiles.com/files/ 21/285_cshrc,$0,"","Michael Yuen" <my8192(a)yahoo.com> D,"You pushd me again and I will popd you one",,http://it.toolbox.com/ blogs/locutus/you-pushd-me-again-and-i-will-popd-you- one-29411,$0,"webpage","'Locutus'" D,"Tricks With cd",,http://gentoo-wiki.com/TIP_Tricks_with_cd, $0,"webpage" D,"WinXP popd/pushd",,http://www.microsoft.com/resources/documentation/ windows/xp/all/proddocs/en-us/popd.mspx?mfr=true D,"",,http://www.softpanorama.org/Scripting/Shellorama/ pushd_and_popd.shtml **A;D,"Advanced Unix Filesystem Navigation",,http:// www.softpanorama.org/Scripting/Shellorama/advanced_navigation.shtml,$0,"webpage","Dr. Nikolai Bezroukov" ^ http://www.softpanorama.org/Scripting/Shellorama/pushd_and_popd.shtml F,"cpd.zsh",,http://zsh.dotsrc.org/Contrib/scripts/users/wf/cpd, $0,"Change to Previous Directory -- menu-driven front end to the directory stack","Wolfgang Friebel" <friebel(a)ifh.de> F,"dirs.sh; popd.sh; pushd.sh",,http://www.freebsd.org/cgi/cvsweb.cgi/ src/bin/sh/funcs/ # dirs, kill, login, newgrp, popd, pushd, suspend "Chris Bertin, Kenneth Almquist" (BSD) "C. Bamford" <cjb(a)mtuxo.uucp> net.sources "Malcolm Dew-Jones" F,"pushd.rexx; popd.rexx; dirs.rexx",2.1,http://sonic.net/~roelofs/ code/pushd21.zip,$0,"directory stack","Greg Roelofs" <roe2(a)midway.uchicago.edu>; "Ric Anderson" <ric(a)cs.arizona.edu>; "Ken Neighbors" <wkn(a)leland.stanford.edu> F,"dirstack.mks.ksh",,ftp://ftp.uu.net/usenet/comp.sources.misc/ volume6/dirstack.mks,$0,"dirstack" F,"dirstack.csh",1987-08-29,<comp.sources.misc>/volume1/8709/15/;ftp:// ftp.uu.net/usenet/comp.sources.misc/volume1/8709/15.Z, $0,"dirstack","Rick Wargo" <rickers(a)RUTGERS.EDU@drexel.UUCP> F,"dirstack.sh",1987-09-29,<comp.sources.misc>/volume1/8709/15/;ftp:// ftp.uu.net/usenet/comp.sources.misc/volume1/8709/16.Z, $0,"dirstack","Rick Wargo" <rickers(a)RUTGERS.EDU@drexel.UUCP> F,"pushd-popd-ksh.ksh",,<simtel>/unix-c/utils/PUSHD-POPD- KSH.TAR.gz,"dirstack","Fletcher Mattox" <fletcher(a)im4u.uucp> F,"pushd-popd-dirs.ash",,http://www.freebsd.org/cgi/cvsweb.cgi/src/bin/ sh/funcs/pushd?rev=1.8&content-type=text/x-cvsweb-markup F,"cd-korn.ksh",,http://www.wuarchive.wustl.edu/pub/simtel/UNIX-C/DIR- MGMT/CD-KORN.KSH,$0,"dirstack: NCA","Fletcher Mattox" ^ wuarchive is now at http://ftp.wustl.edu/ F,"",,http://www.aspinc.com/download/Kshell.zip,$0,"dirstack: NCA","Anatole Olczak" D,"",,http://www.softpanorama.org/Articles/ slightly_skeptical_view_on_shell.shtml,$0,"","Dr. Nikolai Bezroukov" F,"dirs.ksh; popd.ksh; pushd.ksh",,http://src.opensolaris.org/source/ xref/onnv/onnv-gate/usr/src/lib/libshell/common/fun/{dirs,popd,pushd}, $0,"dirstack: component-of ksh93 source tree",DGK TUI;"shdir.c",1986-02-17,http://www.ugu.com/pub/unix/sec1/shdir.tar.Z, $0,"graphical frontend for directory stack","Paul Lew" ^ RM: display dirs output graphically with VT100 graphic chars. ^ Originally: <usenet>/mod.sources/*/volume7/issue95/*/dirstack.csh @,"zappath.sh",,http://www.grymoire.com/Unix/Scripts/Zappath.sh, $0,"shortens output of csh dirs builtin","Bruce Barnett" RM: ~/.cshdirs # (t)csh: file to save the directory stack in RM: "savedirs" and "dirsfile" shell variables that can be set to store the directory stack automatically on logout and restore it on login. RM: "dirstack" shell variable can be examined to see the directory stack and set to put arbitrary directories into the directory stack. RM: ~+1 = expand to dirstack entry #1 RM: ... the inability to use "~+digit" and "~-digit" shortcuts everywhere still is a drawback (cd ~+ can be imitated by cd -). ENV;LIB;"pushpopdirs.ksh",,http://www.kornshell.com/examples/ pushpopdirs,$0,"dirstack",DGK ^ from "Learning the Korn Shell" book example code. ENV;LIB;"mcd.ksh",,http://www.kornshell.com/examples/mcd,$0,"enhanced chdir" ENV;LIB;"kshpushpop.ksh",0.32;1999-12-22,http://www.vmunix.com/~gabor/ ksh/ksh-pushd-popd-0.32.tar.gz,$0,"directory stack","Ga'bor Egressy" <gabor(a)vmunix.com>
From: Chris F.A. Johnson on 11 Dec 2009 09:27 On 2009-12-11, bsh wrote: .... > Okay, so you are using ksh(1) (or bash(1)?) A few points then: > > 1. POSIX functions ( "foo()" ) are deprecated for robust scripting. Only if you are using ksh. I never use anything but the POSIX syntax. -- Chris F.A. Johnson, author <http://shell.cfajohnson.com/> =================================================================== Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress) ===== My code in this post, if any, assumes the POSIX locale ===== ===== and is released under the GNU General Public Licence =====
From: Rakesh Sharma on 11 Dec 2009 13:17
On Dec 11, 7:59 am, bsh <brian_hi...(a)rocketmail.com> wrote: > On Dec 4, 10:08 am, Rakesh Sharma <sharma...(a)hotmail.com> wrote: > > > On Dec 4, 10:40 pm, Parmenides <mobile.parmeni...(a)gmail.com> wrote: > > > ... > > In the below-mentioned line we cannot simply swap && with -a. > > Why? "-a" within at least the "[[ ... ]]" test condition is > equivalent to "&&" and will be decomposed to "[[ ... ]] > && [[ ... ]] ...", as seen in xtraced diagnostic output. > > > if [ $dirname = $PWD ] && [ -z "$DIR_STACK" ]; then <------- > > should be written as: > > if [ \( "x${dirname}" = "x${PWD}" \) -a \( -z "${DIR_STACK}" \) ]; > > Indeed, the POSIX deprecates the use of interstital -o > and -a in preference of && and || to obviate some pathological > input cases! See my post at: > > http://groups.google.com/group/comp.unix.shell/msg/27ed00e545a0a292 > > ^ firstdir=${DIR_STACK%% *} > > Okay, so you are using ksh(1) (or bash(1)?) A few points then: > > 1. POSIX functions ( "foo()" ) are deprecated for robust scripting. > Use kornshell syntax ("function foo") instead. > > 2. Why is grep(1) _ever_ appearing in a script that is designed > to be (indeed, _must_ be) a function, in a shell that has powerful > and efficient regular expression capability builtin? > > 3. There are many, many such pushd/popd/dirs emulation > function suites freely available. I have written one; DGK has > written one; it seems that every third person in the world has > written one. How does your version compare with these others? > > http://www.research.att.com/~dgk/ksh/fun/{pushd,popd,dirs} # NCA > The "bash_pre_rc.ksh" function from the ksh93t distribution. > > My (unverified and very unformatted -- sorry!) list appears below: > > @,"",,http://www.dotfiles.com/files/21/285_cshrc,$0,"" > @,"pushd/popd replacement.csh",2002-08-08,http://dotfiles.com/files/ > 21/285_cshrc,$0,"","Michael Yuen" <my8...(a)yahoo.com> > D,"You pushd me again and I will popd you one",,http://it.toolbox.com/ > blogs/locutus/you-pushd-me-again-and-i-will-popd-you- > one-29411,$0,"webpage","'Locutus'" > D,"Tricks With cd",,http://gentoo-wiki.com/TIP_Tricks_with_cd, > $0,"webpage" > D,"WinXP popd/pushd",,http://www.microsoft.com/resources/documentation/ > windows/xp/all/proddocs/en-us/popd.mspx?mfr=true > D,"",,http://www.softpanorama.org/Scripting/Shellorama/ > pushd_and_popd.shtml > **A;D,"Advanced Unix Filesystem Navigation",,http://www.softpanorama.org/Scripting/Shellorama/advanced_navigation.shtml,$0,"webpage","Dr. > Nikolai Bezroukov" > ^http://www.softpanorama.org/Scripting/Shellorama/pushd_and_popd.shtml > F,"cpd.zsh",,http://zsh.dotsrc.org/Contrib/scripts/users/wf/cpd, > $0,"Change to Previous Directory -- menu-driven front end to the > directory stack","Wolfgang Friebel" <frie...(a)ifh.de> > F,"dirs.sh; popd.sh; pushd.sh",,http://www.freebsd.org/cgi/cvsweb.cgi/ > src/bin/sh/funcs/ # dirs, kill, login, newgrp, popd, pushd, suspend > "Chris Bertin, Kenneth Almquist" (BSD) > "C. Bamford" <c...(a)mtuxo.uucp> net.sources > "Malcolm Dew-Jones" > F,"pushd.rexx; popd.rexx; dirs.rexx",2.1,http://sonic.net/~roelofs/ > code/pushd21.zip,$0,"directory stack","Greg Roelofs" > <r...(a)midway.uchicago.edu>; "Ric Anderson" <r...(a)cs.arizona.edu>; "Ken > Neighbors" <w...(a)leland.stanford.edu> > F,"dirstack.mks.ksh",,ftp://ftp.uu.net/usenet/comp.sources.misc/ > volume6/dirstack.mks,$0,"dirstack" > F,"dirstack.csh",1987-08-29,<comp.sources.misc>/volume1/8709/15/;ftp:// > ftp.uu.net/usenet/comp.sources.misc/volume1/8709/15.Z, > $0,"dirstack","Rick Wargo" <rick...(a)RUTGERS.EDU@drexel.UUCP> > F,"dirstack.sh",1987-09-29,<comp.sources.misc>/volume1/8709/15/;ftp:// > ftp.uu.net/usenet/comp.sources.misc/volume1/8709/16.Z, > $0,"dirstack","Rick Wargo" <rick...(a)RUTGERS.EDU@drexel.UUCP> > F,"pushd-popd-ksh.ksh",,<simtel>/unix-c/utils/PUSHD-POPD- > KSH.TAR.gz,"dirstack","Fletcher Mattox" <fletc...(a)im4u.uucp> > F,"pushd-popd-dirs.ash",,http://www.freebsd.org/cgi/cvsweb.cgi/src/bin/ > sh/funcs/pushd?rev=1.8&content-type=text/x-cvsweb-markup > F,"cd-korn.ksh",,http://www.wuarchive.wustl.edu/pub/simtel/UNIX-C/DIR- > MGMT/CD-KORN.KSH,$0,"dirstack: NCA","Fletcher Mattox" > ^ wuarchive is now athttp://ftp.wustl.edu/ > F,"",,http://www.aspinc.com/download/Kshell.zip,$0,"dirstack: > NCA","Anatole Olczak" > D,"",,http://www.softpanorama.org/Articles/ > slightly_skeptical_view_on_shell.shtml,$0,"","Dr. Nikolai Bezroukov" > F,"dirs.ksh; popd.ksh; pushd.ksh",,http://src.opensolaris.org/source/ > xref/onnv/onnv-gate/usr/src/lib/libshell/common/fun/{dirs,popd,pushd}, > $0,"dirstack: component-of ksh93 source tree",DGK > TUI;"shdir.c",1986-02-17,http://www.ugu.com/pub/unix/sec1/shdir.tar.Z, > $0,"graphical frontend for directory stack","Paul Lew" > ^ RM: display dirs output graphically with VT100 graphic chars. > ^ Originally: <usenet>/mod.sources/*/volume7/issue95/*/dirstack.csh > @,"zappath.sh",,http://www.grymoire.com/Unix/Scripts/Zappath.sh, > $0,"shortens output of csh dirs builtin","Bruce Barnett" > RM: ~/.cshdirs # (t)csh: file to save the directory stack in > RM: "savedirs" and "dirsfile" shell variables that can be set to store > the directory stack automatically on logout and restore it on login. > RM: "dirstack" shell variable can be examined to see the directory > stack and set to put arbitrary directories into the directory stack. > RM: ~+1 = expand to dirstack entry #1 > RM: ... the inability to use "~+digit" and "~-digit" shortcuts > everywhere still is a drawback (cd ~+ can be imitated by cd -). > ENV;LIB;"pushpopdirs.ksh",,http://www.kornshell.com/examples/ > pushpopdirs,$0,"dirstack",DGK > ^ from "Learning the Korn Shell" book example code. > ENV;LIB;"mcd.ksh",,http://www.kornshell.com/examples/mcd,$0,"enhanced > chdir" > ENV;LIB;"kshpushpop.ksh",0.32;1999-12-22,http://www.vmunix.com/~gabor/ > ksh/ksh-pushd-popd-0.32.tar.gz,$0,"directory stack","Ga'bor Egressy" > <ga...(a)vmunix.com> >> In the below-mentioned line we cannot simply swap && with -a. >Why? "-a" within at least the "[[ ... ]]" test condition is >equivalent to "&&" and will be decomposed to "[[ ... ]] >&& [[ ... ]] ...", as seen in xtraced diagnostic output. Why? because they are syntax in error that's why. e.g., if you replace a '&&' with '-a' in the if [ $dirname = $PWD ] && [ -z "$DIR_STACK" ]; then you get if [ $dirname = $PWD ] -a [ -z "$DIR_STACK" ]; then which is not a valid shell syntax. > if [ $dirname = $PWD ] && [ -z "$DIR_STACK" ]; then <------- > should be written as: > if [ \( "x${dirname}" = "x${PWD}" \) -a \( -z "${DIR_STACK}" \) ]; Enclosing the arguments in quotes, prepending with an "x", & enclosing \(...\) will ensure that no arguments will trip the expression evaluation. --Rakesh |