473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

strange behaviour sys.argv

Hi,

I am not used to python and I am wondering about this thing:

If I execute this from the shell:

/root/mk/services.py 192.168.1.101 critical "192.168.1.101
192.168.1.101 SNMPv2-MIB::sysUpTime.0 14:13:02:57.06 SNMPv2-
MIB::snmpTrapOID.0 SNMPv2-SMI::enterprises.789.0.13 SNMPv2-
SMI::enterprises.789.0.2"cfCannotTakeover == 1 priority == critical"
SNMP-COMMUNITY-MIB::snmpTrapAddress.0 192.168.1.101 SNMP-COMMUNITY-
MIB::snmpTrapCommunity.0 "public""

I have the following cmd arguments:

['/root/mk/services.py', '192.168.1.101', 'critical', '192.168.1.101
192.168.1.101 SNMPv2-MIB::sysUpTime.0 14:13:02:57.06 SNMPv2-
MIB::snmpTrapOID.0 SNMPv2-SMI::enterprises.789.0.13 SNMPv2-
SMI::enterprises.789.0.2cfCannotTakeover', '==', '1', 'priority',
'==', 'critical SNMP-COMMUNITY-MIB::snmpTrapAddress.0 192.168.1.101
SNMP-COMMUNITY-MIB::snmpTrapCommunity.0 public']

If I execute the same thing from a bash script:

#!/bin/bash
TRAP='192.168.1.101 192.168.1.101 SNMPv2-MIB::sysUpTime.0
14:13:02:57.06 SNMPv2-MIB::snmpTrapOID.0 SNMPv2-SMI::enterprises.
789.0.13 SNMPv2-SMI::enterprises.789.0.2"cfCannotTakeover == 1
priority == critical" SNMP-COMMUNITY-MIB::snmpTrapAddress.0
192.168.1.101 SNMP-COMMUNITY-MIB::snmpTrapCommunity.0 "public"'
HOST=$(echo "$TRAP" | awk '{print $1}')
SEVERITY='critical'
/root/mk/services.py $HOST $SEVERITY \"$TRAP\"

I get the following result:

['/root/mk/services.py', '192.168.1.101', 'critical',
'"192.168.1.101', '192.168.1.101', 'SNMPv2-MIB::sysUpTime.0',
'14:13:02:57.06', 'SNMPv2-MIB::snmpTrapOID.0', 'SNMPv2-
SMI::enterprises.789.0.13', 'SNMPv2-SMI::enterprises.
789.0.2"cfCannotTakeover', '==', '1', 'priority', '==', 'critical"',
'SNMP-COMMUNITY-MIB::snmpTrapAddress.0', '192.168.1.101', 'SNMP-
COMMUNITY-MIB::snmpTrapCommunity.0', '"public""']

Can someone help me with that?

This is the output of echo /root/mk/services.py $HOST $SEVERITY \"$TRAP
\"

/root/mk/services.py 192.168.1.101 critical "192.168.1.101
192.168.1.101 SNMPv2-MIB::sysUpTime.0 14:13:02:57.06 SNMPv2-
MIB::snmpTrapOID.0 SNMPv2-SMI::enterprises.789.0.13 SNMPv2-
SMI::enterprises.789.0.2"cfCannotTakeover == 1 priority == critical"
SNMP-COMMUNITY-MIB::snmpTrapAddress.0 192.168.1.101 SNMP-COMMUNITY-
MIB::snmpTrapCommunity.0 "public""

Thank you.

Cheers

Marcus

Apr 17 '07 #1
4 1506
schnupfy wrote:
I am not used to python and I am wondering about this thing:
This is not a Python question. It is a question about how to use bash.

I would try to help anyway, but I am unsure what results you actually
want. Your example is too complicated as well. You should strip down
your example to only the arguments that change. In doing this you may
solve the problem on your own.
--
Michael Hoffman
Apr 17 '07 #2
Michael Hoffman wrote:
schnupfy wrote:
>I am not used to python and I am wondering about this thing:

This is not a Python question. It is a question about how to use bash.
[snip]

Michael is correct, it is a bash thing, nothing to do with python.
bash (and other *nix like shells) generally break arguments on
white space. Quoting (both single and double) overrides this with
(slightly) different rules for different shells.
/root/mk/services.py 192.168.1.101 critical "192.168.1.101
192.168.1.101 SNMPv2-MIB::sysUpTime.0 14:13:02:57.06 SNMPv2-
MIB::snmpTrapOID.0 SNMPv2-SMI::enterprises.789.0.13 SNMPv2-
SMI::enterprises.789.0.2"cfCannotTakeover == 1 priority == critical"
SNMP-COMMUNITY-MIB::snmpTrapAddress.0 192.168.1.101 SNMP-COMMUNITY-
MIB::snmpTrapCommunity.0 "public""
Assuming this has been folded and actually is one long
line (which the output confirms), you have passed the python
script seven arguments

'192.168.1.101' (blank seperated)
'critical' (also blank seperated)
a string extending from just after the first double quote to
just before the second, ie starting with '192.168.1.101' and
ending with '789.0.2', with the immediately following (no
white space) unquoted text 'fCannotTakeover' appended
'==' (blank seperated)
'priority"
'=='
a string starting with critical, with the quoted string from
'SNMP-COMMUNITY' to 'Community.0 ' (including the blank), the
unquoted string 'public', and the null quoted string "" all
appended.
TRAP='192.168.1.101 192.168.1.101 SNMPv2-MIB::sysUpTime.0
14:13:02:57.06 SNMPv2-MIB::snmpTrapOID.0 SNMPv2-SMI::enterprises.
789.0.13 SNMPv2-SMI::enterprises.789.0.2"cfCannotTakeover == 1
priority == critical" SNMP-COMMUNITY-MIB::snmpTrapAddress.0
192.168.1.101 SNMP-COMMUNITY-MIB::snmpTrapCommunity.0 "public"'
HOST=$(echo "$TRAP" | awk '{print $1}')
SEVERITY='critical'
/root/mk/services.py $HOST $SEVERITY \"$TRAP\"
Here, the variables are expanded, and then split into
arguments on white space unless quoted. The backslashes protect
the double quotes so they are treated as normal characters, so
the $TRAP variable is also split into arguments on white space.
Quotes resulting from the substitution of $TRAP are also protected
(ie are treated as ordinary characters).

The result is

'192.168.1.101" (From $HOST)
'critical' (From $SEVERITY)
'"192.168.1.101' (Leading '"' from \", rest from
$TRAP, blank seperated)
'192.168.1.101' (from $TRAP, blank seperated)
'SNMPv2-MIB::sysUpTime.0'
and so on for the rest of the $TARP string, splitting it at
white space. The last part of $TRAP, '"public"', has a double
quote appended from the \".

Python is giving exactly what the shell has given it in both cases.

Charles
Apr 17 '07 #3
On Apr 17, 3:00 pm, Charles Sanders <C.delete_this.Sand...@BoM.GOv.AU>
wrote:
Michael Hoffman wrote:
schnupfy wrote:
I am not used to python and I am wondering about this thing:
This is not a Python question. It is a question about how to use bash.

[snip]

Michael is correct, it is a bash thing, nothing to do with python.
bash (and other *nix like shells) generally break arguments on
white space. Quoting (both single and double) overrides this with
(slightly) different rules for different shells.
/root/mk/services.py 192.168.1.101 critical "192.168.1.101
192.168.1.101 SNMPv2-MIB::sysUpTime.0 14:13:02:57.06 SNMPv2-
MIB::snmpTrapOID.0 SNMPv2-SMI::enterprises.789.0.13 SNMPv2-
SMI::enterprises.789.0.2"cfCannotTakeover == 1 priority == critical"
SNMP-COMMUNITY-MIB::snmpTrapAddress.0 192.168.1.101 SNMP-COMMUNITY-
MIB::snmpTrapCommunity.0 "public""

Assuming this has been folded and actually is one long
line (which the output confirms), you have passed the python
script seven arguments

'192.168.1.101' (blank seperated)
'critical' (also blank seperated)
a string extending from just after the first double quote to
just before the second, ie starting with '192.168.1.101' and
ending with '789.0.2', with the immediately following (no
white space) unquoted text 'fCannotTakeover' appended
'==' (blank seperated)
'priority"
'=='
a string starting with critical, with the quoted string from
'SNMP-COMMUNITY' to 'Community.0 ' (including the blank), the
unquoted string 'public', and the null quoted string "" all
appended.
TRAP='192.168.1.101 192.168.1.101 SNMPv2-MIB::sysUpTime.0
14:13:02:57.06 SNMPv2-MIB::snmpTrapOID.0 SNMPv2-SMI::enterprises.
789.0.13 SNMPv2-SMI::enterprises.789.0.2"cfCannotTakeover == 1
priority == critical" SNMP-COMMUNITY-MIB::snmpTrapAddress.0
192.168.1.101 SNMP-COMMUNITY-MIB::snmpTrapCommunity.0 "public"'
HOST=$(echo "$TRAP" | awk '{print $1}')
SEVERITY='critical'
/root/mk/services.py $HOST $SEVERITY \"$TRAP\"

Here, the variables are expanded, and then split into
arguments on white space unless quoted. The backslashes protect
the double quotes so they are treated as normal characters, so
the $TRAP variable is also split into arguments on white space.
Quotes resulting from the substitution of $TRAP are also protected
(ie are treated as ordinary characters).

The result is

'192.168.1.101" (From $HOST)
'critical' (From $SEVERITY)
'"192.168.1.101' (Leading '"' from \", rest from
$TRAP, blank seperated)
'192.168.1.101' (from $TRAP, blank seperated)
'SNMPv2-MIB::sysUpTime.0'
and so on for the rest of the $TARP string, splitting it at
white space. The last part of $TRAP, '"public"', has a double
quote appended from the \".

Python is giving exactly what the shell has given it in both cases.

Charles
ok, thanks for the answers. I try to hand over the 3rd part (the long
trap) as one cmd argument. I will ask in a shell ng. Thanks again.

Cheers

Marcus

Apr 17 '07 #4
schnupfy wrote:
>
ok, thanks for the answers. I try to hand over the 3rd part (the long
trap) as one cmd argument. I will ask in a shell ng. Thanks again.

Cheers
Should be as simple as removing the backslashes

/root/mk/services.py $HOST $SEVERITY "$TRAP"

should pass TRAP as a single arg

Charles
Apr 17 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Phil | last post by:
Hi, I don't understand this strange behaviour: I compile this code : #include <Python.h> #include"Numeric/arrayobject.h" static PyObject *
4
by: Ben | last post by:
Hi all, I'm trying to figure out how how complex map, filter and reduce work based on the following piece of code from http://www-106.ibm.com/developerworks/linux/library/l-prog.html : ...
20
by: Markus Sandheide | last post by:
Hello! Execute these lines: int x = 1; x = x > 2345678901; You will get: x == 1 with Borland C++ Builder
3
by: Sebastian C. | last post by:
Hello everybody Since I upgraded my Office XP Professional to SP3 I got strange behaviour. Pieces of code which works for 3 years now are suddenly stop to work properly. I have Office XP...
1
by: Ian | last post by:
Hi, I have noticed some strange behaviour when loading my database. I load a menu form on startup; this form contains a linked image to display. If the project is not then signed, the menu form...
31
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
4
by: ignw82 | last post by:
Hi all, I have a strange behaviour in dataview, maybe you can help me. the behaviour is like this : First I made a datatable (odt) in data set, and then I created a dataview using this...
8
by: matthewperpick | last post by:
Check out this toy example that demonstrates some "strange" behaviour with keyword arguments and inheritance. ================================= class Parent: def __init__(self, ary = ):...
4
by: Gotch | last post by:
Hi, I'm getting a very strange behaviour while running a project I've done.... Let's expose it: I've two projects. Both of them use a Form to do some Gui stuff. Other threads pack up messages...
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.