简体   繁体   English

从bash脚本传递参数到python

[英]Pass arguments to python from bash script

I have been working on a script mixture of bash and python script. 我一直在研究bashpython脚本的脚本混合。 The bash script can receive unknown count input arguments. bash脚本可以接收未知的计数输入参数。 For example : 例如 :

tinify.sh  test1.jpg test2.jpg test3.jpg .....

After the bash receives all, it pass these arguments to tinify.py . 在bash收到all之后,它将这些参数传递给tinify.py Now I have come out two ways to do that. 现在我已经有两种方法可以做到这一点。

  • Loop in bash and call python tinify.py testx.jpg bash循环并调用python tinify.py testx.jpg

    In another word, python tinify test1.jpg then python tinify test2.jpg , finaly python tinify test3.jpg 换句话说, python tinify test1.jpg然后是python tinify test2.jpgpython tinify test3.jpg

  • Pass all arguments to tinify.py then loop in python 将所有参数传递给tinify.py然后在python循环

But there is a problem, I want to filter same parameter for example if user input tinify.sh test1.jpg test1.jpg test1.jpg , I only want tinify.sh test1.jpg .So I think it's easier to do in second way because python may be convenient. 但是有一个问题,我想过滤相同的参数,例如,如果用户输入tinify.sh test1.jpg test1.jpg test1.jpg ,我只想要tinify.sh test1.jpg 。所以我认为第二种方式更容易因为python可能很方便。

How can I do to pass all arguments to python script? 如何将所有参数传递给python脚本? Thanks in advance! 提前致谢!

You use $@ in tinify.sh 你在tinify.sh使用$@

#!/bin/bash
tinify.py "$@"

It will be far easier to eliminate duplicates inside the Python script that to filter them out from the shell. 消除Python脚本中要从shell中过滤掉的重复项要容易得多。 (Of course, this raises the question whether you need a shell script at all.) (当然,这引发了一个问题,你是否需要一个shell脚本。)

In addition to Chepner's answer above: 除了Chepner的答案之外:

#!/bin/bash
tinify.py "$@"

within python script, tinify.py: 在python脚本中,tinify.py:

from sys import argv
inputArgs = sys.argv[1:]
def remove_duplicates(l):
    return list(set(l))
arguments=remove_duplicates(inputArgs)

The list arguments will contain the arguments passed to python script (duplicated removed as set can't contain duplicated values in python). list arguments将包含传递给python脚本的参数(复制已删除,因为set不能包含python中的重复值)。

A python program can accept any number of command line arguments, using sys.argv — just remember that sys.argv[0] is the name of the script and actual arguments are contained in sys.argv[1:] python程序可以使用sys.argv接受任意数量的命令行参数 - 只记得sys.argv[0]是脚本的名称,实际参数包含在sys.argv[1:]

$ cat test_args.py
from sys import argv

prog_name = argv[0]
print('Program name:', prog_name)

for arg in argv[1:]:
    print(arg)
$ python test_args.py a b 'c d'
Program name: test_args.py
a
b
c d
$

note that an argument containing spaces must be quoted according to the shell syntax. 请注意,必须根据shell语法引用包含空格的参数。

Your file tinify.py should start with the following (if you have two arguments): 你的文件tinify.py应该从以下开始(如果你有两个参数):

import sys

arg1, arg2 = sys.argv[1], sys.argv[2]

sys.argv[0] is the name of the script itself. sys.argv[0]是脚本本身的名称。 You can of course loop over sys.argv . 你当然可以循环遍历sys.argv Personally, i like to pass all the arguments as json objects, so afterwards I do json.loads() 就个人而言,我喜欢将所有参数作为json对象传递,所以之后我做了json.loads()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM