Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

cd then ls -l

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-13-2005   -   Original Discussion by rein
rein's Unix or Linux Image
rein rein is offline
Registered User
 
Join Date: Dec 2004
Last Activity: 31 January 2011, 2:32 PM EST
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 Unix and Linux 01-13-2005   -   Original Discussion by rein
zazzybob's Unix or Linux Image
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 2 October 2017, 12:19 PM EDT
Location: /dev/null
Posts: 2,187
Thanks: 3
Thanked 25 Times in 23 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 Unix and Linux 01-14-2005   -   Original Discussion by rein
bhargav's Unix or Linux Image
bhargav bhargav is offline Forum Advisor  
Registered User
 
Join Date: Sep 2004
Last Activity: 22 January 2016, 4:39 PM EST
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 Unix and Linux 01-14-2005   -   Original Discussion by rein
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Advisor  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 26 February 2016, 12:31 PM EST
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 63
Thanked 473 Times in 272 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 Unix and Linux 01-18-2005   -   Original Discussion by rein
bugbugbug's Unix or Linux Image
bugbugbug bugbugbug is offline
Registered User
 
Join Date: Jan 2005
Last Activity: 10 May 2006, 12:05 PM EDT
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
. ./yourscript.sh
Sponsored Links
    #6  
Old Unix and Linux 05-25-2005   -   Original Discussion by rein
rein's Unix or Linux Image
rein rein is offline
Registered User
 
Join Date: Dec 2004
Last Activity: 31 January 2011, 2:32 PM EST
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 Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



All times are GMT -4. The time now is 09:29 PM.