I have been working on a script mixture of bash and python script. The bash script can receive unknown count input arguments. For example :
tinify.sh test1.jpg test2.jpg test3.jpg .....
After the bash receives all, it pass these arguments to tinify.py. Now I have come out two ways to do that.
Loop in
bashand callpython tinify.py testx.jpgIn another word,
python tinify test1.jpgthenpython tinify test2.jpg, finalypython tinify test3.jpgPass all arguments to
tinify.pythen loop inpython
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.
How can I do to pass all arguments to python script? Thanks in advance!
4 Answers 4
In addition to Chepner's answer above:
#!/bin/bash
tinify.py "$@"
within python script, 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).
1 Comment
remove_duplicates(sys.argv[1:])You use $@ in 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. (Of course, this raises the question whether you need a shell script at all.)
3 Comments
./tinify.sh test1.jpg test2.jpg than python tinify.py test1.jpg test2.jpg although they both works well for me. Is there any other convenient way to invoke them ?#!/usr/bin/python (or whatever is appropriate) to the Python script and make it executable, you can simply run ./tinify.py test1.jpg test2.jpg.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:]
$ 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.
2 Comments
python tinify.py $@ if you don't want to touch the arguments.Your file tinify.py should start with the following (if you have two arguments):
import sys
arg1, arg2 = sys.argv[1], sys.argv[2]
sys.argv[0] is the name of the script itself. You can of course loop over sys.argv. Personally, i like to pass all the arguments as json objects, so afterwards I do json.loads()
4 Comments
tinify.sh test1.jpg test1.jpg test1.jpg should become tinify.sh test1.jpg, you could use a set. For example set([sys.argv[i] for i in range(your_number_of_args)])tinify.py test1.jpg sys.argv[1] will be "test1.jpg". If you want a variable number of args you can count the args using len(sys.argv) Within the bash script I think you can pass all args using "$@". Taken from this answer
"$@", not bare$@, or you're splitting on spaces, expanding literal globs in names, and otherwise not passing the input exactly as it was received.