PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Simple bash script
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Simple bash script

Réponse
 
LinkBack Outils de la discussion
Vieux 31/05/2007, 10h10   #1
Deniz Dogan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Simple bash script

Hello.
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?

--
Deniz Dogan
  Réponse avec citation
Vieux 31/05/2007, 10h40   #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
Vieux 31/05/2007, 14h32   #3
Deniz Dogan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Simple bash script

Teo wrote:
> 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
>


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

--
Deniz Dogan
  Réponse avec citation
Vieux 31/05/2007, 21h02   #4
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Simple bash script

On 2007-05-31, Deniz Dogan wrote:
> Hello.
> 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?


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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h40.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11963 seconds with 12 queries