LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   scripting pushd popd (https://www.linuxquestions.org/questions/linux-newbie-8/scripting-pushd-popd-4175450968/)

casperdaghost 02-20-2013 10:14 AM

scripting pushd popd
 
i have a script in my bin directory - so i could use it from any location
I want it to display certain group of files in a directory.
I want to use pushd to put this directory on top of the stack, in case I want to grep something out of the files without using the whole path from the directory that i am in.
I don't want to use cd because i want to use popd to go back to the previous working directoy.

However when i use the pushd in the script, it leaves me in the current working directory. the pushd command does nt put me into the directory that i want. after the script executes, i ma left in the directory that i am working from


Code:

#!/bin/bash

pushd /var/log
todays_session=$(cat /var/log/session.txt)
echo $todays_session
ls -ltr /var/log/ | grep $todays_session


casper bin $ tspin
/var/log ~/bin
02202013
-rw-r--r-- 1 root      root        21 Feb 20 06:21 02202013-map.txt
-rw-r--r-- 1 root      root    206349 Feb 20 06:21 02202013-accounts.txt
-rw-r--r-- 1 root      root      705 Feb 20 06:21 02202013-routers.txt
-rw-r--r-- 1 root      root  1843014 Feb 20 06:21 02202013-ips.txt
-rw-r--r-- 1 root      root      3250 Feb 20 06:21 02202013-gateways.txt


linosaurusroot 02-20-2013 10:34 AM

Your shell process before you run the script remains unchanged because the activity in the script is in a different shell process. It sounds as if you want to "source" the file in the current process which can be done with the dot symbol.

Code:

.  ~/bin/tspin

casperdaghost 02-20-2013 10:48 AM

I source the file at the beginning of the script and when I run it i get a segmentation fault.
Code:



casper03 bin $ more tspin
#!/bin/bash


. ~/bin/tspin

pushd /var/ftp/
todays_session=$(cat /var/ftp/master/noc/session.txt)
echo $todays_session
ls -ltr /var/ftp/master/ | grep $todays_session
echo "changed to  /var/ftp/master/"
echo "use popd to get to previous working directory "
caspert03 bin $ vim  tspin
casper03 bin $
casper03 bin $
casper03 bin $
csper03 bin $ tspin
Segmentation fault
casper03 bin $


i don't understand what sourcing is.
when i do a locate on the pushd to source it i get his file path.
when I sounrce this path i get an error.

casper@casper3 bin $ locate pushd
/usr/share/man/man1/pushd.1.gz


why did you source the directory that the script is in?

casperdaghost 02-20-2013 11:21 AM

Code:

casper03 bin $ more  /tmp/foo
#!/bin/bash
. /tmp/foo
pushd /var/ftp/
casper03 bin $ /tmp/foo
Segmentation fault


linosaurusroot 02-20-2013 12:17 PM

Code:

######  This file is ~/bin/tspin  --- whatever you do never put any text in here causing it ti call itself.
pushd /var/ftp/
todays_session=$(cat /var/ftp/master/noc/session.txt)
echo $todays_session
ls -ltr /var/ftp/master/ | grep $todays_session
echo "changed to  /var/ftp/master/"
echo "use popd to get to previous working directory "
## You now return to your shell.

then at your shell prompt
Code:

casper03 bin $ 
csper03 bin $      .  ~/bin/tspin


casperdaghost 02-20-2013 12:36 PM

ok that work - kinds defeats the usability - like why put it int he bin directory when i have to source it each time i use.
maybe I could alias it - or make another script that sources the files.


thank you very much

gdejonge 02-20-2013 12:54 PM

The way I usually change directories in shell scripts is with
Code:

olddir=`pwd`
cd /somewhere/somedir
...do a lot of things...
cd $olddir

pwd gives you the current directory. The advantage of this method is that no matter how much you change directories you allways get back to the directory you started in.

Also remember that cd "remembers" the last directory it was in and you can use 'cd -' to jump back.

casperdaghost 02-20-2013 02:28 PM

cd -

that is a really good idea.
that is actually what i was looking for - a way to pop in to a directory, make changes, and then pop back to where i was.

thank you very much.


All times are GMT -5. The time now is 02:50 PM.