pushd and popd


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers pushd and popd
# 8  
Old 05-12-2010
I know they already exist -- I'm learning about shell scripts right now and the book gives these as an example. I just wanted to know out of curiosity why the popd function isn't working they way it should.
# 9  
Old 05-12-2010
Quote:
Originally Posted by Straitsfan
I know they already exist -- I'm learning about shell scripts right now and the book gives these as an example. I just wanted to know out of curiosity why the popd function isn't working they way it should.
I just noticed:

Code:
pushd ()
{ 
      dirname="$1"
      DIRSTACK="$dirname ${DIRSTACK:-$PWD' '}"
      cd ${dirname:?"missing directory name."}
      echo $DIRSTACK"
}

popd ()
{
     DIRSTACK=${DIRSTACK#* }
     cd ${DIRSTACK%% *}
     echo "$PWD"
}

missing a brace.
# 10  
Old 05-13-2010
That was an older version of the code here's the newer one:

Code:
DIRSTACK=""
export DIRSTACK
pushd ()
{ 
      dirname=$1
      echo the value of dirname is "$@"
      if   cd ${dirname:?"missing directory name."} #if cd was successful
      then
           DIRSTACK="$dirname ${DIRSTACK:-$PWD' '}"
           echo "$DIRSTACK"
      else
           echo still in $PWD
      fi
}

popd ()
{
     DIRSTACK=${DIRSTACK#* }
     cd ${DIRSTACK%% *}
     echo "$PWD"
}

But it still doesn't work right.
# 11  
Old 05-14-2010
Quote:
Originally Posted by Straitsfan
But it still doesn't work right.
Overriding the function of a shell builtin may itself not work properly. Also, the DIRSTACK variable already exists in bash -- as an array, not a flat string. You really should try them in a shell that doesn't have pushd and popd as builtins, like ksh, or busybox/ash.

Also:
Code:
${DIRSTACK:-$PWD' '}

Is this literally as the code has it? That seems odd. When I remove the ' ' it works in busybox ash shell.

Last edited by Corona688; 05-14-2010 at 01:14 PM..
# 12  
Old 05-15-2010
Okay, I got a head of myself with this. I thought the DIR_STACK was a typo in my book and assumed it mean DIRSTACK, but it's not. So I'm starting over again and here's the code from the book, which I put in my .profile. The purpose of this excercise is to implement a stack as an environment variable containing a list of directories separated by spaces.

Code:
DIR_STACK=""
export DIR_STACK

pushd ()
{
   dirname=$1
   DIR_STACK="$dirname ${DIR_STACK:-$PWD' '}"
   cd ${dirname:?"missing directory name."}
   echo "$DIR_STACK"
}

popd ()
{
    DIR_STACK=${DIR_STACK#* }
    cd ${DIR_STACK%% *}
    echo "$PWD"
}

Now, the book said to put the DIR_STACK="" and export statement into my .bass_profile file which I don't have -- or, if I'm correct it is in fact my .profile file (which is one of the three files UNIX reads, according to the book, when you log in -- as that the same as exiting and opening a new window?)

So, I've copied the code and tried it out -- for example, I tried pushd to my Mail directory in my home directory. This is the result:

Code:
Mail/ /Users/chrislandalusa' '

why does it include the ' '?

if I try popd I get this result:

Code:
-bash: cd: /Users/chrislandalusa': No such file or directory
/Users/chrislandalusa/Mail

I don't need to specify the directory I want to pop, correct?

If I understand the code correctly, the first time I pushd, since the value of DIR_STACK is null, it prints PWD and the two single quotes, preceeded by the name of the directory that follows pushd.
But then, when I try another directory with pushd, like the inbox directory inside the mail directory, the mail directory is gone:

Code:
pushd inbox/
inbox/ /Users/chrislandalusa' '

Where did the Mail directory go?
And if I try popd I'll get a similar message

Even though there are deficiencies in the code for now (which the book says there are), I'd like some input as to what's going on -- maybe a step by step explanation of each line -- it may well be that these problems are inherent in the code and will be addressed later in the book.

---------- Post updated at 05:08 PM ---------- Previous update was at 04:55 PM ----------

Okay, I just tried something and changed the ' ' to " " and it worked.

So was that a typo in the book? what do single quotes do as opposed to doube quotes?

or is it supposed to be single quotes and I'm missing something?

---------- Post updated at 05:25 PM ---------- Previous update was at 05:08 PM ----------

another update

I figured out I'm getting the "no such directory error" because there are spaces between the directory names.

---------- Post updated at 07:27 PM ---------- Previous update was at 05:25 PM ----------

Corona --

Yes, that's what's in the book, but I changed them to " " and it worked (I posted this about a couple of hours ago as I write this). I'm guessing that it was a typo. Take a look at my most recent post and tell me what you think about my most recent discoveries about this and tell me what you think.

The book is about the bash shell, by the way.

Last edited by Scott; 05-15-2010 at 07:15 AM.. Reason: Fixed code tag
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Pushd popd

Howdy, I'm working through the book LEARN PYTHON THE HARD WAY, Appendix: Command Line Crash Course. I got to wondering if pushd and popd are really ever used all that much? Thank you for your insights, DN (4 Replies)
Discussion started by: danuke
4 Replies

2. Shell Programming and Scripting

Pushd Popd and Dirs History?

Hello everyone, finally made these commands working, but I don't know how ti implement the history command for this utility? Anyone? Please please (28 Replies)
Discussion started by: iennetastic
28 Replies

3. UNIX for Dummies Questions & Answers

Shell script for for pushd function

I'm looking at a script for a pushd function in my bash book: DIRSTACK="" export DIRSTACK pushd () { dirname=$1 DIRSTACK="$dirname ${DIRSTACK:-$PWD' '}" cd ${dirname:?"missing directory name."} echo "$DIRSTACK" } Wanted to ask if someone could explain what's... (0 Replies)
Discussion started by: Straitsfan
0 Replies

4. UNIX for Dummies Questions & Answers

Pushd commands features

Hi All, im using pushd and popd as below , but im getting different results, it is not consistent. pushd <DIR> pkzip <ZIP FILE NAME> * popd echo " Hi" it is going into the DIR as expected and while zipping it, in between it got suspended by itself and executed the remaining step... (1 Reply)
Discussion started by: rithu
1 Replies

5. UNIX for Dummies Questions & Answers

pushd and popd

This question is related to pushd and popd. After I have pushed directories, I would like to see what are the contents of the stack currently before I execut e popd. The reason is that many a times we forget (or can get confused when multiple shells are open) what we pushed and would like to first... (4 Replies)
Discussion started by: sharanbr
4 Replies

6. Shell Programming and Scripting

pushd popd

Are there any arguments for 'pushd' and 'popd' so that it wont output the whole stack to the screen everytime i call the commands? (1 Reply)
Discussion started by: owijust
1 Replies

7. UNIX for Dummies Questions & Answers

pushd/popd missing on SGI irix 6.5.11

I know that pushd/popd are built into csh, but I'm trying to run a set of 3rd party make files that use sh. My problem is that sh simply squawks that pushd and popd are not found, then the make dies due to the error. I've searched the entire system, and they are indeed missing. Evidently SGI... (3 Replies)
Discussion started by: jbalster
3 Replies

8. UNIX for Dummies Questions & Answers

pushd?

Hello Can someone tell me difference between the command cd an pushd? I dont get it! :( Greets Marcus (3 Replies)
Discussion started by: Fwurm
3 Replies
Login or Register to Ask a Question