1

I have a simple function that simply prints "Hi!". I want to use bash to call my function, instead of lets say IDLE. However bash doesn't seem to want to print the output returned from the hi(): function.

 #!/usr/bin/python
 def hi():
 print 'Hi!'

This doesn't print "Hi!", when I type python hi.py (or ./hi.py) into bash

However if I do not include the print statement inside of a function, but just inside the file hi.py as a lone print 'Hi!' statement; then bash does output text "Hi!" accordingly. From bash this code below outputs Hi!

 #!/usr/bin/python
 print 'Hi!'

From within bash, how might I make bash output the string from the function hi(): in the file hi.py?

Thanks

asked Jan 20, 2017 at 0:05
4
  • 1
    You need to call the function. e.g. add hi() at the end of your python script. (this is frequently done in an if __name__ == '__main__': suite...) Commented Jan 20, 2017 at 0:07
  • If you really want to run the specific function(and not the module), see this question. However, it seems like what you want is what @mgilson suggested. Commented Jan 20, 2017 at 0:12
  • Thanks and sorry I had to reword my title. Commented Jan 20, 2017 at 0:13
  • I literally love stack overflow and its valued contributors. I aspire to be as helpful as you all one day, so I can help others too. Thanks thanks thanks a millie. Commented Jan 20, 2017 at 0:16

3 Answers 3

1

Assuming that you want to execute a particular function.The python executable accepts the '-c' flag to indicate that you are passing it code. So if my file (hi.py) is:

def hi():
 print 'Hi!'

Then I could do:

$ python -c "execfile('hi.py'); hi()"

which pints the output

HI!

Reason

when you execute a python script you should tell it where to start.Since Hi is not a main function you need to manually call hi()

def hi():
 print 'Hi!'
hi()
answered Jan 20, 2017 at 0:22
Sign up to request clarification or add additional context in comments.

1 Comment

This solution is the most applicable for me because it entails no alteration to the original hi.py module.
0

You need to do this in you hi.py:

 def hi():
 print 'Hi!'
 if __name__ == '__main__':
 hi()

Then it will work in bash:

#!/bin/bash
python hi.py
answered Jan 20, 2017 at 0:12

3 Comments

This solution worked for me. Thanks. But how does this work? So my understanding is that name is a variable (of the hi(): function even though I didn't declare it) which is compared to value 'main' (another variable of the hi(): function or basically all functions). If they're equal then hi() function is called.
This is acting like the main method. Like java, when you have defined a function, you will need to create the main function (public static void main()) for calling that function. Now what happened here is when you using python hi.py in command line, it assumes that the code you run contains the main method which has the name '__main__'. Therefore, when '__main__' is detected, the hi() function will be called.
Thanks for your clarification as to python expecting to find the main method. I am familiar with Java syntax so the analogy hit home. Ive been learning python programming using Harvey Mudd College's CS For All online book available here cs.hmc.edu/csforall/index.html. Their path toward OOP in python takes the reader through various programming paradigms (i.e. functional programming and imperative) to teach Python syntax and semantics; before reaching OOP. Thus I hadnt reached classes yet, and was unfamiliar with the main function. Thanks.
0

Are you calling the function in your python script? In order to get the print statement in your function to be activated you actually need to call the function. For example running python test.py for this file prints "Hi!"

# Program named test.py
def output():
 print('Hi!')
output() # Calling the output function which will cause a print statement

Also calling this python file in bash works also. For example calling bash call.sh for this script worked and printed "Hi!" to the command line

#!/bin/bash
# Script to call python file
python test.py
answered Jan 20, 2017 at 0:14

1 Comment

Thanks this solution works as well. Basically as you said above I must call the function( its name output()) from within the module itself, so python can allow bash to output the string. Its getting clearer thanks. Above your post @Jiaxi advised a similar solution with the addition of checking the hi(): functions variable name against the value 'main'. These are essentially similar solution but his uses the if condition. Thank you.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.