LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Run script each time cd is used (https://www.linuxquestions.org/questions/linux-newbie-8/run-script-each-time-cd-is-used-438917/)

c19h28O2 04-26-2006 04:07 AM

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

uselpa 04-26-2006 04:12 AM

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.

c19h28O2 04-26-2006 04:24 AM

Hi,

So if I creat an alias in my .profile could i call the script from there? how would I do that?

uselpa 04-26-2006 04:28 AM

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$


c19h28O2 04-26-2006 08:08 AM

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

uselpa 04-26-2006 08:27 AM

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:/$


c19h28O2 04-26-2006 08:41 AM

Thanks

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

ksh: syntax error: `(' unexpected

uselpa 04-26-2006 09:58 AM

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.

c19h28O2 04-27-2006 04:42 AM

Hi,

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

Cheers

C19

uselpa 04-27-2006 07:38 AM

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.

c19h28O2 04-28-2006 04:01 AM

Hi,

I thought ksh meant Korn Shell?

uselpa 04-28-2006 04:39 AM

Yes, ksh is Korn Shell. But you didn't say that you used ksh, that's my point.

c19h28O2 05-02-2006 09:36 AM

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...

uselpa 05-02-2006 09:59 AM

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.

c19h28O2 05-02-2006 04:19 PM

Cool, no probs

Cheers


All times are GMT -5. The time now is 09:39 PM.