Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-13-2005
Registered User
 

Join Date: Dec 2004
Location: Zürich
Posts: 151
Thanks: 0
Thanked 1 Time in 1 Post
cd then ls -l

Hi,

I'm trying to create a little script:

if [ -d $1 ]
then
cd $1
ls -l $1
fi

so I can make a new command: cl that does both.

simple enough but I noticed that after executing I'm returned to my original location. How do I surpress that behaviour? I'm using bash-2 on HP-UX 11.

Thanks.
Sponsored Links
    #2  
Old 01-13-2005
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Thanks: 0
Thanked 7 Times in 7 Posts
That's because it's executed in a subshell.

You could add a function to .bashrc something like


Code:
cl()
{
   if [ -d "$1" ]; then
      cd "$1"
      ls -l
   fi
}

Cheers
ZB
Sponsored Links
    #3  
Old 01-14-2005
bhargav's Avatar
bhargav bhargav is offline Forum Advisor  
Registered User
 

Join Date: Sep 2004
Location: USA
Posts: 512
Thanks: 0
Thanked 2 Times in 2 Posts
Have a question here ....
See the following 2 aliases ....

alias cl="cd scripts ; ls -lrt"
alias cla="cd $1 ; ls -lrt"

Here is the concern ...
1)After running the first alias command 'cl' , the current directory is $HOME/scripts
2) After running the second alias command 'cla scripts' , the current directory is $HOME only.

For both alias commands , o/p is nothing but list of "scripts" directory.

Any ideas what's happening here .... ?
    #4  
Old 01-14-2005
Perderabo's Avatar
Unix Daemon (Adminstrator Emeritus)
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,679
Thanks: 26
Thanked 238 Times in 148 Posts
Quote:
Originally Posted by bhargav
alias cla="cd $1 ; ls -lrt"
That is the ksh syntax for an alias. But an alias in ksh cannot have parameters. That $1 will not be the parameter on the alias invocation. Use a function if you want parameters.
Sponsored Links
    #5  
Old 01-18-2005
Registered User
 

Join Date: Jan 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
. ./yourscript.sh
Sponsored Links
    #6  
Old 05-25-2005
Registered User
 

Join Date: Dec 2004
Location: Zürich
Posts: 151
Thanks: 0
Thanked 1 Time in 1 Post
the result, by the way

a bit late maybe but I thought I might post the result:

cl()
{
if [ -d $1 ]
then
cd $1
ls -la
else
cd $HOME
ls -la
fi
}

then

./cl.sh

in my .profile works very well on ksh and in bash
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



All times are GMT -4. The time now is 04:23 AM.