Syntax error: "fi" unexpected - bash

I am trying to write a shell script that will either start or stop openvpn, depending on what the user enters. When I run the script it correctly prompts me for what I want to do. But when I type "1" and hit [Enter] it outputs this and terminates:
./control-openvpn.sh: 27: ./control-openvpn.sh: Syntax error: "fi" unexpected
Here is the main part of my code. The functions are above it in the script.
# Main
echo -n "What to do? 1.Start or 2.Stop. [1/2]"
read action
if [ "$action" == "1" ]
then
start_openvpn()
elif [ "$action" == "2" ]
then
stop_openvpn()
fi
Thank you in advance

In bash, when you do start_openvpn(), you are declaring a new function. Thus, bash gets confused when the next thing it sees is fi. Something like this should work for you:
read -p 'What to do? 1.Start or 2.Stop. [1/2] ' action
if [ $action -eq 1 ]; then
start_openvpn
elif [ $action -eq 2 ]; then
stop_openvpn
fi

Related

Shell Script compare the values with input parameter

apps="http:git.abc.com";
cluster-ui="http:git.xyz.com";
customer-ui="http:git.xxx.com";
SERVICE=$1;
My requirement is if I pass service name as a 'apps' then I need to clone the $apps url.
Here
if [ $Service -eq apps ]
not think a good approach as my repo url might get increased so more and more loop will come
Any suggestions?
The $ sign assigns the input argument, so we're getting first input if it matches the below variable, so do what you want inside if condition.
#!/bin/bash
apps="http:git.abc.com";
clusterui="http:git.xyz.com";
customerui="http:git.xxx.com";
#SERVICE=$1;
#Store global
repo=''
# if empty parameter is passed
if [ $# -lt 1 ] ; then
echo "Parameters Need"
exit 1
fi;
# for search the correct parameter
if [ $1 = "apps" ]; then
repo=$apps
elif [ $1 = "cluster-ui" ] ; then
repo=$clusterui
elif [ $1 = "customer-ui" ] ; then
repo=$customerui
else
echo "Not found"
fi;
echo $repo
Note just repeat elif [ ] ;then for more entries or think!
how to access run this file like this sh ./file.sh apps just replace apps with yours. make sure you have permission to execute the file if you don't have, give it to permission like below
chmod 766 file
now run the shell script sh ./file.sh clusterui
'Case statement' would suit here more than if ladder

bash script syntax error at function call and passing array as argument

I'm new to bash so assume that I don't understand everything in this simple script as I've been putting this together as of today with no prior experience with bash.
I get this error when I run test.sh:
command substitution: line 29: syntax error near unexpected token `$1,'
./f.sh: command substitution: line 29: `index_of($1, $urls))'
FILE: f.sh
#!/bin/bash
urls=( "example.com" "example2.com")
error_exit()
{
echo "$1" 1>&2
exit 1
}
index_of(){
needle=$1
haystack=$2
for i in "${!haystack[#]}"; do
if [[ "${haystack[$i]}" = "${needle}" ]]; then
echo "${i}"
fi
done
echo -1
}
validate_url_param(){
index=-2 #-2 as flag
if [ $# -eq 0 ]; then
error_exit "No url provided. Exiting"
else
index=$(index_of($1, $urls)) #ERROR POINTS TO THIS LINE
if [ $index -eq -1 ]; then
error_exit "Provided url not found in list. Exiting"
fi
fi
echo $index
}
FILE: test.sh
#!/bin/bash
. ./f.sh
index=$(validate_url_param "example.com")
echo $index
echo "${urls[0]}"
I've lost track of all of the tweaks I tried but google is failing me and I'm sure this is some basic stuff so... thanks in advance.
The immediate error, just like the error message tells you, is that shell functions (just like shell scripts) do not require or accept commas between their arguments or parentheses around the argument list. But there are several changes you could make to improve this code.
Here's a refactored version, with inlined comments.
#!/bin/bash
urls=("example.com" "example2.com")
error_exit()
{
# Include script name in error message; echo all parameters
echo "$0: $#" 1>&2
exit 1
}
# A function can't really accept an array. But it's easy to fix:
# make the first argument the needle, and the rest, the haystack.
# Also, mark variables as local
index_of(){
local needle=$1
shift
local i
for ((i=1; i<=$#; ++i)); do
if [[ "${!i}" = "${needle}" ]]; then
echo "${i}"
# Return when you found it
return 0
fi
done
# Don't echo anything on failure; just return false
return 1
}
validate_url_param(){
# global ${urls[#]} is still a bit of a wart
if [ $# -eq 0 ]; then
error_exit "No url provided. Exiting"
else
if ! index_of "$1" "${urls[#]}"; then
error_exit "Provided url not found in list. Exiting"
fi
fi
}
# Just run the function from within the script itself
validate_url_param "example.com"
echo "${urls[0]}"
Notice how the validate_url_param function doesn't capture the output from the function it is calling. index_of simply prints the result to standard output and that's fine, just let that happen and don't intervene. The exit code tells us whether it succeeded or not.
However, reading the URLs into memory is often not useful or necessary. Perhaps you are simply looking for
grep -Fx example.com urls.txt

Shell script syntax error (if, then, else)

I have been trying to make a shell script in bash that will display the following:
You are the super user (When I run the script as root).
You are the user: "user" (When I run the script as a user).
#!/bin/bash/
if { whoami | grep "root" }; then
echo $USER1
else
echo $USER2
fi
I keep recieving these syntax error messages:
script.sh: line 2: syntax error near unexpected token `then'
script.sh: line 2: `if { whoami | grep "root" }; then'
Could someone help me out?
If braces are used to chain commands then the last command must have a command separator after it.
{ foo ; bar ; }
userType="$(whoami)"
if [ "$userType" = "root" ]; then
echo "$USER1"
else
echo "$USER2"
fi
pay attention with your first line, the correct syntax for she-bang is:
#!/bin/bash
everything you put there, is the interpreter of your script, you can also put something like #!/usr/bin/python for python scripts, but your question is about the if statement, so you can do this in two ways in shell script using
if [ test ] ; then doSomething(); fi
or
if (( test )) ; then doSomething(); fi
so to answer your question basically you need to do this
#!/bin/bash
if [ `id -u` -eq 0 ] ; then
echo "you are root sir";
else
echo "you are a normal user"
fi
if (( "$USER" = "root" )); then
echo "you are root sir";
else
echo "you are a normal user"
fi
note that you could use a command using `cmd` or $(cmd) and compare using -eq (equal) or = (same), hope this help you :-)

read y or n to confirm deletion unix shell

I have trying to have a unix shell script ask the user whether they want to delete an item.
I have the following code
I keep getting the following error
./menu.sh 73: [: missing ]
echo "Confirm deletion: (y)es or (n)o: "
read confirmDeletion
if [ "$confirmDeletion"="y"];
then
echo "YES"
else
echo "NO"
pause
fi
I cannot seem to work out what is wrong
Any help will be appreciated
Thanks
You need spaces around the = operator:
if [ "$conformDeletion" = "y" ];
and before the ]

If condition not work regular in bash

Code
if [ $setup==="y" ]
then
echo "kurulum:"$setup
exit
full_dir=$full_dir"/public"
else
echo "Sub-Public folder is exist? [public,web]"
read folder_extend
if [ $folder_extend ]
then
full_dir=$full_dir"/"$folder_extend
fi
fi
Setup param $setup view as "n" after run sh but still condition firts part run. Where wrong code ?
Thanks.
Change it to:
if [ "$setup" = "y" ]
then
echo "kurulum:"$setup
exit
full_dir=$full_dir"/public"
else
echo "Sub-Public folder is exist? [public,web]"
read folder_extend
if [ "$folder_extend" ]
then
full_dir=$full_dir"/"$folder_extend
fi
fi
It should just be a single =, and you need spaces around it. You should also quote variables in most contexts, in case they contain spaces.

Resources