Professional Web Applications Themes

How to execute perl from bash - UNIX Programming

Hi everyone! I need to trip blank spaces at begin and end on certain string. I can do it executing perl -e '$str = " 2003-07-29 "; $str =~ s/^\s+|\s+$//g; print $str;' works fine! but i need the value inside a script. So i wrote: ------------------------------ #!/bin/bash curr_date=" 2003-07-29" t=`perl -e "$str = \"$curr_date\"; $str =~ s/^\s+|\s+$//g; print $str;\"` echo $t ------------------------------- And i get error! i try ------------------------------ #!/bin/bash curr_date=" 2003-07-29" t=`perl -e "\$str = \"$curr_date\"; \$str =~ s/^\s+|\s+$//g; print \$str;\"` echo $t ------------------------------- And i get error too. The problem is that i can't set perl variables inside ...

  1. #1

    Default How to execute perl from bash

    Hi everyone!
    I need to trip blank spaces at begin and end on certain string.

    I can do it executing

    perl -e '$str = " 2003-07-29 "; $str =~ s/^\s+|\s+$//g; print $str;'

    works fine!

    but i need the value inside a script. So i wrote:

    ------------------------------
    #!/bin/bash

    curr_date=" 2003-07-29"

    t=`perl -e "$str = \"$curr_date\"; $str =~ s/^\s+|\s+$//g; print
    $str;\"`

    echo $t
    -------------------------------

    And i get error!

    i try

    ------------------------------
    #!/bin/bash

    curr_date=" 2003-07-29"

    t=`perl -e "\$str = \"$curr_date\"; \$str =~ s/^\s+|\s+$//g; print
    \$str;\"`

    echo $t
    -------------------------------

    And i get error too. The problem is that i can't set perl variables
    inside the -e section. The problem (it seems) how bash make diference
    between script and perl variables?

    Thanx!

    [email]nbrionespuc.cl[/email]
    Nicolas Briones Guest

  2. #2

    Default Re: How to execute perl from bash

    In article <208fae61.0307301249.32b584e6posting.google.com >,
    Nicolas Briones <nbrionespuc.cl> wrote:
    >Hi everyone!
    >I need to trip blank spaces at begin and end on certain string.
    >
    >I can do it executing
    >
    >perl -e '$str = " 2003-07-29 "; $str =~ s/^\s+|\s+$//g; print $str;'
    >
    >works fine!
    >
    >but i need the value inside a script. So i wrote:
    >
    >------------------------------
    >#!/bin/bash
    >
    >curr_date=" 2003-07-29"
    >
    >t=`perl -e "$str = \"$curr_date\"; $str =~ s/^\s+|\s+$//g; print
    >$str;\"`
    >
    >echo $t
    >-------------------------------
    >
    >And i get error!
    You should be using single quotes, not double quotes, just like you did
    when you executed the command manually. Inside double quotes, $ is used to
    indicate shell variables.

    --
    Barry Margolin, [email]barry.margolinlevel3.com[/email]
    Level(3), Woburn, MA
    *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
    Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
    Barry Margolin Guest

  3. #3

    Default Re: How to execute perl from bash

    Nicolas Briones wrote:
    >
    > Hi everyone!
    > I need to trip blank spaces at begin and end on certain string.
    > I can do it executing
    >
    > perl -e '$str = " 2003-07-29 "; $str =~ s/^\s+|\s+$//g; print $str;'
    >
    > works fine!
    > but i need the value inside a script. So i wrote:
    >
    > ------------------------------
    > #!/bin/bash
    >
    > curr_date=" 2003-07-29"
    > t=`perl -e "$str = \"$curr_date\"; $str =~ s/^\s+|\s+$//g; print $str;\"`
    > echo $t
    > -------------------------------
    >
    > And i get error!
    > i try
    >
    > ------------------------------
    > #!/bin/bash
    >
    > curr_date=" 2003-07-29"
    > t=`perl -e "\$str = \"$curr_date\"; \$str =~ s/^\s+|\s+$//g; print \$str;\"`
    > echo $t
    > -------------------------------
    >
    > And i get error too. The problem is that i can't set perl variables
    > inside the -e section. The problem (it seems) how bash make diference
    > between script and perl variables?

    curr_date=" 2003-07-29"
    t=`perl -e's/^\s+//, s/\s+$// for $ARGV[0]; print $ARGV[0]' $curr_date`
    echo $t



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  4. #4

    Default Re: How to execute perl from bash

    Nicolas Briones wrote:
    > Hi everyone!
    > I need to trip blank spaces at begin and end on certain string.
    >
    > I can do it executing
    >
    > perl -e '$str = " 2003-07-29 "; $str =~ s/^\s+|\s+$//g; print $str;'
    >
    > works fine!
    >
    > but i need the value inside a script. So i wrote:
    >
    > ------------------------------
    > #!/bin/bash
    >
    > curr_date=" 2003-07-29"
    >
    > t=`perl -e "$str = \"$curr_date\"; $str =~ s/^\s+|\s+$//g; print
    > $str;\"`
    >
    > echo $t
    > -------------------------------
    >
    > And i get error!
    >
    > i try
    >
    > ------------------------------
    > #!/bin/bash
    >
    > curr_date=" 2003-07-29"
    >
    > t=`perl -e "\$str = \"$curr_date\"; \$str =~ s/^\s+|\s+$//g; print
    > \$str;\"`
    >
    > echo $t
    > -------------------------------
    >
    > And i get error too. The problem is that i can't set perl variables
    > inside the -e section. The problem (it seems) how bash make diference
    > between script and perl variables?
    >
    > Thanx!
    >
    > [email]nbrionespuc.cl[/email]
    Why are you using a bash script to run a perl one-liner? Surely it would
    make more sense to simply write the whole thing as a perl script.

    #! /usr/bin/perl -w

    use strict;

    my $curr_date = " 2003-07-29 ";

    $curr_date =~ s/^\s+|\s+$//g;

    print "$curr_date\n";



    Chris.

    Chris Guest

Similar Threads

  1. Question Execute SQL satatements in perl using DBI execute method
    By bennjoe in forum Brainstorming Area
    Replies: 0
    Last Post: October 7th, 04:53 PM
  2. Execute all processes in the background from bash.
    By guerrilla_thought in forum Linux / Unix Administration
    Replies: 6
    Last Post: May 20th, 04:52 PM
  3. from bash to perl; source() equivalent
    By Twb in forum PERL Beginners
    Replies: 2
    Last Post: January 3rd, 07:09 AM
  4. Replies: 4
    Last Post: November 28th, 05:28 PM
  5. perl and bash about localization
    By Eric Schwartz in forum PERL Miscellaneous
    Replies: 1
    Last Post: September 6th, 05:24 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139