如何通过提供参数使用shell脚本为python程序创建linux命令?

时间:2017-04-20 03:56:13

标签: python linux shell

例如,我有hello.pyadd.pysquare.py等python文件。 这些文件的定义是

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

我想从shell脚本(例如pyshell.sh)执行这些文件,并希望创建像

这样的命令
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

我正在尝试使用此代码

#! /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)

但是我收到了像

这样的错误
usage: 1_e.py [-h] {--hai} ...
1_e.py: error: invalid choice: 'name' (choose from '--hai')

2 个答案:

答案 0 :(得分:4)

这是在python中使用argparse all的示例。

您可以使用以下命令运行它:

  • 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)
    

答案 1 :(得分:1)

您可以在python file.py之类的python脚本中使用shebang,这样可以像shell脚本一样执行这些文件,例如file.py如果您使用shebang,则可以#! /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 执行。因此,根据您的问题,您可以在shell脚本中调用这些脚本,例如

python

如果您不在python脚本中使用shebang,请在/path/to/script.py前添加shebang,在脚本中更好地使用absolute path并使用const routes = [{ path: '/', component: Home, children: [ { path: "/health" children: [ { path: 'overview' component: Overview }, { path: 'blood', component: Blood } ] } ] }] 。还要确保相应的脚本具有执行权限。