1

I am trying to create a bash script that will prompt user input asking what vpn tunnel that the user would like to connect to. So, for example.. there is pvn1 and vpn2 so it needs to be something like: I understand that in it's current state it is pseudo code..

read -p "Connect to vpn1 or vpn2? (enter vpn1 or vpn2)"

if vpn1
        then /path/to/vpn/cert
if vpn2
        then /path/to/vpn/cert

I would appreciate some guidence with this as the only thing I can find online are "yes/no" input responses.. but these are usually [if "yes" then do "this" .. if "no or other" than do "nothing or exit"...

This example is too simple for what I need done. I need an exact bash command ran for "vpn1" and another exact bash command for "vpn2"

I appreciate all help :)

2 Answers 2

2

You need to give a variable to the read command, and then compare it.

read -p "Connect to vpn1 or vpn2? (enter vpn1 or vpn2)" vpn
case "$vpn" in
    vpn1) do stuff ;;
    vpn2) do other stuff ;;
    *) echo "Enter vpn1 or vpn2" ;;
esac

Replace do stuff and do other stuff with the actual commands you want to run in each case.

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

4 Comments

This is the error that I receive upon using this: syntax error near unexpected token `do'
Those aren't actual commands, they're just placeholders where you put what you want to do.
Ahh, I was thinking that 'do' was the initialization command.. like "if" "$this" "do" {this command}
Yeah, I can see how you might think that, since do is a keyword in for and while.
0

Try this out:

read -p "vpn1 or vpn2? (Enter vpn1 or vpn2)" vpn
if [[ $vpn == "vpn1" ]]
   then 
       /path/to/vpn1/cert
elif [[ $vpn == "vpn2" ]]
   then
       /path/to/vpn2/cert
else 
    echo "vpn1 or vpn2 has to be entered in order to continue"
fi

5 Comments

Note that I didn't test this out.
Note that [[ ]] and == are bash features that aren't available in all shells, so you should only use them in scripts with shebang lines that specify bash (i.e. #!/bin/bash or #!/usr/bin/env bash). The portable equivalent would be e.g. if [ "$vpn" = vpn1 ]; then.
this only eccepts "vpn" as an answer. I tested it and it only accepts whatever is at the end of this line: read -p "vpn1 or vpn2? (Enter vpn1 or vpn2)" vpn
Otherwise it tells me 'echo "vpn1 or vpn2 has to be entered in order to continue"'
@aphexlog It works for me as written. Make sure you've copied it (or my variant) correctly.

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.