LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-26-2012, 08:44 PM   #1
linuxcoder
LQ Newbie
 
Registered: Jul 2012
Posts: 9

Rep: Reputation: Disabled
pushd and popd emulator


Some shells don't have pushd and popd so I wrote this to emulate the functionality. Naturally, if your scripts are stored somewhere other than ~/scripts, you'd change the pushd alias to reflect that.

I've added the ability to list the pushed directory. Can push any directory from anywhere.

In your .profile, add the following lines:
popd='cd `cat $HOME/.pushd`'
pushd=$HOME/scripts/pushd.sh
Then save this file to wherever you keep your shell scripts as pushd.sh (as above):

Code:
PUSHDIR=$1
PUSHFILE=$HOME/.pushd

# if -l is passed as a parameter, list what the current pushed directory points to
if [ "$PUSHDIR" = "-l" ]; then
  if [ -f $HOME/.pushd ]; then
    cat $PUSHFILE
  fi

  exit
fi

if [ "$PUSHDIR" != "" ]; then
  if [ "$PUSHDIR" = "." ]; then
    PUSHDIR=`pwd`
  fi

  if [ -d $PUSHDIR ]; then
    PUSH=$PUSHDIR
  else
    PUSH=""
  fi
else
  PUSH=`pwd`
fi

echo $PUSH > $PUSHFILE
 
Old 10-28-2012, 07:25 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
It appears that you have not used pushd and popd extensively in your experiences. Your script does not permit one to use pushd repeatedly, unlike the real mccoy. It needs to support a "stack" of directories, not just one.

In other words, something like this should be supported:
Code:
pushd /tmp
pushd /usr/include
pushd /usr/lib
popd             # returns user to /usr/include
popd             # returns user to /tmp
popd             # returns user to starting directory
Also your script also needs to go beyond merely checking if the destination is a directory. It also needs to check whether the user has permission to navigate to the destination folder.

Lastly, I do not see anywhere in your script that 'cd' is called to actually change directories to the (valid) destination directory.
 
Old 10-28-2012, 09:10 AM   #3
linuxcoder
LQ Newbie
 
Registered: Jul 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dwhitney67 View Post
It appears that you have not used pushd and popd extensively in your experiences. Your script does not permit one to use pushd repeatedly, unlike the real mccoy. It needs to support a "stack" of directories, not just one.
That's an incredibly bad assumption. I toyed with the idea of stacks, but for my purposes decided to stick with the single level version. Something to fill the gap between not having the functionality and the csh version, which by the way, the source code is readily available to those who want to compile that. As with all the code here, anyone is free to take the code and expand it to suit their purposes. That's kind of the whole point.

You do have a valid point about the permissions, though.

Quote:
Lastly, I do not see anywhere in your script that 'cd' is called to actually change directories to the (valid) destination directory.
That's because that's not in the script, nor is it in the actual pushd code.
 
Old 10-28-2012, 10:05 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by linuxcoder View Post
I toyed with the idea of stacks, but for my purposes decided to stick with the single level version.
Then try using 'cd <dest>' and then 'cd $OLDPWD'.

Of course, the notion of $OLDPWD may not be supported by your shell.

But please, I've had a piss poor week of translating a shell script into batch script (for Windoze), and although I hated every moment of it, it's incredibly hard to believe that you gave up so damn quickly with yours. Btw, take this script and augment it to your liking... to do whatever you please.

Code:
#!/bin/bash

Last edited by dwhitney67; 10-28-2012 at 10:08 AM.
 
Old 10-28-2012, 05:16 PM   #5
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 547

Rep: Reputation: 115Reputation: 115
Quote:
Originally Posted by linuxcoder View Post
That's an incredibly bad assumption. I toyed with the idea of stacks, but for my purposes decided to stick with the single level version. Something to fill the gap between not having the functionality and the csh version, which by the way, the source code is readily available to those who want to compile that. As with all the code here, anyone is free to take the code and expand it to suit their purposes. That's kind of the whole point.
With all due respect, the whole point of pushd and popd is to have the ability to push multiple directories onto the stack then pop them off. Otherwise, one would be far easier just typing something like "x=$(pwd)" and "cd x". And if you're working at a single level then the chances are you're just working on two directories, in which case you can use "cd -".
 
Old 10-28-2012, 05:54 PM   #6
linuxcoder
LQ Newbie
 
Registered: Jul 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by devnull10 View Post
With all due respect, the whole point of pushd and popd is to have the ability to push multiple directories onto the stack then pop them off. Otherwise, one would be far easier just typing something like "x=$(pwd)" and "cd x". And if you're working at a single level then the chances are you're just working on two directories, in which case you can use "cd -".
With all due respect, the script does exactly what I need it to do or I would have written something more extensive or compiled the C code. Thought I'd post a quick and simple utility. Didn't realize it was crap all over the poster day.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
go: a cd/pushd/popd/dirs replacement JurgyMan AIX 9 05-22-2011 07:50 PM
go: a cd/pushd/popd/dirs replacement JurgyMan Programming 1 05-18-2011 04:06 AM
[SOLVED] tcsh: pushd + popd in one function lucmove Programming 1 07-03-2010 07:56 PM
pushd &popd itsmesee Linux - Newbie 8 12-03-2007 12:10 AM
pushd popd dirs christianunix Linux - Newbie 1 10-26-2007 11:10 PM


All times are GMT -5. The time now is 01:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration