0

I have two python scrips (direct.py and nat.py) and I would like to make a bash script to select which python to start based on my input.

Also, when manually running those py scripts I have input options (0...8,h and q). When press 'q' I have sys.exit().

eg:

./start.sh and output should be something like:
Chose your mode: (if type nat)
python3 nat.py

now python script is displayed and if press q, back to bash to chose option.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Boris
  • 5
  • 1
    echo the options, use read to get input and use switch case depends on input. – Siva Oct 23 '18 at 08:42
  • I have made it and it works good with cases, but is there a way to return at chose options when stopping py script ? – Boris Oct 23 '18 at 09:04
  • @Boris, put the read and switch case in an infinite while loop? If needed, you would then probably need a trap to catch ctrl-c properly: https://unix.stackexchange.com/questions/42287/terminating-an-infinite-loop – Gohu Oct 23 '18 at 09:39

3 Answers3

0

Set up an infinite loop with a prompt for the user input; if the input matches one of the options, then (here, pretend to) run that option. If they enter an invalid option, tell them so; if they want to quit, let them out. Once the user exits the corresponding python script (or enters an invalid option), they'll be returned to the loop to choose an option again.

#!/bin/sh

while :
do
  printf 'Choose your mode (nat or direct) or q to quit: '
  read REPLY
  case $REPLY in
    (nat)       echo python3 nat.py
        ;;
    (direct)    echo python3 direct.py
        ;;
    (q)         break
        ;;
    (*)         echo Unknown option
        ;;
  esac
done
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
0

Thank you for replies.
Actually I have done it via atexit from py scripts.

def change():
import subprocess
subprocess.call("/home/VPN/vpn.sh", shell=True)

import atexit
atexit.register(change)

def quit(param):
import sys
sys.exit()

Boris
  • 5
0

With bash, you might want to consider the select builtin like

select R in nat direct quit; do [ $REPLY -ge 3 ] && break; echo python3 $R.py; done
RudiC
  • 8,969