0

I don't have much experiences on bash, and I need to run a python file from my Ubuntu VM terminal. The python file needs some user inputs passed in as arguments, I am wondering if there is any way to let me have customised options that followed by user inputs.

I have tried 'alias options =''' but it is not something that I need here unfortunately.

I'd like to have something like this:

 python test.py -name [name] -gender [gender] 

Hope someone would help.

7
  • 3
    You can write a short script to handle the options and then call the script. If you want to require the order of options to be -name and -gender and remove those two option label and pass simply Joe M it becomes trivial using the positional parameters $1 and $2. Commented Jul 4, 2019 at 12:47
  • 4
    Aliases don't let you manipulate arguments, consider using a function instead : function my_python_script() { python test.py -name "$1" -gender "$2"; }. That said if you can modify the python script then I'd go with David C. Rankin's suggestion. Commented Jul 4, 2019 at 13:03
  • 1
    Your python program would be responsible for parsing the commandline arguments. Read about argparse and/or getopt Commented Jul 4, 2019 at 14:49
  • @glennjackman I thought the same, but we both probably misunderstood the question at first glance. It seems like OP is not the author of the python script and the script already parses command line arguments. I think OP wants a shortcut to call the script using some fixed plus some custom arguments without having to write out the fixed ones each time. Commented Jul 4, 2019 at 15:08
  • @Aaron, ...aside: better to take out the function there; my_python_script() { ...; } is compatible with all POSIX-compliant shells, whereas function my_python_script() { ...; } is only available in those that support a mismash of POSIX and legacy ksh syntax (the latter being just function my_python_script { with no ()). See wiki.bash-hackers.org/scripting/obsolete Commented Jul 5, 2019 at 14:17

1 Answer 1

1

Ok, I think I understand your question, but if I'm wrong feel free to let me know.

So you want to run a script, said script takes command line arguments and you want to accept user input from the terminal when running the script. There are multiple approaches, A simple one would be declaring a function to wrap the logic of parsing stdin and creation the options, as in:

root@mybox[/tmp] Fri Jul 05 <09:07:24>
--> function wrap_script {
> read -p 'Enter gender:' opt1
> read -p 'Enter name:' opt2
> echo "Here you'd call test.py using -gender $opt1 and -name $opt2"
> }
root@mybox[/tmp] Fri Jul 05 <09:08:14>
--> wrap_script
Enter gender:foo
Enter name:bar
Here you'd call test.py using -gender foo and -name bar
root@mybox[/tmp] Fri Jul 05 <09:08:19>

If you want to keep the function, make sure it's sourced upon login, either directly in your .bash_profile or by sourcing some other file.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this is what I need in my case!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.