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

Hi all. I have a production environment that requires the use of sudo to execute perl scripts by multiple users. (emulating a production account, not root) A perl script accepts a where clause as an input parameter. I.e, sudo myperlscript.pl -cs CONNECTIONSTRING -w "fieldvalue in ('111','222')" In myperlscript.pl, w is defined as string ("w=s" => \$where_condition) However if printing $where_condition, the response is simply fieldvalue with the rest of the string stripped off after the first space. This causes an error when executing the combined sql statement. My query to the Monks community; Is there an escape sequence or quote combination you can recommend to allow the entire string to be passed to the variable? I can enter the where condition to and read from a file, but would like to solve this riddle with a less cumbersome work around. Any suggestions are welcome.

  • Comment on sudo ignoring string entry after first space encountered

Replies are listed 'Best First'.
Re: sudo ignoring string entry after first space encountered (ssh)
by tye (Sage) on Apr 14, 2015 at 22:38 UTC

    'sudo' usually execs things directly and so doesn't need to add quotes when it gets an argument value that includes shell meta characters (like spaces) and so doesn't suffer from what you described (based on my experience and the other reply you got already):

    $ perl -le'print for @ARGV' blah "a b c" blah a b c $ sudo perl -le'print for @ARGV' blah "a b c" blah a b c

    On the other hand, 'ssh' ends up invoking a shell and also doesn't try to figure out what quotes it would need to add and so requires you to add such:

    $ ssh localhost perl -le'"print for @ARGV"' blah "a b c" blah a b c

    See how I added an extra set of quotes after -le but not around the last argument and so what gets sent to the shell ends up being:

    perl -le"print for @ARGV" blah a b c

    Perhaps you are using ssh to invoke sudo but are blaming sudo for your problem? Just a guess.

    - tye        

Re: sudo ignoring string entry after first space encountered
by fishmonger (Chaplain) on Apr 14, 2015 at 21:26 UTC

    I'm unable to duplicate your problem. Could you post a short but complete script that demonstrates the issue?

      Hi, I'm not sure an apples to apples example is possible in print. I have sudu set up as an alias (alias sudu='sudo -H -u prodaccount SudoENV.ksh where SuoENV.ksh is a bash shell script that references the prodaccount environment variables. Actual command line example; sudu optim_table.pl -i ab_in_keys_201504 -ic NZMDSODS -n 1000 -o work_cal_123 -oc NZMDSODS -u clough -w "track_seq in ('Z010201503052014871','Z0102015030520148710')" .pl snippets

      GetOptions ("i=s" => \$input_tbl, "ic=s" => \$input_con_str, ## defaults to +"nzmdsods" "o=s" => \$output_tbl, ## defaults to +"${input_tbl}" "oc=s" => \$output_con_str, ## defaults to +"nzmdsods" "u=s" => \$user, "k=s" => \$key, ## defaults to +"rowid" "w=s" => \$where_condition, ## defaults to +"1=1" "n=i" => \$rec_count, ## defaults to +1000 "cfg=s" => \$config_file, ## defaults to +same name as report file; if specified, overrides pii class mapping "ld=s" => \$log_dir ## defaults to +"/Projects/OPScripts/MDS/logs" ); ..... print "CED WHERE CONDITION $where_condition\n"; $where_condition = "1=1" if (!$where_condition); $where_clause = "where $where_condition"; print "CED WHERE CLAUSE $where_clause\n";
      result: CED WHERE CONDITION track_seq CED WHERE CLAUSE track_seq expected: CED WHERE CONDITION track_seq in ('Z010201503052014','Z010201503052014') CED WHERE CONDITION track_seq in ('Z010201503052014','Z010201503052014') This may not be a perl issue per se and more of a shell issue. I'm hoping the symptoms described will foster some thoughts/tricks on how to rectify the quirk.

        where SuoENV.ksh is a bash shell script

        So are you using $* in your script where you should be using "$@" ?

        $ cat wrapper #!/bin/bash perl -le'print for @ARGV' $* perl -le'print for @ARGV' "$@" $ ./wrapper foo "a b c" foo a b c foo a b c

        - tye        

Re: sudo ignoring string entry after first space encountered
by RonW (Parson) on Apr 15, 2015 at 19:44 UTC

    Please try the following Perl script and sudo command, then post the results.

    sudotest.pl:

    #!perl -w use strict; my $i = 0; for ($ARGV) { print qq( $i : "$_" \n); $i++; }
    sudo -H -u prodaccount sudotest.pl