Discussion: Simple bash script
Afficher un message
Vieux 31/05/2007, 11h40   #2
Teo
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Simple bash script

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

  Réponse avec citation
 
Page generated in 0,04664 seconds with 9 queries