LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-25-2014, 12:33 AM   #1
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,345

Rep: Reputation: 135Reputation: 135
Quoting and escaping


I have a shell script, the arguments of which are collected in quote as a singlle argument and passed that single arg. to a perl script inside.

I want to give a n argument like this:

Code:
"Test Internal Cluster"
to the perl script.

When I am using
Code:
"Test\ Internal\ Cluster"
argument to the shell script its passing as
Code:
Test Internal Cluster
to the perl script, but I want that to be in double quotes.
This seems simple but not able to use proper quoting and escaping to achieve.
 
Old 02-25-2014, 01:27 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,191

Rep: Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784
Maybe you could show the snippet in both bash and Perl that receive and send this information?
 
Old 02-25-2014, 01:31 AM   #3
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,345

Original Poster
Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by grail View Post
Maybe you could show the snippet in both bash and Perl that receive and send this information?
Here is the shell script:
Code:
#!/bin/bash -xv

Perl=/usr/bin/perl
Api=/usr/local/API/check_api.pl

if [ $# -gt 0 ]; then
  if [ -x $Api ]
   then
        output=$($Perl $Api $1)
        exitstatus=$?
        F1=$(echo $output | awk -F':' '{print $1}')
        F2=$(echo $output | awk -F':' '{print $2}' | awk -F'|' '{print $1}')
        F3=$(echo $output | awk -F':' '{print $2}' | awk -F'|' '{print $2}')
        echo " $F1: $F3 | $F2"
  else
        echo "check_api.pl not found"
 fi
 exit $exitstatus
else
    echo "Usage : $0 '-D <DC> -C <Cluster> -u <userbame> -p <password> -i <interval> -l <command>  -s <subcommand>'"
fi
Here you can see the whole argument passed to shell script is collected to $1 and passed to the check_api perl script.

The issue is happening with cluster name with space(passed to -C).

And the wholeargument passed to the shell script should be in single/double quote.
 
Old 02-25-2014, 02:05 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,191

Rep: Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784
And why are your variables not quoted?
Code:
output=$($Perl $Api "$1")
It also seems a little weird that you retrieve the "exitstatus", but never test it and then call it to be used when it may in fact not be set at all (ie it is outside the 'if' where it gets set)

You test "$Api" to be executable by the user running the script but your error says that it has not been found. What if it were only executable by root? What if it exists but is not executable at all?

Lastly, have a look at parameter substitution as all of those calls to awk seem to be a huge over kill. If you can provide dummy data that might exist in "output" I can probably help
 
Old 02-25-2014, 02:55 AM   #5
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,345

Original Poster
Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by grail View Post
And why are your variables not quoted?
Code:
output=$($Perl $Api "$1")
It also seems a little weird that you retrieve the "exitstatus", but never test it and then call it to be used when it may in fact not be set at all (ie it is outside the 'if' where it gets set)

You test "$Api" to be executable by the user running the script but your error says that it has not been found. What if it were only executable by root? What if it exists but is not executable at all?

Lastly, have a look at parameter substitution as all of those calls to awk seem to be a huge over kill. If you can provide dummy data that might exist in "output" I can probably help
Hi grail, Thanks for the reply.

I haven't quoted the variable $1 because the perlscript is not accepting the whole argument as a single as below which I found in debug mode - where the whole argument is quoted if $1 is quoted.

Code:
/usr/bin/perl /usr/local/check_api.pl '-D x.x.x.x -C Test_Internal_Cluster -u user -p pass  -i 300 -l runtime -s list'

and throws error as incorrect usage

But without quoting the $1 the argument passed to perl script works like this and which gives me the result:

Code:
/usr/bin/perl /usr/local/check_api.pl -D x.x.x.x -C Test_Internal_Cluster -u user -p pass -i 300 -l runtime -s list
But I am failed with a cluster with space in its name.

In case of a space in cluster name if I am running in the command line directly(not from shell script) it works fine without any issue:

Code:
/usr/bin/perl /usr/local/check_api.pl -D x.x.x.x -C "Test Internal Cluster" -u user -p pass -i 300 -l runtime -s list
which I wanted to achieve from shell script.

Last edited by divyashree; 02-25-2014 at 02:56 AM.
 
Old 02-25-2014, 05:00 AM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
So why not do that then; the shell script is redundant.
 
Old 02-25-2014, 05:58 AM   #7
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,000

Rep: Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003
You have all the arguments put into $1, actually you can handle it in one or split by white spaces. But if you want to keep some words together you need to implement a special handler to it - or pass the arguments one by one and not in a single string.
 
Old 02-25-2014, 07:32 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,191

Rep: Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784
I am with others in not really seeing the point of the script, however, you can simply pass the line as is, ie:
Code:
-D x.x.x.x -C '"Test Internal Cluster"' -u user -p pass -i 300 -l runtime -s list
Then you can simply refer to $@ which will be all of your arguments
 
Old 02-26-2014, 09:45 PM   #9
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,345

Original Poster
Rep: Reputation: 135Reputation: 135
Thanks to all for your valuable inputs. I rewrote the script using getopts to pass one by one argument and it worked like charm.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash syntax - question regarding quoting: quoting doesn't work as I expect. c.stadegaart Linux - General 5 04-02-2012 11:40 PM
[SOLVED] Bash quoting gerhard111 Linux - Software 5 12-09-2010 08:09 AM
Perl quoting issue resetreset Programming 8 01-10-2009 08:58 AM
bash - quoting RGummi Linux - General 3 10-21-2006 03:06 PM


All times are GMT -5. The time now is 06:23 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration