4

I have inserted python code inside bash script like this:

#!/bin/bash
echo "You are in Bash"
python <<END
# -*- coding: utf-8 -*-
import os
import sys
import getpass
print "You are in python"
username=raw_input('Bitbucket Username : ')
END
echo "Python ended"

But the problem is I am not able to take input using raw_input or input when I insert python in bash. Python alone works fine. What is the problem here in my code?

2
  • What are you trying to do? What's the goal of the script? Commented Feb 26, 2016 at 21:49
  • @mipadi I want to add some data to bitbucket using python selenium. The data I am going to add is in bash. Commented Feb 27, 2016 at 7:24

2 Answers 2

3

Instead of a heredoc, just user the -c parameter to pass a command:

$ python -c "username = raw_input('Enter name: ')
print 'Hello', username
"
Enter name: Chris
Hello Chris
Sign up to request clarification or add additional context in comments.

4 Comments

should I write python -c <<END ?
@VivekPuri No. Don't use the heredoc at all. You can express multiple lines of input simply by splitting the outer quotation marks as I've done above.
There is no solution to this problem if I need to write python code in a single portion and don't want to split?
@VivekPuri Your question lists python code on separate lines, exactly as I have done in my answer. Have you tried to adapt my answer for your needs?
2

Once you say END, you are telling bash that your program is over. Bash gives your lines as input to Python. If you are in python, type each line that you wrote there, regardless of what prompt, and when you are done, signify EOF by Ctrl-D on linux, Ctrl-Z on windows. You will see that raw_input() will have a EOFError. That is what bash is doing. It gives Python each line, and then says "We're done. Send EOF". It pays no attention to what prompt is given. It doesn't know that you are asking for input. This is a useful feature, however, because if it didn't have that, there would be nothing telling Python to exit...ever. You would have to send KeyboardInterrupt to get that program to exit.

Comments

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.