Register
It is currently Tue Sep 02, 2014 12:47 am

Merge 'cd' with 'ls'


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Mon Apr 11, 2011 10:14 am   

Joined: Mon Apr 11, 2011 10:08 am
Posts: 3
Hello!
I wanted to create a really simple script. Along with changing the working directory I wanted to see the directory's content.

At first I tried sth. like:

Code:
#!/bin/bash
# file dir.sh
cd $1
ls


But then I realised that every new proccess has a new working directory. So I've tried calling it like:
Code:
. ./dir.sh


And yes! It worked!
But to achieve perfect solution I want to create an alias for 'cd'. So I modified my .bashrc by adding a line:
Code:
alias cd=". ./dir.sh"


The result of 'cd' after reloading bash was an infinite loop and segmentation fault. Then I realised, that the command 'cd $1' in dir.sh was changed due to the alias on 'cd'. And here I'm stuck. Any hints?


Top
 Profile  
 PostPosted: Mon Apr 11, 2011 11:10 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
create a function in your .profile like so:
Code:
function lcd { cd "$@" && ls -l; }

then you can use 'lcd <directory>' to change directory and ls afterwards.
(don't forget to source your profile at least once, or open a new shell)


Top
 Profile  
 PostPosted: Mon Apr 11, 2011 3:16 pm   

Joined: Mon Apr 11, 2011 10:08 am
Posts: 3
Maybe I forgot to mention that this solution is not satisfying enaguh for me =)
I know that learning to use 'lcd' instead of 'cd' is not a big deal, but from a programming point of view I've been wondering if there is a nice method to somehow override 'cd'.


Top
 Profile  
 PostPosted: Mon Apr 11, 2011 10:28 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
This will be hard since 'cd' is a shell built-in command and not some binary on the system (like for example 'ls' is both).
So when 'cd' gets overloaded with new functionality and you use 'cd' in that overloaded function, you get what you already experienced, an infinite loop. If 'cd' would be some binary on the system, you could reference it with a full path (like /bin/cd or something) and bypass the same name. Unfortunately it isn't a binary.


Top
 Profile  
 PostPosted: Tue Apr 12, 2011 9:46 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Use
Code:
builtin cd
inside of the script. It will not call the alias then.


Top
 Profile  
 PostPosted: Tue Apr 12, 2011 3:51 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
That's a nice feature I wasn't aware of yet. Thanks!


Top
 Profile  
 PostPosted: Wed Apr 13, 2011 8:21 am   

Joined: Mon Apr 11, 2011 10:08 am
Posts: 3
Thanks! That's exactly what I desired.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP