user6839762 user6839762 - 2 months ago 15
Perl Question

Escaping double quotes in perl in Linux

Its not letting me reply to comment so editing here, using Linux as OS.
I am using the undermentioned to get tickets created, but -k parameter, duplication parameter gets the message in " " and if there are "" in the message, then these parameters gets messed up and messages dont get forwarded.
How to escape these double quotes in -k parameter so that it goes through?
Thanks in advance

#!/usr/bin/perl

use strict;

use Getopt::Std;



# declare the perl command line flags/options we want to allow


# Define the environment

my %option=();

my $SEND_OUTPUT = "" ;

my $MODE = "Prod" ;

my $SENDEVENT_LOG = "";

my $DATE = "" ;

my $SEND_EVENT = "";

my $SEND_OUTPUT_LOG = "" ;

my $DSOM_SEV = "" ;

getopts("q:a:n:p:s:k:c:m:t:y:z:", \%option);


# Define the environment



if ( $MODE =~ m/Prod/i ) {

$SENDEVENT_LOG="/var/opt/SendEvent/log/SendEvent.log";

}



if ( $MODE =~ m/Dev/i ) {



$SENDEVENT_LOG="/var/opt/SendEvent/log/SendEvent.Dev.log";



}

# Define time stamp



my $DATE=`date +%Y:%m:%d:\%T`; chomp $DATE ; my $TIME=`date +%T`; chomp $TIME; my $NOW=$DATE." ";





# Open Logfile and define the Date Variable




open ( LOGFILE, ">>${SENDEVENT_LOG}" ) or die ("Cannot open SendEvent log file ");




# test for the existence of the options on the command line.

# in a normal program you'd do more than just print these.




print LOGFILE "$DATE *******************************************************************\n";

print LOGFILE "\n$DATE New Instance of SendEvent initiated\n" ;

print LOGFILE "$DATE Examine the input arguments below to validate this instance of SendEvent\n\n" ;



print "\n\n" ;



if ( $option{q} ) { print LOGFILE "$DATE -q $option{q}\n" } else { print LOGFILE "$DATE -q **** value missing *****\n" }

if ( $option{a} ) { print LOGFILE "$DATE -a $option{a}\n" } else { print LOGFILE "$DATE -a **** value missing *****\n" }

if ( $option{n} ) { print LOGFILE "$DATE -n $option{n}\n" } else { print LOGFILE "$DATE -n **** value missing *****\n" }

if ( $option{p} ) { print LOGFILE "$DATE -p $option{p}\n" } else { print LOGFILE "$DATE -p **** value missing *****\n" }

if ( $option{s} ) { print LOGFILE "$DATE -s $option{s}\n" } else { print LOGFILE "$DATE -s **** value missing *****\n" }

if ( $option{k} ) { print LOGFILE "$DATE -k $option{k}\n" } else { print LOGFILE "$DATE -k **** value missing *****\n" }

if ( $option{c} ) { print LOGFILE "$DATE -c $option{c}\n" } else { print LOGFILE "$DATE -c **** value missing *****\n" }

if ( $option{m} ) { print LOGFILE "$DATE -m $option{m}\n" } else { print LOGFILE "$DATE -m **** value missing *****\n" }

if ( $option{t} ) { print LOGFILE "$DATE -t $option{t}\n" } else { print LOGFILE "$DATE -t **** value missing *****\n" }

if ( $option{y} ) { print LOGFILE "$DATE -y $option{y}\n" } else { print LOGFILE "$DATE -y **** value missing *****\n" }

if ( $option{z} ) { print LOGFILE "$DATE -z $option{z}\n" } else { print LOGFILE "$DATE -z **** value missing *****\n" }



# other things found on the command line

print LOGFILE "$DATE Other things found on the command line:\n" if $ARGV[0];

foreach (@ARGV)

{

print LOGFILE "$DATE $_\n";

}


my $TOOL = "$option{q}" ;

my $MSG_APPL = "$option{a}" ;

my $MSG_NODE_NAME = "$option{n}" ;

my $ESM_PROFILE = "$option{p}" ;

my $MSG_SEV = "$option{s}" ;

my $DEDUPLICATION = "$option{k}" ;

my $MANAGEMENT_ID = "$option{c}" ;

my $LONG_TEXT = "$option{m}" ;

my $SHORT_TEXT = "$option{t}" ;

my $DEFAULT_Q = "$option{y}" ;

my $ACCOUNT = "$option{z}" ;



# Translate OpenView Severity to DSOM Severity



# OpenView Sev DSOM Sev



if ( $MSG_SEV =~ m/Critical/i ) { $DSOM_SEV = "FATAL" }

if ( $MSG_SEV =~ m/Major/i ) { $DSOM_SEV = "CRITICAL" }

if ( $MSG_SEV =~ m/Minor/i ) { $DSOM_SEV = "MINOR" }

if ( $MSG_SEV =~ m/Warning/i ) { $DSOM_SEV = "WARNING" }

if ( $MSG_SEV =~ m/Normal/i ) { $DSOM_SEV = "HARMLESS" }



print LOGFILE "\n$DATE Translating Openview Severity to DSOM Severity, $MSG_SEV => $DSOM_SEV\n\n" ;


$SEND_OUTPUT_LOG = "/opt/SendEvent/dist/SendEvent/SendEvent send -q \"$TOOL\" -a \"$MSG_APPL\" -n \"$MSG_NODE_NAME\" -p \"$ESM_PROFILE\" -s \"$DSOM_SEV\" -k \"$DEDUPLICATION\" -c \"$MANAGEMENT_ID\" -m \"$LONG_TEXT\" -t \"ABC $SHORT_TEXT\" -z \"ABC\" -y \"$DEFAULT_Q\"" ;



$SEND_EVENT=`/opt/SendEvent/dist/SendEvent/SendEvent send -q "$TOOL" -a "$MSG_APPL" -n "$MSG_NODE_NAME" -p "$ESM_PROFILE" -s "$DSOM_SEV" -k "$DEDUPLICATION" -c "$MANAGEMENT_ID" -m "$LONG_TEXT" -t "ABC $SHORT_TEXT" -z "ABC" -y \"$DEFAULT_Q\"` ;


print LOGFILE "$DATE \n$SEND_EVENT\n\n" ;



print LOGFILE "$DATE $SEND_OUTPUT_LOG\n\n" ;

Answer

To me, the easiest way to quote a variable for shell interpolation is to:

  1. enclose it in ''
  2. replace any ' with '"'"'

So replace

$SEND_EVENT=`/opt/SendEvent/dist/SendEvent/SendEvent send -q "$TOOL" -a "$MSG_APPL" -n "$MSG_NODE_NAME" -p "$ESM_PROFILE" -s "$DSOM_SEV" -k "$DEDUPLICATION" -c "$MANAGEMENT_ID" -m "$LONG_TEXT" -t "ABC $SHORT_TEXT" -z "ABC" -y \"$DEFAULT_Q\"`  ;

with

(my $quoted_DEDUPLICATION = $DEDUPLICATION) =~ s/'/'"'"'/g;
$SEND_EVENT=`/opt/SendEvent/dist/SendEvent/SendEvent send -q "$TOOL" -a "$MSG_APPL" -n "$MSG_NODE_NAME" -p "$ESM_PROFILE" -s "$DSOM_SEV" -k '$quoted_DEDUPLICATION' -c "$MANAGEMENT_ID" -m "$LONG_TEXT" -t "ABC $SHORT_TEXT" -z "ABC" -y \"$DEFAULT_Q\"`  ;

(and do the same for all your other parameters).

Alternatively, use IPC::System::Simple::capturex and pass all the arguments in an array.

Comments