hyliau has asked for the wisdom of the Perl Monks concerning the following question:

hi,
If $ARGV2 contains symbol like ( , ). It will return error "Syntax error: '(' is not expected. How should I overcome this to accept the open/close bracket?

Initial script as below:

#!/usr/bin/perl # $1 $2 $3 (shell) are in $ARGV[0], $ARGV[1] and $ARGV[2] (perl) $ARGV[2] =~ s/[\r\n]+/ /g; #Now $3/$ARGV[2] doesn't contain any newline/linefeed anymore $shellindex=1; foreach (@ARGV) { print '$',$shellindex,' ($ARGV[',$shellindex-1,']: ',$_,"\n"; ++$shellindex; } print "Hello, World...\n";

Replies are listed 'Best First'.
Re: How to make perl accept symbol open/close bracket?
by gjb (Vicar) on Jun 27, 2003 at 09:49 UTC

    Most probably this is not a Perl issue but a shell issue.

    Try quoting the arguments you supply to your script on the command line, e.g.

    $ ./shellArgs.pl 'abc' '(abc)' 'evg'
    rather than
    $ ./shellArgs.pl abc (abc) evg
    Note that 'abc' and 'evg' need not be quoted, strictly speaking, so
    $ ./shellArgs.pl abc '(abc)' evg
    works just as well.

    Hope this helps, -gjb-

      hi,
      still not working using the '(abc)' as an example.

      pls refer to below example of $ARGV2 value which contains the symbol open/close bracket.

      Message from user AUDIT on SMDI22 Security alarm \(SECURITY\) and security audit \(SECURITY\) on SMDI22, + system id: 62940 Auditable event: Remote interactive breakin detection Event time: 26-JUN-2003 17:52:51.48 PID: 20405AE3 Process name: _TNA147: Username: HJKH Terminal name: TNA147:, _TNA147:, Host: 100.10.80.100 Po +rt: 59469 Remote node id: 1678397540 Remote node fullname: 100.10.80.100 Remote username: TELNET_640A5064 Status: %LOGIN-F-NOSUCHUSER, no such user

        gjb is correct. The problem is in your shell. Perl doesn't care if you have parenthesis in your variable (except maybe in a regex). I assume the message you printed above in output from something. The parenthesis are escaped because someone programmed it to do that. That has nothing to do with the input of the program.

        I copied your program and ran it and it worked fine as long as the arguments are quoted. It might be a difference in shells (I use bash on Linux) so you might try escaping the parenthesis by using a \ character.

        Hope that helps

        Lobster Aliens Are attacking the world!