Visit Our UNIX and Linux User Community


cd then ls -l


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cd then ls -l
# 1  
Old 01-13-2005
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.
# 2  
Old 01-13-2005
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
# 3  
Old 01-14-2005
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
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.
# 5  
Old 01-18-2005
. ./yourscript.sh
# 6  
Old 05-25-2005
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

Previous Thread | Next Thread
Test Your Knowledge in Computers #244
Difficulty: Medium
Networking research in the early 1970s by Robert E. Kahn and Vint Cerf let to the formulation of the Transmission Control Program (TCP), and its specification in December 1974 in RFC 699.
True or False?

Featured Tech Videos