['s in shell

['s in shell

Post by Ian Stain » Fri, 07 Feb 1992 08:50:27



In /bin/sh AT&T V4.0
How does the shell interpret [ ]  square brackets?

I find that if I do not quote brackets, I get unpredictable  (to me (-:
results).  For example, I found that:

foobar | tr [a-b] [A-B]

does not produce the same output as
`foobar | tr [a-b] [A-B]`

I have to quote the brackets to make it work like this:

`foobar | tr '['a-b']' '['A-B']'

Why does the indirect mode act different than the direct mode in
interpreting the brackets?

 
 
 

['s in shell

Post by David Gundla » Mon, 10 Feb 1992 10:53:01



% In /bin/sh AT&T V4.0
% How does the shell interpret [ ]  square brackets?
Hmmm...  Not too sure, since I'm on a Sun at 4.1.1, but I think it
doesn't.  At all.

%
% I find that if I do not quote brackets, I get unpredictable  (to me (-:
% results).  For example, I found that:
%
% foobar | tr [a-b] [A-B]
This seems as though it would run two tests (under SunOS, [ is the
program usr/bin/[ and is linked to /usr/bin/test), thus giving you
strange output.

%
% does not produce the same output as
% `foobar | tr [a-b] [A-B]`
This should also run tests, though.  Hmmm...

%
% I have to quote the brackets to make it work like this:
%
% `foobar | tr '['a-b']' '['A-B']'
This is the only one which will pass the square brackets to the tr
command-- or at least, it should be.

%
% Why does the indirect mode act different than the direct mode in
% interpreting the brackets?

I still dunno.  I just hope that this may have helped clear up your
problem a bit...

:-D
--





"I think, therefore I am wrong." --   me

 
 
 

['s in shell

Post by Paul Falst » Mon, 10 Feb 1992 11:30:52




 said...
>% In /bin/sh AT&T V4.0
>% How does the shell interpret [ ]  square brackets?
>Hmmm...  Not too sure, since I'm on a Sun at 4.1.1, but I think it
>doesn't.  At all.

% man sh

SH(1)                    USER COMMANDS                      SH(1)
...
  Filename Generation
...
          [...]
               Matches any one of  the  enclosed  characters.   A
               pair  of  characters  separated by `-' matches any
               character lexically between the  pair,  inclusive.
               If  the first character following the opening [ is
               a ! any character not enclosed is matched.

The shell does interpret brackets.  They're part of the wildcard set.
This is true on any /bin/sh I know of (including our SunOS).

Quote:>% I find that if I do not quote brackets, I get unpredictable  (to me (-:
>% results).  For example, I found that:
>%
>% foobar | tr [a-b] [A-B]

The tr [a-b] [A-B] command will, if you're lucky, simply run tr with
the arguments [a-b] and [A-B] (in sh, wildcard patterns are left alone
if they don't match anything).  If you're unlucky, there will be a file
called "a" or "B" or something in the current directory.  In that case,
[a-b] or [A-B] will be replaced with the list of matched filenames, which
is not what you want.

Quote:>This seems as though it would run two tests (under SunOS, [ is the
>program usr/bin/[ and is linked to /usr/bin/test), thus giving you
>strange output.

The [ will only be interpreted as a command if it is in command position.
Also, under SunOS, [ and test are built into /bin/sh; they're not external
commands:

$ PATH=/
$ [
test: ] missing

Quote:>% does not produce the same output as
>% `foobar | tr [a-b] [A-B]`
>This should also run tests, though.  Hmmm...

I'm not sure why this would produce different results.  Probably, you
ran it in a different directory than the previous example.  The other
directory didn't have the one-character filenames in it, thus producing
different results.

Quote:>% I have to quote the brackets to make it work like this:
>%
>% `foobar | tr '['a-b']' '['A-B']'
>This is the only one which will pass the square brackets to the tr
>command-- or at least, it should be.

The earlier ones will pass the square brackets to tr, but only if you're
lucky.  Always quote the brackets just to be sure.  Quote the whole
argument, in fact; it's easier:

foobar | tr '[a-b]' '[A-B]'

--
Paul Falstad                     | 10 PRINT "PRINCETON CS"

 
 
 

['s in shell

Post by Adrian Maria » Mon, 10 Feb 1992 15:31:53



Quote:>foobar | tr '[a-b]' '[A-B]'

I have seen this sort of construction tossed around a lot, and am
rather mystified.  

Why do people include the brackets in tr invocations?  

According to the manual, the effect of including the brackets is to
force the conversion of '[' to '[' and the conversion of ']' to ']'.
This seems pointless.  If you omit the brackets, then you don't need
to worry about quoting, and the command is just:
        foobar | tr a-b A-B

Am I missing something?

 
 
 

['s in shell

Post by Jeff Leys » Mon, 10 Feb 1992 23:39:10




!!
!!>foobar | tr '[a-b]' '[A-B]'
!!
!!I have seen this sort of construction tossed around a lot, and am
!!rather mystified.  
!!
!!Why do people include the brackets in tr invocations?  

Because SYSV requires them, and (as you note) BSD effectively ignores them.
Thus the use of brackets is much more portable.

!!Am I missing something?

Yes.  "Unix" is *not* monolithic.  Just because your manual says something,
don't assume that all other manuals say the same thing.

                                A Preview of the first Presidential debate:
                        George Bush:    "What would my opponent offer?  The
                                         economic malaise of the Carter era.
                                         Fiscal Irresponsibilty and a sea
                                         of red ink.  And Saddam Hussein still
                                         in power thumbing his noes at the
                                         U.S. ........Wait a minute, I think
                                         I've got your note cards."
                                                -- Tom Toles
--


 
 
 

['s in shell

Post by Kari E. Hurt » Tue, 11 Feb 1992 00:14:42




> According to the manual, the effect of including the brackets is to
> force the conversion of '[' to '[' and the conversion of ']' to ']'.
> This seems pointless.  If you omit the brackets, then you don't need
> to worry about quoting, and the command is just:
>    foobar | tr a-b A-B

> Am I missing something?

You read wrong manual :-)

[Quote:

SYSTEM V DESCRIPTION
     When  string2  is  short,  characters  in  string1  with  no
     corresponding character in string2 are not translated.

     In either  string  the  following  abbreviation  conventions
     introduce  ranges  of characters or repeated characters into
     the strings.  Note: in the System V version, square brackets
     are required to specify a range.

]

- K E H

 
 
 

1. What's 'side effects' of Ksh built-ins?

Hi,

I have a book called "The new KornShell Command and Programming
Language". In several chapters, it mentions "side effects" of
built-ins. I'm confused what's that so that I can't understand some
paragraphs. Any help is appreciated.

Lesley Yang

2. PCI (kernel 1.2.8)

3. Can ISP detect when dial-ins are 'overloaded' ?

4. Accessing file system info from Java

5. how do 'plug-ins' work?

6. gcc error

7. Can ISP detect when dial-ins are 'overloaded' ?

8. "man" issues

9. shell built ins

10. Shells and built-ins

11. Korn Shell's equivalent of the C Shell's !*

12. Can't get dial-ins, anyone?

13. IP Accounting doesn't work for PPP dial-ins :-(( ??