LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 07-03-2010, 07:01 PM   #1
lucmove
Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian Wheezy, Slackware
Posts: 604

Rep: Reputation: 70
tcsh: pushd + popd in one function


I really like pushd and popd, but I don't like their long spellings, so I made aliases: 'x' runs pushd and 'xx' runs popd. That's not bad, but I wish I could improve them in the following way:

x => If followed by a path, 'pushd' (and cd to) that path. If not given any arguments, run 'popd'.

xx => 'cd -'

The problem is implementing that pushd/popd in one command trick. Since tcsh does not have functions, I've been struggling hard to come with a solution. I made a script, and it works, but it only works within the context of the script. The script exits and not only am I still sitting in the same directory, but also my dirstack is emptied.

A similar problem: make 'cdd'. I remember using that program in DOS a long time ago. I would run 'cdd /path/to/directory' and cd to that path if it existed, or create it and cd to it if it didn't exist. Again, I made a script, but that only works within the context of the script, not my current shell. So the new dir is created, but my shell won't cd into it.

Help, please?
 
Old 07-03-2010, 07:56 PM   #2
lucmove
Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian Wheezy, Slackware
Posts: 604

Original Poster
Rep: Reputation: 70
Thumbs up Solved

Ok, I found the solution on my own. I was making the following mistake in my .tcshrc:

Code:
foreach i (`\ls -1 ~/myscripts`)
	eval alias `basename "$i" .sh` "~/myscripts/$i"
end
That was causing each script to be executed as a command. The right (and rather arcane!) way to make it work is to use 'source':

Code:
foreach i (`\ls -1 ~/myscripts`)
	eval alias `basename "$i" .sh` "source ~/myscripts/$i"
end
Now it works. And here is the complete solution in case someone ever wants to use the idea:

in .tcshrc:
Code:
foreach i (`\ls -1 ~/myscripts`)
	eval alias `basename "$i" .sh` "source ~/myscripts/$i"
end

# or just make one specific alias:
# alias x "source ~/myscripts/x.sh"

alias xx 'cd - '
in ~/myscripts/x.sh:

Code:
#!/usr/bin/env tcsh

if 	( $#argv < 1 ) then
	popd
	exit
endif

if 	( -e "$1" ) then
	pushd "$1"
else
	echo "$1 not found."
endif
Note: I like the whole idea better with this line in .tcshrc:

Code:
alias cd 'pushd '
And a little bit of complete magic:

Code:
complete {cd,popd,pushd,x}	'n/*/d/'

Last edited by lucmove; 07-03-2010 at 08:01 PM.
 
  


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
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 06:00 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
puhsd, popd implementation Rudess Linux - General 1 03-12-2007 07:37 PM
Tab key function on tcsh shell and redhat 8.0 jeff_lim1974 Linux - General 0 06-21-2004 05:43 AM


All times are GMT -5. The time now is 04:32 AM.

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