Unix Shell - Simple bash script

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > May 2007 > Simple bash script





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Simple bash script
Deniz Dogan

2007-05-31, 7:21 am

Hello.
I am very new to bash scripting and was hoping you could help me out a
bit. I am trying to create a bash script (but I'd prefer an alias) which
changes the directory and then lists the files in that direcotry. This
is what I came up with:

#!/bin/sh
cd $1;
ls;

This does not work and I believe this is due to the script starting a
new "sh" shell and then changing the directory in THAT shell. How should
I do this?

--
Deniz Dogan
Teo

2007-05-31, 7:21 am

Dear Deniz,

On May 31, 11:10 am, Deniz Dogan <kristn...@nospam.com> wrote:
> I am very new to bash scripting and was hoping you could help me out a
> bit. I am trying to create a bash script (but I'd prefer an alias) which
> changes the directory and then lists the files in that direcotry. This
> is what I came up with:
>
> #!/bin/sh
> cd $1;
> ls;
>
> This does not work and I believe this is due to the script starting a
> new "sh" shell and then changing the directory in THAT shell. How should
> I do this?


Bash aliases do not support parameters (i.e., the $1). You could use a
function instead:

$ function cdls {
if [ -d $1 ] ; then
cd $1;
ls;
else
echo "Error: '$1' is not a directory" 1>&2 ;
fi
}
$ cdls foo
Error: 'foo' is not a directory
$ cdls doc
[snip]

see bash(1) in the FUNCTIONS section for more details

Cheers,

Matteo

Deniz Dogan

2007-05-31, 1:21 pm

Teo wrote:
> Dear Deniz,
>
> On May 31, 11:10 am, Deniz Dogan <kristn...@nospam.com> wrote:
>
> Bash aliases do not support parameters (i.e., the $1). You could use a
> function instead:
>
> $ function cdls {
> if [ -d $1 ] ; then
> cd $1;
> ls;
> else
> echo "Error: '$1' is not a directory" 1>&2 ;
> fi
> }
> $ cdls foo
> Error: 'foo' is not a directory
> $ cdls doc
> [snip]
>
> see bash(1) in the FUNCTIONS section for more details
>
> Cheers,
>
> Matteo
>


Thank you for your response, Matteo, it is highly appreciated!

--
Deniz Dogan
Chris F.A. Johnson

2007-05-31, 7:24 pm

On 2007-05-31, Deniz Dogan wrote:
> Hello.
> I am very new to bash scripting and was hoping you could help me out a
> bit. I am trying to create a bash script (but I'd prefer an alias) which
> changes the directory and then lists the files in that direcotry. This
> is what I came up with:
>
> #!/bin/sh
> cd $1;
> ls;
>
> This does not work and I believe this is due to the script starting a
> new "sh" shell and then changing the directory in THAT shell. How should
> I do this?


A script is executed in a new process, and it cannot change the
environment of the calling shell -- unless you source it:

.. /path/to/script

What you want is a shell function. Place this in your ~/.bashrc:

cdls() ## use whatever name you like
{
builtin cd "$1" &&
ls || printf "Could not change directory to %s\n" "$1"
}


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com