I have the following script in my spec file %preun tag:
if [ $1 -eq 0 ]; then
echo "Stopping blah service before uninstalling.."
. /etc/init.d/blahforever stop
echo "blah service stopped."
fi
I get "syntax error near unexpected token `fi'" error
but when I add else block it runs fine:
if [ $1 -eq 0 ]; then
echo "Stopping blah service before uninstalling.."
. /etc/init.d/blahforever stop
echo "blah service stopped."
else
echo "I dont need else block"
fi
But I don't need an else block! How do I get rid of it?
PS: I tried removing the semi colon after the condition and still didn't help.
This is a shell syntax error in the line ". /etc/init.d/blahforever stop".
There is no file to be sourced (i.e. read into the current shell)
called "stop".
Related
I'm attempting to run a bash script in a Jenkins freestyle job, but getting a strange inclusion of extra quotes that's throwing errors in the script.
I finally found the extra quotes by adding what ought to be unnecessary amounts of debugging, so I'm still unsure if it's actually what's causing the issue or not.
Jenkins script (the label parameter in the job is either empty or populated from upstream):
#!/usr/bin/env bash
set -ex
if [[ -z $label ]]; then
echo "label not provided"
labelSend=" "
else
echo "label provided"
labelSend="--label \"$label\" "
fi
./label-script.sh --stack-id $stack_id --update $update "$labelSend"
and the label-script script has a parameter parser like so:
#!/usr/bin/env bash
set -e
print_usage () {
echo
echo "Usage: label-script.sh [OPTIONS]"
echo
echo "Does stuff."
echo
echo " Options:"
echo " --stack-id ID"
echo " --update Use 'true' if this is an update"
echo " --label Optional. Overrides the label"
echo
}
parse_args () {
if [[ $# -eq 0 ]]; then
print_usage
exit 0
fi
while [[ $# -gt 0 ]]
do
key="$1"
case ${key} in
--stack-id) stack_id=$2; shift;;
--update) update=$2; shift;;
--label) label=$2; shift;;
*) print_usage; exit 1;;
esac
shift
done
}
parse_args "$#"
# further script things
Now if I run the Jenkins job, I'm seeing this output:
+ [[ -z testing woo ]]
+ echo 'label provided'
label provided
+ labelSend='--label "testing woo" '
+ ./label-script.sh --stack-id 1 --update false '--label "testing woo" '
and the script prints the help menu rather than continuing with the code.
Note specifically the extra ' around the label parameter when calling the script. I'm thinking this is what's causing my script to fail, as it's not able to parse the given parameter. I have to include label in some form of quotes because it could be a multi-word string that needs to be appropriately quoted for the script to parse.
I've tried every variation of the labelSend=--label $label line that I can think of - nested quotes, no quotes, escaped quotes, etc, with no luck.
Has anyone run into something similar? Is there any quoting method that will get me past this?
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
I am trying to write a script to toggle HAproxy to reroute a user to a maintenance page if a service is down. Here is the part I am having trouble with:
if [ "$idIsValid" = "false" ]; then
if [ "$(cat isUp)" = "true" ]; then
echo "Site is down! Routing to maintenance page."
echo false > isUp
mv haproxy.cfg haproxy.cfg.temp
mv haproxy.cfg.other haproxy.cfg
mv haproxy.cfg.temp haproxy.cfg.other
#restart haproxy
service haproxy restart
echo "Restarting HAproxy"
elif [ "$(cat isUp)" = "false" ]; then
#do nothing since it has already changed
echo "Nothing to do; service is still down."
else
#notify me that isUp is set to something other than true or false, or something else is wrong.
fi
fi
When I run this, I get the error:
status-check.sh: Syntax error: "fi" unexpected
This error is pointing to the nested fi on that second to last line. I am having trouble finding the syntax error here. Please assist! Thank you.
It's because there are no commands in the last else block. If you either remove this block or put a command in there (not a comment) you won't get a syntax error.
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 :-)
I have a wrapper.sh script which call another script run_workflow.sh which eventually calls a workflow. I would like to handle error for run_wrklow.sh...i.e, if the workflow is executed successfully then i need to call another script run_workflow2.sh which triggers another workflow.
Here is the sample code...Please suggest me how to handle errors
wrapper.sh
sh run_workflow.sh #trigger workflow1
if [ $? -ne 0 ]; then
echo "Workflow Failed"
else
echo "Wrokflow Success"
sh run_workflow2.sh #trigger workflow2
if [ $? -ne 0 ]; then
echo "Workflow2 Failed"
else
echo "Workflow2 Success"
fi
fi
However when i try this code I'm not able to return failed status.
Here is my suggestion. You don't need to explicitly test $?, the syntax is that if is followed by a command ([ is the test command).
exit_value=1 # default failure
if sh run_workflow.sh #trigger workflow1
then
echo "Wrokflow Success"
if sh run_workflow2.sh #trigger workflow2
then
echo "Workflow2 Success"
exit_value=0
else
echo "Workflow2 Failed" >&2
fi
else
echo "Workflow Failed" >&2
fi
exit $exit_value
Note that I echo error messages to stderr (>&2). The exit command returns an error, which is an integer between 0-255. By convention we return 0 on success and 1 on error.
I also indented my code, which all experienced programmers do.