2

I'm fairly new to programming and I searched the internet for a way to pass bash output to a Python script.

I came up with this in bash.

XAS_SVN=`svn info` 
ssh hudson@test "python/runtests.py $XAS_SVN"

And this in python.

import sys
print sys.argv[1]

When I echo $SVN_INFO I get the result.

Path: . URL: //svn/rnd-projects/testing/python Repository Root: //svn/rnd-projects Repository UUID: d07d5450-0a36-4e07-90d2-9411ff75afe9 Revision: 140 Node Kind: directory Schedule: normal Last Changed Author: Roy van der Valk Last Changed Rev: 140 Last Changed Date: 2009-06-09 14:13:29 +0200 (Tue, 09 Jun 2009)

However the python script just prints the following.

Path:

Why is this and how can I solve this? I the $SVN_INFO variable not of string type?

I know about the subprocess module and the Popen function, but I don't think this is a solution since the python script runs on another server.

1
  • Why was af's answer validated, it doesn't actually work, you can't expand a variable inside single quotes (try `echo '$PATH'). Commented Aug 26, 2009 at 8:29

2 Answers 2

1

Since you have spaces in the variable, you need to escape them or read all the arguments in your script:

print ' '.join(sys.argv[1:])

But it might be better to use stdin/stdout to communicate, especially if there can be some characters susceptible to be interpreted by the shell in the output (like "`$'). In the python script do:

for l in sys.stdin:
     sys.stdout.write(l)

and in shell:

svn info | ssh hudson@test python/runtests.py
0
0

Yes, you need to put in quotes your input to python/runtest.py because otherwise argv[1] gets it only up to the first space. Like this:

ssh hudson@test "python/runtest.py \"$XAS_SVN\""
3
  • Did you test this? If you use single quotes, the variable XAS_SVN won't be expanded. Commented Aug 26, 2009 at 8:22
  • Yes, that's true. I had a little mix-up when testing my un-edited answer, which was like it is now. I tested it with different shell and it handled the quotes differently. My bad. Commented Aug 26, 2009 at 8:33
  • First it didn't work, but after your edit it works perfectly, thanks! Commented Aug 26, 2009 at 10:19

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.