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 > -n option for bash script
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

-n option for bash script

Réponse
 
LinkBack Outils de la discussion
Vieux 30/08/2007, 09h06   #1
Ray
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut -n option for bash script

Hi all,

I'm encountering something which I don't quite understand. On
version 3.1.17(1) of GNU bash (for Debian stable), I made this very
simple script:

-----
#!/bin/bash

echo $#
echo $1

echo $*

echo $@
-----

And then I ran it as follows:

>testscript

0



>testscript -n -x

2
-x-x>testscript -x -n
2
-x
-x -n
-x -n
>testscript foo -n -x

3
foo
foo -n -x
foo -n -x

Everything is expected except when "-n" is the first option. It says
(correctly) that there are two options (-n and -x), but "-n" is
dropped and disappears. Is there something special about "-n"? I
tried googling for this but funny enough "-n" is dropped in Google
searches and I'm not sure what this problem is called so that I can
enter it in Google...

Any would be appreciated; thanks! (And yes, I realize that one
solution is to just not use "-n"...but I'd like to know why or what
I'm doing wrong. Perhaps my installation of bash is too old?) I was
thinking that perhaps "-n" is some kind of control character that is
being translated to something that is unprintable...but that's
something I've never heard of...

Ray

  Réponse avec citation
Vieux 30/08/2007, 09h13   #2
John L
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: -n option for bash script


"Ray" <raykyoto@gmail.com> wrote in message news:1188461218.861452.62820@x40g2000prg.googlegro ups.com...
> Hi all,
>
> I'm encountering something which I don't quite understand. On
> version 3.1.17(1) of GNU bash (for Debian stable), I made this very
> simple script:
>
> -----
> #!/bin/bash
>
> echo $#
> echo $1
>
> echo $*
>
> echo $@
> -----
>
> And then I ran it as follows:
>
> >testscript

> 0
>
>
>
> >testscript -n -x

> 2
> -x-x>testscript -x -n
> 2
> -x
> -x -n
> -x -n
> >testscript foo -n -x

> 3
> foo
> foo -n -x
> foo -n -x
>
> Everything is expected except when "-n" is the first option. It says
> (correctly) that there are two options (-n and -x), but "-n" is
> dropped and disappears. Is there something special about "-n"?


The question you need to ask is what "echo -n" does: man echo
for details.

--
John.


  Réponse avec citation
Vieux 30/08/2007, 09h22   #3
pgas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: -n option for bash script

On Aug 30, 11:06 am, Ray <rayky...@gmail.com> wrote:
> Hi all,
>
> I'm encountering something which I don't quite understand. On
> version 3.1.17(1) of GNU bash (for Debian stable), I made this very
> simple script:
>
> -----
> #!/bin/bash
>
> echo $#
> echo $1
>
> echo $*
>
> echo $@
> -----
>
> And then I ran it as follows:
>
> >testscript

>
> 0
>
> >testscript -n -x

>
> 2
> -x-x>testscript -x -n


echo
If -n is specified, the trailing newline is suppressed.

the -n is interpreted by echo

  Réponse avec citation
Vieux 30/08/2007, 16h33   #4
Ray
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: -n option for bash script


Hi John and pgas,

On Aug 30, 5:22 pm, pgas <pierre.gas...@gmail.com> wrote:
> echo
> If -n is specified, the trailing newline is suppressed.
>
> the -n is interpreted by echo


I see...thank you both for the explanation! So, this is just a
special case for bash scripts? That if -n is the first parameter, it
gets picked up by echo? Looking at " echo", it seems that echo
takes 3 arguments (-n, -e, and -E), so these three are special cases
that one has to remember?

Is there anything else to be aware of? Where would one find this
information in the man pages? I looked at "man bash" and nothing
seems to indicate the link between bash scripts and echo.

Thanks a lot!

Ray


  Réponse avec citation
Vieux 30/08/2007, 17h05   #5
Joachim Schmitz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: -n option for bash script

"Ray" <raykyoto@gmail.com> schrieb im Newsbeitrag
news:1188488030.994888.253240@x35g2000prf.googlegr oups.com...
>
> Hi John and pgas,
>
> On Aug 30, 5:22 pm, pgas <pierre.gas...@gmail.com> wrote:
>> echo
>> If -n is specified, the trailing newline is suppressed.
>>
>> the -n is interpreted by echo

>
> I see...thank you both for the explanation! So, this is just a
> special case for bash scripts? That if -n is the first parameter, it
> gets picked up by echo? Looking at " echo", it seems that echo
> takes 3 arguments (-n, -e, and -E), so these three are special cases
> that one has to remember?

Yes

> Is there anything else to be aware of? Where would one find this
> information in the man pages? I looked at "man bash" and nothing
> seems to indicate the link between bash scripts and echo.

because ther is none, it's not a bash thing, it echo 'eating' the -n, not
bash.

use
echo -- $1
echo -- $*
echo -- $@

the -- should tell echo (and any other command) that no options are to
follow.

Bye, Jojo


  Réponse avec citation
Vieux 30/08/2007, 22h20   #6
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: -n option for bash script

On 2007-08-30, Ray wrote:
>
> Hi John and pgas,
>
> On Aug 30, 5:22 pm, pgas <pierre.gas...@gmail.com> wrote:
>> echo
>> If -n is specified, the trailing newline is suppressed.
>>
>> the -n is interpreted by echo

>
> I see...thank you both for the explanation! So, this is just a
> special case for bash scripts? That if -n is the first parameter, it
> gets picked up by echo?


It doesn't "get picked up by echo"; it is an argument to echo that
may be (and is, by bash) interpreted as an option. Not all
versions of echo regard -n as an option.

> Looking at " echo", it seems that echo
> takes 3 arguments (-n, -e, and -E), so these three are special cases
> that one has to remember?


The command can take as many arguments as you give it, subject to
system limitations. Those arguments that begin with a hyphen may
or may not be interpreted as options.

The POSIX standard says that echo does not support any options;
some shells do accept options to echo.

> Is there anything else to be aware of? Where would one find this
> information in the man pages? I looked at "man bash" and nothing
> seems to indicate the link between bash scripts and echo.


Read the entry for echo in the 'SHELL BUILTIN COMMANDS' section
of the man page.

If you do not have complete control over the text you want to
print, do not use echo; use printf instead.

--
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
Vieux 30/08/2007, 22h21   #7
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: -n option for bash script

On 2007-08-30, Joachim Schmitz wrote:
....
> use
> echo -- $1
> echo -- $*
> echo -- $@
>
> the -- should tell echo (and any other command) that no options are to
> follow.


That usage is contrary to the POSIX specification, which says:

The echo utility shall not recognize the "--" argument in the
manner specified by Guideline 10 of the Base Definitions...

--
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
Vieux 31/08/2007, 01h18   #8
Barry Margolin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: -n option for bash script

In article <1188488030.994888.253240@x35g2000prf.googlegroups .com>,
Ray <raykyoto@gmail.com> wrote:

> Hi John and pgas,
>
> On Aug 30, 5:22 pm, pgas <pierre.gas...@gmail.com> wrote:
> > echo
> > If -n is specified, the trailing newline is suppressed.
> >
> > the -n is interpreted by echo

>
> I see...thank you both for the explanation! So, this is just a
> special case for bash scripts? That if -n is the first parameter, it
> gets picked up by echo? Looking at " echo", it seems that echo
> takes 3 arguments (-n, -e, and -E), so these three are special cases
> that one has to remember?


There's nothing "special" going on. Your script contains the command:

echo $*

and $* gets replaced with the parameters you gave to the script. So if
you do:

testscript -n -x

then that command becomes:

echo -n -x

> Is there anything else to be aware of? Where would one find this
> information in the man pages? I looked at "man bash" and nothing
> seems to indicate the link between bash scripts and echo.


It's not even specific to echo. Put the following line in your script:

cat "$@"

and then run:

testscript -n -x

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
  Réponse avec citation
Vieux 31/08/2007, 01h43   #9
Ray
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: -n option for bash script


Hi all,

On Aug 31, 9:18 am, Barry Margolin <bar...@alum.mit.edu> wrote:
> In article <1188488030.994888.253...@x35g2000prf.googlegroups .com>,
>
> Ray <rayky...@gmail.com> wrote:
> There's nothing "special" going on. Your script contains the command:
>
> echo $*
>
> and $* gets replaced with the parameters you gave to the script. So if
> you do:
>
> testscript -n -x
>
> then that command becomes:
>
> echo -n -x


Ah, I see now! I was mistakenly thinking that arguments to scripts
are "special" and somehow, echo (and other commands in a script)
wouldn't treat them as arguments to themselves. I see where I was
wrong now...thank you! As I don't have control over what is passed to
the script, I think I will stick to cat, printf, etc.

Ray


  Réponse avec citation
Vieux 31/08/2007, 17h47   #10
Joachim Schmitz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: -n option for bash script

"Chris F.A. Johnson" <cfajohnson@gmail.com> schrieb im Newsbeitrag
news:pvojq4-3kl.ln1@xword.teksavvy.com...
> On 2007-08-30, Joachim Schmitz wrote:
> ...
>> use
>> echo -- $1
>> echo -- $*
>> echo -- $@
>>
>> the -- should tell echo (and any other command) that no options are to
>> follow.

>
> That usage is contrary to the POSIX specification, which says:
>
> The echo utility shall not recognize the "--" argument in the
> manner specified by Guideline 10 of the Base Definitions...

pitty. Sorry, I didn't check before posting.

Bye, Jojo


  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 17h49.


É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,25017 seconds with 18 queries