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

Hi, I am trying to create a shell script to be executed inside a perl code, Problem:- The Script gets created, but the Variable value is not set.

sub create_db_install_val(){ $File_name="$HOME/DB_INSTALL_VALIDATION.sh"; print "\n Shell name to validate DB Installation Completion is $File_n +ame\n"; open (FILE,">$File_name") or die "cannot open $File_name file"; print FILE "#!/bin/sh\n"; print FILE "FILE=\"/scratch/pratranj/DB_11.2.0.1.0/config/setup.log\"\ +n"; print FILE "STRING=\"Completed creating database instance\"\n"; print FILE "EXIT=1\n"; print FILE "while : ;do\n"; print FILE " [[ -f \"$FILE\" ]] && grep -q \"$STRING\" \"$FILE\" && + echo \"DB Installation is completed\" && break\n"; print FILE "sleep 5\n"; print FILE "done\n"; close(FILE); }

When I execute this perl fucntion, it writes shell script where the $FILE value is not set, something Like this:-

#!/bin/sh FILE="/scratch/pratranj/DB_11.2.0.1.0/config/setup.log" STRING="Completed creating database instance" EXIT=1 while : ;do [[ -f '""' ]] && grep -q "" "" && echo "DB Installation is complet +ed" && break sleep 5 done

Please suggest how to get the value of $FILE and $STRING in the shell file created within perl

Thanks, PR

Replies are listed 'Best First'.
Re: creating Shell Script using Perl
by kcott (Archbishop) on Sep 06, 2017 at 05:41 UTC

    G'day pratikmonu,

    Consider using a here-document for that. You should find the code is a lot cleaner and you'll only need a single print statement.

    Here's a rough example based on the code you posted:

    #!/usr/bin/env perl use strict; use warnings; my ($file, $string) = qw{$HOME/... Completed...}; print <<"EOF"; #!/bin/sh FILE="$file" STRING="$string" while ... "$file" ... "$string" ... EOF

    Output:

    #!/bin/sh FILE="$HOME/..." STRING="Completed..." while ... "$HOME/..." ... "Completed..." ...

    — Ken

Re: creating Shell Script using Perl
by afoken (Chancellor) on Sep 06, 2017 at 05:03 UTC

    Add use strict; use warnings; to your code for some enlightment.

    Look up "interpolation" if you need another clue.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      using strict and warning gives me this error:-

      Variable "$FILE" is not imported at NativeUIAutomationScript.pl line 136.

      Variable "$FILE" is not imported at NativeUIAutomationScript.pl line 136.

      I am not really sure How to use interpolation in this function.

        I am not really sure How to use interpolation in this function.

        You should not. You have already thought of backslashes to mask the quote marks.

        Also put backslashes before the $ sigil of variables that should only be in use in the final shell script.

        Cheers, Sören

        Créateur des bugs mobiles - let loose once, run everywhere.
        (hooked on the Perl Programming language)

        print FILE "    [[ -f \"$FILE\" ]] && grep -q \"$STRING\" \"$FILE\" && echo \"DB Installation is completed\" && break\n";

        $FILE and $STRING appear in a Perl double-quoted string. Perl will try to interpolate these Perl variables into the string, but you really intend them for the shell. In order to protect them from Perl interpolation, escape them with a  \ (backslash) (as afoken has written). So the statement would look something like (untested)
            print FILE "    [[ -f \"\$FILE\" ]] && grep -q \"\$STRING\" \"\$FILE\" && echo \"DB Installation is completed\" && break\n";
        or maybe, since you don't really seem to need Perl interpolation at all in this statement,
            print FILE '    [[ -f "$FILE" ]] && grep -q "$STRING" "$FILE" && echo "DB Installation is completed" && break', "\n";
        (or use a here-doc as kcott suggested).


        Give a man a fish:  <%-{-{-{-<

Re: creating Shell Script using Perl
by vinoth.ree (Monsignor) on Sep 06, 2017 at 06:04 UTC
    Hi pratikmonu

    Look at the below code I have just changed the print statement with 'single quote' to stop the variable interpolation to use the variable as it is. In your code perl looks for scalar variable $FILE and $STRING.

    use strict; use warnings; sub create_db_install_val(){ my $File_name=$ENV{'HOME'}."/DB_INSTALL_VALIDATION.sh"; print "\n Shell name to validate DB Installation Completion is $File_n +ame\n"; open (FILE,">$File_name") or die "cannot open $File_name file"; print FILE "#!/bin/sh\n"; print FILE "FILE=\"/scratch/pratranj/DB_11.2.0.1.0/config/setup.log\"\ +n"; print FILE "STRING=\"Completed creating database instance\"\n"; print FILE "EXIT=1\n"; print FILE "while : ;do\n"; print FILE ' [[ -f "$FILE" ]] && grep -q "$STRING" "$FILE" && echo +"DB Installation is completed" && break',"\n"; print FILE "sleep 5\n"; print FILE "done\n"; close(FILE); }
    Updated the code after Re^2: creating Shell Script using Perl

    All is well. I learn by answering your questions...
      print FILE '    [[ -f "$FILE" ]] && grep ... && break\n';

      This gives you a literal  \n (backslash-n) at the end | embedded in the output file. See this for a better (IMHO) approach.


      Give a man a fish:  <%-{-{-{-<

        Oh! Sorry about that, didn't noticed it, I just concentrated on getting those variable into the shell script.


        All is well. I learn by answering your questions...
Re: creating Shell Script using Perl
by manoj_speed (Prior) on Oct 10, 2017 at 11:37 UTC
    Please try the below code.
    sub create_db_install_val(){ $File_name="DB_INSTALL_VALIDATION.sh"; print "\n Shell name to validate DB Installation Completion is $File +_name\n"; $MYSCRIPT=<<SCRIPT; #!/bin/sh FILE="/scratch/pratranj/DB_11.2.0.1.0/config/setup.log" STRING="Completed creating database instance" EXIT=1 while : ;do [[ -f "\$FILE" ]] && grep -q "\$STRING" "\$FILE" && echo "DB Install +ation is completed" && break sleep 5 done SCRIPT open (FILE,">$File_name") or die "cannot open $File_name file"; print FILE $MYSCRIPT; close(FILE); } Output: ------- $ cat DB_INSTALL_VALIDATION.sh #!/bin/sh FILE="/scratch/pratranj/DB_11.2.0.1.0/config/setup.log" STRING="Completed creating database instance" EXIT=1 while : ;do [[ -f "$FILE" ]] && grep -q "$STRING" "$FILE" && echo "DB Installati +on is completed" && break sleep 5 done
    -- The wisest mind has something yet to learn.