如何通过提供参数为使用shell脚本的python程序生成linux命令?
例如,我有像hello.py、add.py、square.py这样的python文件。 这些文件的定义如下:如何通过提供参数为使用shell脚本的python程序生成linux命令?,python,linux,shell,Python,Linux,Shell,例如,我有像hello.py、add.py、square.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)
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')
您可以在python脚本中使用shebang,如/usr/bin/python,以便这些文件可以像shell脚本一样执行,例如,如果使用shebang,python file.py可以作为file.py执行。所以根据你的问题,你可以像这样在shell脚本的switch case中调用这些脚本
#! /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
如果在python脚本中不使用shebang,请在/path/to/script.py前面添加python,最好在脚本中使用shebang,并使用绝对路径。还要确保相应的脚本具有执行权限。您可以在python脚本中使用shebang,如/usr/bin/python,以便这些文件可以像shell脚本一样执行,例如,如果使用shebang,python file.py可以作为file.py执行。所以根据你的问题,你可以像这样在shell脚本的switch case中调用这些脚本
#! /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
如果在python脚本中不使用shebang,请在/path/to/script.py前面添加python,最好在脚本中使用shebang,并使用绝对路径。还要确保各个脚本具有执行权限。下面是一个使用python中的argparse all的示例 您可以通过以下方式运行它: python pyshell.py你好你好 python pyshell.py添加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)
下面是一个在python中使用argparse all的示例 您可以通过以下方式运行它: python pyshell.py你好你好 python pyshell.py添加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)
shell脚本是必需的还是可以继续使用python?您可能想看看argparse模块@fizzyh2o-谢谢您的回复。您能解释一下如何在上述场景中继续使用“argparse”吗?是否需要一个shell脚本,或者您能坚持使用python吗?您可能想看看argparse模块@fizzyh2o-谢谢您的回复。您能解释一下如何在上述场景中继续使用“argparse”吗?谢谢您的回答。你能用上面的场景详细说明你的答案吗?这对于shell脚本来说是可行的,但是python文件本身是不起作用的,因为它们需要从命令行输入或执行函数。我不明白你在说什么。请注意$2是传递给shell脚本的参数。我在shell脚本中回答了它,因为用户提出了与从shell脚本执行相关的问题。谢谢您的回答。你能用上面的场景详细说明你的答案吗?这对于shell脚本来说是可行的,但是python文件本身是不起作用的,因为它们需要从命令行输入或执行函数。我不明白你在说什么。请注意$2是传递给shell脚本的参数。我在shell脚本中回答了这个问题,因为用户问了有关从shell脚本执行的问题。谢谢。它帮助了我。这里我有一个疑问。我们必须维护相同的子Parser名称和函数名称吗?不,子Parser名称和函数名称实际上不必匹配-如果所有变量名称都匹配,这有助于更好地理解代码。谢谢。它帮助了我。这里我有一个疑问。我们必须维护相同的子Parser名称和函数名称吗?不,子Parser名称和函数名称实际上不必匹配-如果所有变量名称都匹配,这有助于更好地理解代码。