How to execute perl from bash

Ask a Question related to UNIX Programming, Design and Development.

  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]nbriones@puc.cl[/email]
    Nicolas Briones Guest

  2. Similar Questions and Discussions

    1. Execute all processes in the background from bash.
      I was wondering if it's possible to prepend "time" and append "&" to all commands that I execute in bash. For example I'd like $ mozilla to...
    2. exit perl script and cd in bash?
      I've been searching the archives and google for an answer. I suspect it can't be done but thought I'd ask. What I'm trying to do is create a...
    3. from bash to perl; source() equivalent
      (Please CC: me as I'm not yet on this list.) I have moved / am moving from bash to perl for my misc scripts. I am still at a loss as to how to...
    4. problem using bash variables with command-line perl in bash script
      Hi! I have a problem with variables when using command-line perl in a bash script. The script should update a date (in 2003-10-10 form) if the...
    5. perl and bash about localization
      eastcapital@hotmail.com (Mi) writes: I'm sorry, that doesn't look like real code, or at least not all of it. $cal is a variable, and unless we...
  3. #2

    Default Re: How to execute perl from bash

    In article <208fae61.0307301249.32b584e6@posting.google.com >,
    Nicolas Briones <nbriones@puc.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.margolin@level3.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

  4. #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

  5. #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]nbriones@puc.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

Posting Permissions

  • You may not post new threads
  • You may 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