I'm parsing the result of a curl call into a variable this way:
result=$(curl some curl parameters)
I'm then making a check:
if [ $result != "job completed" ];
then printf "ok"
fi
but I'm getting the following error:
[: too many arguments
any idea why?
It's almost certainly because you haven't stringified the result, meaning that the [ command is being given more than three arguments.
If it contains the string job completed, you will end up with:
if [ job completed != "job completed" ] ...
I suggest you try:
if [ "$result" != "job completed" ] ...
Related
I am running the following in bash:
read -p "Server: " SERVER
read -p "Username: " NAME
read -p "UserPassword: " USERPASSWORD
curl -s -u ${NAME}:${USERPASSWORD} http://<REMOTE_SERVER_DETAILS>
if [ $http_response != "200" ] then
echo "Error"
else
echo "Created Successfully."
fi
The curl command runs successfully . All i want to output to the user is the status , ie the http response header , but it gives an error
syntax error near unexpected token `else'
Any ideas as to why this is giving syntax error?
Also can i output the error ?
Two solutions: extra semicolon or line break afer ].
if [ "$http_response" != "200" ]; then
if [ "$http_response" != "200" ]
then
I've implemented script like below:
podName=someValue ; // Here value is dynamic (empty or non empty)
if[ $podName == " "] then
echo "Empty"
Even though I got empty an output but still could see:
if[ == ] : not found [No such file or directory ]
error message while running the script.
Seems like there is a small formatting issue which is causing this error. This should be the correct code.
podName=someValue
if [ "$podName" == " " ]; then
echo "Empty"
fi
You can check if the string is empty in bash / sh like this (some containers don't have bash shell):
podName="someValue"
if [ -z "$podName" ]; then
echo "Empty"
fi
or:
podName="someValue"
if [ "$podName" = "" ]; then
echo "Empty"
fi
The following syntax works only in bash [ "$podName" == "" ]
My first question is why putting ! in the front of if statement fails to produce syntax error when status is not double quoted. That is how is [ ! $x = "string" ] different from
[ $x != "string" ]?
My script is as follows.
#!/bin/bash
status=" "
# Comparison 1: without error
echo "-----Comparison 1-----"
if [ ! $status = "success" ]
then echo 'Error: status was not success but: ' $status
else
echo "The status is success."
fi
echo "-----Comparison 2-----"
# Comparison 2: with error message but still shows success
if [ $status != "success" ]
then echo 'Error: status was not success but: ' $status
else
echo "The status is success."
fi
echo "-----Comparison 3-----"
# Comparison 3: Correct result after quoting status
if [ ! "$status" == "success" ]
then echo 'Error: status was not success but: ' $status
else
echo "The status is success."
fi
echo "-----Comparison 4-----"
# Comparison 4: Correct result after quoting status
if [ "$status" != "success" ]
then echo 'Error: status was not success but: ' $status
else
echo "The status is success."
fi
The output is
-----Comparison 1-----
The status is success.
-----Comparison 2-----
./test2.sh: line 14: [: !=: unary operator expected
The status is success.
-----Comparison 3-----
Error: status was not success but:
-----Comparison 4-----
Error: status was not success but:
Additional questions
Why does it produce "The status is success." in Comparison 2 after a syntax error? How would a syntax error in an if statement affects the evaluated result of such if statement?
p.s. I know we need "" around $status to make output right.
You need to quote $status. Without the quotes, your first comparison is
if [ ! = "success" ]
which will fail because ! and success are not equal strings.
Your second one results in a syntactically invalid expression for [:
if [ != "success" ]
The syntax error you see in condition 2 isn't a shell syntax error, but a condition syntax error raised by the [ command. The command itself runs fine, but exits with a non-zero exit status.
I have a simple shell script which I want to set up as a periodic Jenkins job rather than a cronjob for visibility and usability for less experienced users.
Here is the script:
#!/bin/bash
outputfile=/opt/jhc/streaming/check_error_output.txt
if [ "grep -sq 'Unable' $outputfile" == "0" ]; then
echo -e "ERROR MESSAGE FOUND\n"
exit 1
else
echo -e "NO ERROR MESSAGES HAVE BEEN FOUND\n"
exit 0
fi
My script will always return "NO ERROR MESSAGES HAVE BEEN FOUND" regardless of whether or not 'Unable' is in $outputfile, what am I doing wrong?
I also need my Jenkins job to class this as a success if 'Unable' isn't found (e.g. If script returns "0" then pass, everything else is fail)
Execute the grep command and check the exit status instead:
#!/bin/bash
outputfile=/opt/jhc/streaming/check_error_output.txt
grep -sq 'Unable' $outputfile
if [ "$?" == "0" ]; then
echo -e "ERROR MESSAGE FOUND\n"
exit 1
else
echo -e "NO ERROR MESSAGES HAVE BEEN FOUND\n"
exit 0
fi
You are comparing two different strings. The outcome will always be false, i.e. the else part is taken.
Also, no need to explicitly query the status code. Do it like this:
if grep -sq 'Unable' $outputfile
then
....
else
....
fi
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