0

I am trying to make a VPN using open vpn on my raspberry pi, and I am currently typing a file using the terminal to create a profile script for the devices I want to connect. Using the step by step guide by BBC. I have been trying to fix this problem for about an hour now and I keep getting the same error please help.

#!/bin/bash
DEFAULT="Default.txt"
FILEEXT=".ovpn"
CRT=".crt"
KEY=".3des.key"
CA="ca.crt"
TA="ta.key"

echo "Please enter an existing Client Name:"
read NAME

if [!-f$NAME$CRT]; then
echo "[ERROR]: Client Public Key Certificate not found: $NAME$CRT"
exit
fi
echo "Client's cert found:$NAME$CR"
if [!-f$NAME$KEY]; then
echo "[ERROR]: Client 3des Private Key not found:$NAME$KEY"
exit
fi
echo "Client's Private Key found:$NAME$KEY"

Error From Code

  • Not exactly a duplicate (not marking it as one), but relevant: http://stackoverflow.com/questions/9581064/why-should-be-there-a-space-after-and-before-in-the-bash-script – Benjamin W. Feb 20 '16 at 09:09

3 Answers3

3

Try to add additional spaces inside your if statement:

if [ ! -f $NAME$CRT ]; then

The complete script is now:

#!/bin/bash
DEFAULT="Default.txt"
FILEEXT=".ovpn"
CRT=".crt"
KEY=".3des.key"
CA="ca.crt"
TA="ta.key"

echo "Please enter an existing Client Name:"
read NAME

if [ ! -f $NAME$CRT ]; then
echo "[ERROR]: Client Public Key Certificate not found: $NAME$CRT"
exit
fi
echo "Client's cert found:$NAME$CR"

Here's a demonstration showing that it works:

$ chmod +x ./myscript 

$ touch foo.crt

$ ./myscript 
Please enter an existing Client Name:
foo
Client's cert found:foo

$ ./myscript
Please enter an existing Client Name:
bar
[ERROR]: Client Public Key Certificate not found: bar.crt
that other guy
  • 116,971
  • 11
  • 170
  • 194
Edgar Rokjān
  • 17,245
  • 4
  • 40
  • 67
  • `test` syntax is just plain weird, and `bash` follows it pretty closely, though there are some extensions. If the pi doesn't have help installed, this link might help: http://www.computerhope.com/unix/test.htm To make life fun, under `sh`, `[` is just a link to `test` While this command is built into `bash`, it keeps the same syntax. – Jeremy J Starcher Feb 19 '16 at 19:24
  • I seemed to get the same error :/ ./MakeOVPN.sh: line 12: syntax error near unexpected token `then' ./MakeOVPN.sh: line 12: `if[ ! -f $NAME$CRT ]; then' – Dan The Gamer Feb 19 '16 at 21:03
  • @EdgarRokyan once i have done that i get command not found error on all the my if statements ...? – Dan The Gamer Feb 19 '16 at 23:11
  • @DanTheGamer Can you update your question after following this advice, and include the new errors? Also, [ShellCheck](http://www.shellcheck.net) is helpful for these things. – that other guy Feb 19 '16 at 23:51
  • @thatotherguy Thanks for your great edit! Now the answer looks much more clear! – Edgar Rokjān Feb 20 '16 at 08:10
  • @DanTheGamer do you really see no difference between your code and code from my answer? Edit your *if* statements properly as it's mentioned in the answer. – Edgar Rokjān Feb 21 '16 at 14:18
  • @EdgarRokyan Sorry i didn't much notice in the reedit of the answer and allowed me to fix the problem. Thank you very much. – Dan The Gamer Feb 23 '16 at 20:28
-2
Try this 

#!/bin/bash
`DEFAULT="Default.txt"
FILEEXT=".ovpn"
CRT=".crt"
KEY=".3des.key"
CA="ca.crt"
TA="ta.key"

echo "Please enter an existing Client Name:"
read NAME

if[ !-f $NAME$CRT ]; then
echo "[ERROR]: Client Public Key Certificate not found: $NAME$CRT"
exit
fi
echo "Client's cert found:$NAME$CR"
  • ./MakeOVPN.sh: line 12: syntax error near unexpected token `then' ./MakeOVPN.sh: line 12: `if[ ! -f $NAME$CRT ]; then' – Dan The Gamer Feb 19 '16 at 21:02
  • This is pretty close, but it's missing vital spaces after `if` and after the `!`. Without this, it's not valid syntax. – that other guy Feb 19 '16 at 23:59
  • See [Edgar's answer](http://stackoverflow.com/a/35512947/1899640) for a working script, including how to test that it works. Your script (ignoring the leading backtick) gives ``./myscript: line 12: syntax error near unexpected token `then' ./myscript: line 12: `if[ !-f $NAME$CRT ]; then`` because of the missing spaces. – that other guy Feb 22 '16 at 18:16
-2

All I did was add a space after the if and it got past your error.

#!/bin/bash
DEFAULT="Default.txt"
FILEEXT=".ovpn"
CRT=".crt"
KEY=".3des.key"
CA="ca.crt"
TA="ta.key"

echo "Please enter an existing Client Name:"
read NAME

if [ ! -f $NAME$CRT ]; then
echo "[ERROR]: Client Public Key Certificate not found: $NAME$CRT"
exit
fi
echo "Client's cert found:$NAME$CR"
tale852150
  • 1,618
  • 3
  • 17
  • 23
  • This gets me to the next bit but i also get an error about the command not found and the statement isnt run console below – Dan The Gamer Feb 19 '16 at 22:51
  • pi@raspberrypi:/etc/openvpn/easy-rsa/keys $ ./MakeOVPN.sh Please enter an existing Client Name: DanielIphone ./MakeOVPN.sh: line 12: [!-fDanielIphone.crt]: command not found Client's cert found:DanielIphone ./MakeOVPN.sh: line 18: syntax error near unexpected token `then' ./MakeOVPN.sh: line 18: `if[!-f$NAME$KEY]; then' pi@raspberrypi:/etc/openvpn/easy-rsa/keys $ – Dan The Gamer Feb 19 '16 at 22:51
  • This does not check whether the file exists. It instead tries to execute a command named by a glob, which is certainly not the intention. – that other guy Feb 19 '16 at 23:58
  • I wasn't attempting to fix your entire script, I was just attempting to get past your initial error. I have updated the code to check for file exists, which what I believe you are asking for now. – tale852150 Feb 21 '16 at 22:36
  • This still does not work: there is a missing space before the final `]`. Please see [Edgar's answer](http://stackoverflow.com/a/35512947/1899640) for how to correctly check that the file doesn't exist, including how to test the script to verify that it works. – that other guy Feb 22 '16 at 18:14
  • I've updated my answer to reflect the changes you mentioned and to follow Edgar's answer. Thank you. – tale852150 Apr 19 '16 at 16:03