LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 04-26-2006, 04:07 AM   #1
c19h28O2
LQ Newbie
 
Registered: Apr 2006
Location: London
Posts: 29

Rep: Reputation: 15
Run script each time cd is used


Hi,

Is there a way to automatically run a script each time i use the cd command?

Thanks

C19
 
Old 04-26-2006, 04:12 AM   #2
uselpa
Senior Member
 
Registered: Oct 2004
Location: Luxemburg
Distribution: Slackware, OS X
Posts: 1,507

Rep: Reputation: 47
Not directly, but you could create your own cd command which executes your script and then does the original cd. You can achieve the same with a bash alias.
 
Old 04-26-2006, 04:24 AM   #3
c19h28O2
LQ Newbie
 
Registered: Apr 2006
Location: London
Posts: 29

Original Poster
Rep: Reputation: 15
Hi,

So if I creat an alias in my .profile could i call the script from there? how would I do that?
 
Old 04-26-2006, 04:28 AM   #4
uselpa
Senior Member
 
Registered: Oct 2004
Location: Luxemburg
Distribution: Slackware, OS X
Posts: 1,507

Rep: Reputation: 47
Try something like this, substitution the echo for your own command:
Code:
pu@slackw:~$ alias
alias d='dir'
alias dir='/usr/bin/ls $LS_OPTIONS --format=vertical'
alias ls='/usr/bin/ls $LS_OPTIONS'
alias mc='. /usr/share/mc/bin/mc-wrapper.sh'
alias v='vdir'
alias vdir='/usr/bin/ls $LS_OPTIONS --format=long'
pu@slackw:~$ alias cd='echo "my script";cd'
pu@slackw:~$ cd projects/
my script
pu@slackw:~/projects$
 
Old 04-26-2006, 08:08 AM   #5
c19h28O2
LQ Newbie
 
Registered: Apr 2006
Location: London
Posts: 29

Original Poster
Rep: Reputation: 15
Hey uselpa,

Thanks for that I managed to sort it out.

I have another question, I would like to create an alias that when i cd it automatically runs ls?

Thanks

C19
 
Old 04-26-2006, 08:27 AM   #6
uselpa
Senior Member
 
Registered: Oct 2004
Location: Luxemburg
Distribution: Slackware, OS X
Posts: 1,507

Rep: Reputation: 47
Then it's easier to use functions:
Code:
pu@slackw:~$ function cd() { builtin cd "$*" ; ls ; }
pu@slackw:~$ cd /
bin/  boot/  build/  dev/  etc/  home/  lib/  mnt/  opt/  proc/  root/  sbin/  sys/  tmp/  usr/  var/
pu@slackw:/$
 
Old 04-26-2006, 08:41 AM   #7
c19h28O2
LQ Newbie
 
Registered: Apr 2006
Location: London
Posts: 29

Original Poster
Rep: Reputation: 15
Thanks

but i've pasted your code in my .profile run it again with . .profile and i get this error?

ksh: syntax error: `(' unexpected
 
Old 04-26-2006, 09:58 AM   #8
uselpa
Senior Member
 
Registered: Oct 2004
Location: Luxemburg
Distribution: Slackware, OS X
Posts: 1,507

Rep: Reputation: 47
Which shell are you using? bash or ksh? Which distro are you using?
My example is based on bash, which is the standard shell on Linux.
 
Old 04-27-2006, 04:42 AM   #9
c19h28O2
LQ Newbie
 
Registered: Apr 2006
Location: London
Posts: 29

Original Poster
Rep: Reputation: 15
Hi,

I'm using ksh and i'm using a flavour of unix svr4

Cheers

C19
 
Old 04-27-2006, 07:38 AM   #10
uselpa
Senior Member
 
Registered: Oct 2004
Location: Luxemburg
Distribution: Slackware, OS X
Posts: 1,507

Rep: Reputation: 47
I haven't used ksh so I can't help you there. But be aware that in the Linux world people will assume that you use bash because that's the default shell for Linux (at least for all the distros I've installed in the past). So when you post a shell question to this (mainly Linux) forum, please specify that you use Korn Shell.
 
Old 04-28-2006, 04:01 AM   #11
c19h28O2
LQ Newbie
 
Registered: Apr 2006
Location: London
Posts: 29

Original Poster
Rep: Reputation: 15
Hi,

I thought ksh meant Korn Shell?
 
Old 04-28-2006, 04:39 AM   #12
uselpa
Senior Member
 
Registered: Oct 2004
Location: Luxemburg
Distribution: Slackware, OS X
Posts: 1,507

Rep: Reputation: 47
Yes, ksh is Korn Shell. But you didn't say that you used ksh, that's my point.
 
Old 05-02-2006, 09:36 AM   #13
c19h28O2
LQ Newbie
 
Registered: Apr 2006
Location: London
Posts: 29

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by c19h28O2
Hi,

I'm using ksh and i'm using a flavour of unix svr4

Cheers

C19
I answered your original question no need to be so rude...
 
Old 05-02-2006, 09:59 AM   #14
uselpa
Senior Member
 
Registered: Oct 2004
Location: Luxemburg
Distribution: Slackware, OS X
Posts: 1,507

Rep: Reputation: 47
I wasn't being rude. Just pointing out that if you ask questions related to shell in a Linux forum, you need to specify that you use ksh right from the start.
 
Old 05-02-2006, 04:19 PM   #15
c19h28O2
LQ Newbie
 
Registered: Apr 2006
Location: London
Posts: 29

Original Poster
Rep: Reputation: 15
Cool, no probs

Cheers
 
  


Reply



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
How run script after certain idle time? nio Linux - Software 2 07-09-2005 02:05 PM
on Network Up Script run? On Battery power run script? v2-ncl Linux - General 0 12-08-2003 09:34 AM
RH8 - how to automatically run a shell script at boot time nu-B Linux - Newbie 2 11-03-2003 02:47 PM
RH8 - how to automatically run iptables rules shell script at boot time nu-B Linux - General 1 10-29-2003 08:38 PM
i want to run a script or program at boot time!! FreakboY Linux - Newbie 9 10-12-2003 08:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:40 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
Open Source Consulting | Domain Registration