2

Here is a simple snippet of python code

#!/usr/bin/python
import sys
print sys.argv

Now, if I save this as test.py, make it executable, and run it as

./test.py abc it prints ['./test.py','abc'] which is perfectly sensible.

But if I run it as

./test.py http://www.google.co.in/imgres?hl=en&sa=X&biw=1600&bih=729&tbm=isch&prmd=imvns&tbnid=5FSY1KAB4T48vM:&imgrefurl=http://www.freeganita.com/en/mdlife/4_4.htm&docid=x4Z8mIMpa8FS9M&imgurl=http://www.freeganita.com/en/mdlife/4_4_files/image007.jpg&w=581&h=260&ei=MFaGUMnDHYXSrQfp5YGoDA&zoom=1&iact=hc&vpx=439&vpy=132&dur=1059&hovh=150&hovw=336&tx=71&ty=171&sig=100962344983266552479&page=1&tbnh=123&tbnw=276&start=0&ndsp=28&ved=1t:429,r:2,s:0,i:89

it prints

[1] 14812
[2] 14813
[3] 14814
[4] 14815
[5] 14816
[6] 14817
[7] 14818
[8] 14819
[9] 14820
[10] 14821
[11] 14822
[12] 14823
[13] 14824
[14] 14825
[15] 14826
[16] 14827
[17] 14828
[18] 14829
[19] 14830
[20] 14831
[21] 14832
[22] 14833
[23] 14834
[24] 14835
[25] 14836
[26] 14837
[27] 14838
[28] 14839
[2]   Done                    sa=X
[3]   Done                    biw=1600
[4]   Done                    bih=729
[5]   Done                    tbm=isch
[6]   Done                    prmd=imvns
[7]   Done                    tbnid=5FSY1KAB4T48vM:
[8]   Done                    imgrefurl=http://www.freeganita.com/en/mdlife/4_4.htm
[9]   Done                    docid=x4Z8mIMpa8FS9M
[10]   Done                    imgurl=http://www.freeganita.com/en/mdlife/4_4_files/image007.jpg
[11]   Done                    w=581
[15]   Done                    iact=hc
[18]   Done                    dur=1059
[20]   Done                    hovw=336
[21]   Done                    tx=71
[22]   Done                    ty=171
[24]   Done                    page=1
[25]   Done                    tbnh=123
jpm@andromeda:~/test$ ['./test.py', 'http://www.google.co.in/imgres?hl=en']

Can someone explain this?

2 Answers 2

7

you're not quoting or escaping your url, so you're getting all kinds of stuff coming from the shell interpretation of your argument. Try

./test.py 'http://www.google.co.in/imgres?hl=en&sa=X&biw=1600&bih=729&tbm=isch&prmd=imvns&tbnid=5FSY1KAB4T48vM:&imgrefurl=http://www.freeganita.com/en/mdlife/4_4.htm&docid=x4Z8mIMpa8FS9M&imgurl=http://www.freeganita.com/en/mdlife/4_4_files/image007.jpg&w=581&h=260&ei=MFaGUMnDHYXSrQfp5YGoDA&zoom=1&iact=hc&vpx=439&vpy=132&dur=1059&hovh=150&hovw=336&tx=71&ty=171&sig=100962344983266552479&page=1&tbnh=123&tbnw=276&start=0&ndsp=28&ved=1t:429,r:2,s:0,i:89'
Sign up to request clarification or add additional context in comments.

Comments

3

May I assume that you run this in Linux command line? In this case, & is a special control character that forks the background processes.

Use the quotes to prevent that:

./test.py "http://www.google.co.in/imgres?hl=en&sa=X&biw=1600&bih=729" # etc

1 Comment

I have totally forgot that. Thanks. That answers the question. I'm accepting the other one since it recommends singlequotes, which prevents shell expansion of variables. ( $something )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.