1

For example I have python files like hello.py, add.py, square.py. Definitions of these files are

hello.py:-
def hello(a):
    print a      #a is some name

add.py:-
def add(a,b):
    print a+b      #where a,b are numbers which I have to pass as arguments in command

square.py:-
def square(a):
    print a**2      #where 'a' is a number

I want to execute these files from shell script(For example pyshell.sh) and want to make commands like

pyshell --hello name  -  then it has to execute hello.py
pyshell --add 4 5  -  then it has to execute add.py
pyshell --square 2  -  then it has to execute square.py

I am trying with this code

#! /usr/bin/python
import argparse
# Create Parser and Subparser
parser = argparse.ArgumentParser(description="Example ArgumentParser")
subparser = parser.add_subparsers(help="commands")

# Make Subparsers
hai_parser = subparser.add_parser('--hai', help='hai func')
hai_parser.add_argument("arg",help="string to print")
hai_parser.set_defaults(func='hai')

args = parser.parse_args()

def hai(arg):
  print arg

if args.func == '--hai':
  hai(args.arg)

But I am getting an error like

usage: 1_e.py [-h] {--hai} ...
1_e.py: error: invalid choice: 'name' (choose from '--hai')
2
  • 1
    Is a shell script necessary or can you stick with python? You might want to look at the argparse module docs.python.org/3.6/library/argparse.html#module-argparse Commented Apr 20, 2017 at 4:05
  • @ fizzyh2o - Thank you for the reply. Can you explain how to proceed with 'argparse' for the above scenario? Commented Apr 20, 2017 at 4:12

2 Answers 2

4

Here's an example using argparse all in python.

You can run it by with the following:

  • python pyshell.py hello "well hi"
  • python pyshell.py add 20 3.4
  • python pyshell.py square 24

    pyshell.py:-

    import argparse
    
    # Create Parser and Subparser
    parser = argparse.ArgumentParser(description="Example ArgumentParser")
    subparser = parser.add_subparsers(help="commands")
    
    # Make Subparsers
    hello_parser = subparser.add_parser('hello', help='hello func')
    hello_parser.add_argument("arg",help="string to print")
    hello_parser.set_defaults(func='hello')
    
    add_parser = subparser.add_parser('add', help="add func")
    add_parser.add_argument("x",type=float,help='first number')
    add_parser.add_argument("y",type=float,help='second number')
    add_parser.set_defaults(func='add')
    
    square_parser = subparser.add_parser('square', help="square func")
    square_parser.add_argument("a",type=float,help='number to square')
    square_parser.set_defaults(func='square')
    
    args = parser.parse_args()
    
    def hello(arg):
      print arg
    
    def add(x,y):
      print x + y
    
    def square(a):
      print a**2
    
    if args.func == 'hello':
      hello(args.arg)
    elif args.func == 'add':
      add(args.x,args.y)
    elif args.func == 'square':
      square(args.a)
    
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. It was helped me. Here I have a doubt. Do we have to maintain the same subparser name and the function name?
No the subparser name and function names do not actually have to match - it just helps understand the code better if all the variable names match up.
1

You can use shebang inside python scripts like #! /usr/bin/python such that those files can be executed like shell script for example python file.py can be executed as file.py if you use shebang. So according to your question you can call those scripts in switch case in shell scripts like this

#! /bin/bash
case ${1:-''} in
"hello")
    /path/to/hello $2
    ;;
"add")
    /path/to/add $2 $3
    ;;
"square")
    /path/to/square $2
    ;;
*)
    echo "Invalid option supplied"
    exit 1
    ;;
exit 0

If you don't use shebang in python script add python in front of /path/to/script.py better use shebang in script and use absolute path. Also make sure that the respective scripts has execute permissions.

4 Comments

Thank you for your answer. Can you elaborate your answer using above scenario please.
This will work for the shell script, but the python files as they are will not work since they take to input from the command line or execute the functions.
I didn't understand what you are saying. Please note $2 is the argument passed to the shell script.
i answered it in shell script because the user asked question relating to execute from shell script.

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.