LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 02-20-2013, 11:14 AM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 348

Rep: Reputation: 16
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
 
Old 02-20-2013, 11:34 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 870
Blog Entries: 2

Rep: Reputation: 219Reputation: 219Reputation: 219
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
 
1 members found this post helpful.
Old 02-20-2013, 11:48 AM   #3
casperdaghost
Member
 
Registered: Aug 2009
Posts: 348

Original Poster
Rep: Reputation: 16
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?
 
Old 02-20-2013, 12:21 PM   #4
casperdaghost
Member
 
Registered: Aug 2009
Posts: 348

Original Poster
Rep: Reputation: 16
Code:
casper03 bin $ more   /tmp/foo
#!/bin/bash
. /tmp/foo
pushd /var/ftp/
casper03 bin $ /tmp/foo
Segmentation fault
 
Old 02-20-2013, 01:17 PM   #5
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 870
Blog Entries: 2

Rep: Reputation: 219Reputation: 219Reputation: 219
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
 
1 members found this post helpful.
Old 02-20-2013, 01:36 PM   #6
casperdaghost
Member
 
Registered: Aug 2009
Posts: 348

Original Poster
Rep: Reputation: 16
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
 
Old 02-20-2013, 01:54 PM   #7
gdejonge
Member
 
Registered: Aug 2010
Location: Netherlands
Distribution: Kubuntu, Debian, Suse, Slackware
Posts: 311

Rep: Reputation: 71
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.
 
2 members found this post helpful.
Old 02-20-2013, 03:28 PM   #8
casperdaghost
Member
 
Registered: Aug 2009
Posts: 348

Original Poster
Rep: Reputation: 16
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.
 
  


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
pushd and popd emulator linuxcoder Programming 5 10-28-2012 05:54 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 11:09 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