D
D
denislysenko2021-12-17 01:12:42
bash
denislysenko, 2021-12-17 01:12:42

How to write a bash script that will run a file that supports command line arguments?

I have get-movies.py file.

I can call it like this: python3 get-movies.py.

This file supports 5 arguments:
-year_from (if the argument is not written after the file name, then it defaults to 1800)
-year_to (if the argument is not written after the file name, then it defaults to 2030)
-N (if the argument is not written after the file name then it defaults to None)
-regexp (if the argument is not written after the file name, then it defaults to '')
-genres (if the argument is not written after the file name, then it defaults to '')

I don't know how important this is, but in parentheses above indicated the values ​​​​that they take in the file by default.

That is, I can run this file in the terminal by writing these arguments after the file name, for example:

python3 get-movies.py -year_from 2005 -regexp 'Terminator'  -genres 'Action' -N 3

Another example:

python3 get-movies.py -N 3 -regexp 'Terminator'

Another example:

python3 get-movies.py -genres 'Comedy' -N 3

Actually, I need to write a bash script that will be called file.sh for example, this file should run the get-movies.py file, which lies in the same directory as file.sh and file.sh should also accept these command line arguments.

That is, if I do: file.sh -N 3 -regexp 'Terminator', then this file.sh should run get-movies.py inside itself with the same parameters, that is, it should do this inside itself: python3 get-movies.py -N 3 -regexp 'Terminator'
But I don’t understand how to write such a script so that these arguments it was possible to transfer in a different order, or it was possible not to transfer at all, but in any case, this file.sh would run get-movies.py.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shurshur, 2021-12-17
@denislysenko

#!/bin/bash
python3 get-movies.py "[email protected]"

Here the notation "[email protected]" means to pass all the arguments of the script as they are, and with the correct escaping, that is, "Termninator 2" will be passed as "Terminator 2", and not "Terminator" "2".
upd: I will also add that for such a simple case, another approach may be more suitable: passing the interpreter through the shebang. You need to specify in the first line of the get-movies.py file:
#!/usr/bin/env python3
In this case, running the script will call /usr/bin/env python3 get-movies.py with all other parameters.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question