Bash if then else syntax












0















I'm trying to do test automation with a bash script using if then else statements but I'm running into a few errors. For one, when I try to execute it I'm doing something wrong with the variable assignment with j and k, because it tells me that the command j and the command k aren't found when I try to execute. How do you correctly create variables?
The most confusing thing though is when I try to execute the script I get an error telling me I have an unexpected token near fi, and then it just says 'fi'. What am I doing wrong here?



#!/bin/bash

j = 0
k = 0

echo Test1:
echo -ne "0nInnUGn" | /u/cgi_web/Tuition/cost
echo Test2:
echo -ne "0nOutnUGn" | /u/cgi_web/Tuition/cost
echo Test3:
echo -ne "0nInnGRn" | /u/cgi_web/Tuition/cost
echo Test4:
echo -ne "0nOutnGRn" | /u/cgi_web/Tuition/cost

for i in {1..17}
do
echo Test$((i+4)):
if[ "$j" -eq 0 ] && [ "$k" -eq 0 ] then
$j = 1
echo -ne "$inInnUGn" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 1 ] && [ "$k" -eq 0 ] then
$k = 1
echo -ne "$inInnGRn" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 1 ] && [ "$k" -eq 1 ] then
$j = 0
echo -ne "$inOutnUGn" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 0 ] && [ "$k" -eq 1 ] then
$k = 0
echo -ne "$inOutnGRn" | /u/cgi_web/Tuition/cost
fi

done


EDIT: I figure out the variable issue with j and k, I had to remove the spaces in the statement.










share|improve this question




















  • 1





    j = 0 -> j=0, k = 0 -> k=0

    – tkausl
    Nov 21 '18 at 20:24






  • 1





    Also you need spaces after your if's.

    – Paul Hodges
    Nov 21 '18 at 20:45






  • 1





    Please take a look: shellcheck.net

    – Cyrus
    Nov 21 '18 at 20:51
















0















I'm trying to do test automation with a bash script using if then else statements but I'm running into a few errors. For one, when I try to execute it I'm doing something wrong with the variable assignment with j and k, because it tells me that the command j and the command k aren't found when I try to execute. How do you correctly create variables?
The most confusing thing though is when I try to execute the script I get an error telling me I have an unexpected token near fi, and then it just says 'fi'. What am I doing wrong here?



#!/bin/bash

j = 0
k = 0

echo Test1:
echo -ne "0nInnUGn" | /u/cgi_web/Tuition/cost
echo Test2:
echo -ne "0nOutnUGn" | /u/cgi_web/Tuition/cost
echo Test3:
echo -ne "0nInnGRn" | /u/cgi_web/Tuition/cost
echo Test4:
echo -ne "0nOutnGRn" | /u/cgi_web/Tuition/cost

for i in {1..17}
do
echo Test$((i+4)):
if[ "$j" -eq 0 ] && [ "$k" -eq 0 ] then
$j = 1
echo -ne "$inInnUGn" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 1 ] && [ "$k" -eq 0 ] then
$k = 1
echo -ne "$inInnGRn" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 1 ] && [ "$k" -eq 1 ] then
$j = 0
echo -ne "$inOutnUGn" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 0 ] && [ "$k" -eq 1 ] then
$k = 0
echo -ne "$inOutnGRn" | /u/cgi_web/Tuition/cost
fi

done


EDIT: I figure out the variable issue with j and k, I had to remove the spaces in the statement.










share|improve this question




















  • 1





    j = 0 -> j=0, k = 0 -> k=0

    – tkausl
    Nov 21 '18 at 20:24






  • 1





    Also you need spaces after your if's.

    – Paul Hodges
    Nov 21 '18 at 20:45






  • 1





    Please take a look: shellcheck.net

    – Cyrus
    Nov 21 '18 at 20:51














0












0








0








I'm trying to do test automation with a bash script using if then else statements but I'm running into a few errors. For one, when I try to execute it I'm doing something wrong with the variable assignment with j and k, because it tells me that the command j and the command k aren't found when I try to execute. How do you correctly create variables?
The most confusing thing though is when I try to execute the script I get an error telling me I have an unexpected token near fi, and then it just says 'fi'. What am I doing wrong here?



#!/bin/bash

j = 0
k = 0

echo Test1:
echo -ne "0nInnUGn" | /u/cgi_web/Tuition/cost
echo Test2:
echo -ne "0nOutnUGn" | /u/cgi_web/Tuition/cost
echo Test3:
echo -ne "0nInnGRn" | /u/cgi_web/Tuition/cost
echo Test4:
echo -ne "0nOutnGRn" | /u/cgi_web/Tuition/cost

for i in {1..17}
do
echo Test$((i+4)):
if[ "$j" -eq 0 ] && [ "$k" -eq 0 ] then
$j = 1
echo -ne "$inInnUGn" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 1 ] && [ "$k" -eq 0 ] then
$k = 1
echo -ne "$inInnGRn" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 1 ] && [ "$k" -eq 1 ] then
$j = 0
echo -ne "$inOutnUGn" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 0 ] && [ "$k" -eq 1 ] then
$k = 0
echo -ne "$inOutnGRn" | /u/cgi_web/Tuition/cost
fi

done


EDIT: I figure out the variable issue with j and k, I had to remove the spaces in the statement.










share|improve this question
















I'm trying to do test automation with a bash script using if then else statements but I'm running into a few errors. For one, when I try to execute it I'm doing something wrong with the variable assignment with j and k, because it tells me that the command j and the command k aren't found when I try to execute. How do you correctly create variables?
The most confusing thing though is when I try to execute the script I get an error telling me I have an unexpected token near fi, and then it just says 'fi'. What am I doing wrong here?



#!/bin/bash

j = 0
k = 0

echo Test1:
echo -ne "0nInnUGn" | /u/cgi_web/Tuition/cost
echo Test2:
echo -ne "0nOutnUGn" | /u/cgi_web/Tuition/cost
echo Test3:
echo -ne "0nInnGRn" | /u/cgi_web/Tuition/cost
echo Test4:
echo -ne "0nOutnGRn" | /u/cgi_web/Tuition/cost

for i in {1..17}
do
echo Test$((i+4)):
if[ "$j" -eq 0 ] && [ "$k" -eq 0 ] then
$j = 1
echo -ne "$inInnUGn" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 1 ] && [ "$k" -eq 0 ] then
$k = 1
echo -ne "$inInnGRn" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 1 ] && [ "$k" -eq 1 ] then
$j = 0
echo -ne "$inOutnUGn" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 0 ] && [ "$k" -eq 1 ] then
$k = 0
echo -ne "$inOutnGRn" | /u/cgi_web/Tuition/cost
fi

done


EDIT: I figure out the variable issue with j and k, I had to remove the spaces in the statement.







bash syntax-error






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 20:29







csStudent

















asked Nov 21 '18 at 20:20









csStudentcsStudent

487




487








  • 1





    j = 0 -> j=0, k = 0 -> k=0

    – tkausl
    Nov 21 '18 at 20:24






  • 1





    Also you need spaces after your if's.

    – Paul Hodges
    Nov 21 '18 at 20:45






  • 1





    Please take a look: shellcheck.net

    – Cyrus
    Nov 21 '18 at 20:51














  • 1





    j = 0 -> j=0, k = 0 -> k=0

    – tkausl
    Nov 21 '18 at 20:24






  • 1





    Also you need spaces after your if's.

    – Paul Hodges
    Nov 21 '18 at 20:45






  • 1





    Please take a look: shellcheck.net

    – Cyrus
    Nov 21 '18 at 20:51








1




1





j = 0 -> j=0, k = 0 -> k=0

– tkausl
Nov 21 '18 at 20:24





j = 0 -> j=0, k = 0 -> k=0

– tkausl
Nov 21 '18 at 20:24




1




1





Also you need spaces after your if's.

– Paul Hodges
Nov 21 '18 at 20:45





Also you need spaces after your if's.

– Paul Hodges
Nov 21 '18 at 20:45




1




1





Please take a look: shellcheck.net

– Cyrus
Nov 21 '18 at 20:51





Please take a look: shellcheck.net

– Cyrus
Nov 21 '18 at 20:51












2 Answers
2






active

oldest

votes


















1














Bash if statements require a semi-colon before the then:



if [ condition ] || [ condition ]; then
# code
elif [ condition ] && [ condition ]; then
# code
fi


For example.






share|improve this answer



















  • 1





    ... Or a newline, of course.

    – AlexP
    Nov 21 '18 at 20:44











  • Technically inaccurate. In fact, I prefer newlines with no semicolons.

    – Paul Hodges
    Nov 21 '18 at 20:44



















1














To help anyone who might look at this for help in the future, I figured I'd answer my own question with all the syntax errors I found from my own testing and with the helpful responses of others.
To start the variable assignment:



j = 0


you can't have spaces in between, so it would be:



j=0


Also if statements need a space between if and the bracket and need a semicolon after the last bracket before then. Therefore my incorrect if statement



if[ "$j" -eq 0 ] && [ "$k" -eq 0 ] then


becomes



if [ "$j" -eq 0 ] && [ "$k" -eq 0 ]; then


or instead of a semicolon you can have a new line between the bracket, so it would become



if [ "$j" -eq 0 ] && [ "$k" -eq 0 ]
then





share|improve this answer





















  • 1





    In many languages, spaces can often be added or removed to improve readability without changing the meaning. But in shell syntax, this is not the case. In the shell, spaces are important delimiters, and adding or removing them can change the meaning of a command, sometimes in unexpected ways. In the test expression, spaces are required between each element. In the assignment, they're forbidden. There are a few places where spaces are optional, but they're rare.

    – Gordon Davisson
    Nov 22 '18 at 1:04











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419917%2fbash-if-then-else-syntax%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Bash if statements require a semi-colon before the then:



if [ condition ] || [ condition ]; then
# code
elif [ condition ] && [ condition ]; then
# code
fi


For example.






share|improve this answer



















  • 1





    ... Or a newline, of course.

    – AlexP
    Nov 21 '18 at 20:44











  • Technically inaccurate. In fact, I prefer newlines with no semicolons.

    – Paul Hodges
    Nov 21 '18 at 20:44
















1














Bash if statements require a semi-colon before the then:



if [ condition ] || [ condition ]; then
# code
elif [ condition ] && [ condition ]; then
# code
fi


For example.






share|improve this answer



















  • 1





    ... Or a newline, of course.

    – AlexP
    Nov 21 '18 at 20:44











  • Technically inaccurate. In fact, I prefer newlines with no semicolons.

    – Paul Hodges
    Nov 21 '18 at 20:44














1












1








1







Bash if statements require a semi-colon before the then:



if [ condition ] || [ condition ]; then
# code
elif [ condition ] && [ condition ]; then
# code
fi


For example.






share|improve this answer













Bash if statements require a semi-colon before the then:



if [ condition ] || [ condition ]; then
# code
elif [ condition ] && [ condition ]; then
# code
fi


For example.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 20:35









NunchyNunchy

825411




825411








  • 1





    ... Or a newline, of course.

    – AlexP
    Nov 21 '18 at 20:44











  • Technically inaccurate. In fact, I prefer newlines with no semicolons.

    – Paul Hodges
    Nov 21 '18 at 20:44














  • 1





    ... Or a newline, of course.

    – AlexP
    Nov 21 '18 at 20:44











  • Technically inaccurate. In fact, I prefer newlines with no semicolons.

    – Paul Hodges
    Nov 21 '18 at 20:44








1




1





... Or a newline, of course.

– AlexP
Nov 21 '18 at 20:44





... Or a newline, of course.

– AlexP
Nov 21 '18 at 20:44













Technically inaccurate. In fact, I prefer newlines with no semicolons.

– Paul Hodges
Nov 21 '18 at 20:44





Technically inaccurate. In fact, I prefer newlines with no semicolons.

– Paul Hodges
Nov 21 '18 at 20:44













1














To help anyone who might look at this for help in the future, I figured I'd answer my own question with all the syntax errors I found from my own testing and with the helpful responses of others.
To start the variable assignment:



j = 0


you can't have spaces in between, so it would be:



j=0


Also if statements need a space between if and the bracket and need a semicolon after the last bracket before then. Therefore my incorrect if statement



if[ "$j" -eq 0 ] && [ "$k" -eq 0 ] then


becomes



if [ "$j" -eq 0 ] && [ "$k" -eq 0 ]; then


or instead of a semicolon you can have a new line between the bracket, so it would become



if [ "$j" -eq 0 ] && [ "$k" -eq 0 ]
then





share|improve this answer





















  • 1





    In many languages, spaces can often be added or removed to improve readability without changing the meaning. But in shell syntax, this is not the case. In the shell, spaces are important delimiters, and adding or removing them can change the meaning of a command, sometimes in unexpected ways. In the test expression, spaces are required between each element. In the assignment, they're forbidden. There are a few places where spaces are optional, but they're rare.

    – Gordon Davisson
    Nov 22 '18 at 1:04
















1














To help anyone who might look at this for help in the future, I figured I'd answer my own question with all the syntax errors I found from my own testing and with the helpful responses of others.
To start the variable assignment:



j = 0


you can't have spaces in between, so it would be:



j=0


Also if statements need a space between if and the bracket and need a semicolon after the last bracket before then. Therefore my incorrect if statement



if[ "$j" -eq 0 ] && [ "$k" -eq 0 ] then


becomes



if [ "$j" -eq 0 ] && [ "$k" -eq 0 ]; then


or instead of a semicolon you can have a new line between the bracket, so it would become



if [ "$j" -eq 0 ] && [ "$k" -eq 0 ]
then





share|improve this answer





















  • 1





    In many languages, spaces can often be added or removed to improve readability without changing the meaning. But in shell syntax, this is not the case. In the shell, spaces are important delimiters, and adding or removing them can change the meaning of a command, sometimes in unexpected ways. In the test expression, spaces are required between each element. In the assignment, they're forbidden. There are a few places where spaces are optional, but they're rare.

    – Gordon Davisson
    Nov 22 '18 at 1:04














1












1








1







To help anyone who might look at this for help in the future, I figured I'd answer my own question with all the syntax errors I found from my own testing and with the helpful responses of others.
To start the variable assignment:



j = 0


you can't have spaces in between, so it would be:



j=0


Also if statements need a space between if and the bracket and need a semicolon after the last bracket before then. Therefore my incorrect if statement



if[ "$j" -eq 0 ] && [ "$k" -eq 0 ] then


becomes



if [ "$j" -eq 0 ] && [ "$k" -eq 0 ]; then


or instead of a semicolon you can have a new line between the bracket, so it would become



if [ "$j" -eq 0 ] && [ "$k" -eq 0 ]
then





share|improve this answer















To help anyone who might look at this for help in the future, I figured I'd answer my own question with all the syntax errors I found from my own testing and with the helpful responses of others.
To start the variable assignment:



j = 0


you can't have spaces in between, so it would be:



j=0


Also if statements need a space between if and the bracket and need a semicolon after the last bracket before then. Therefore my incorrect if statement



if[ "$j" -eq 0 ] && [ "$k" -eq 0 ] then


becomes



if [ "$j" -eq 0 ] && [ "$k" -eq 0 ]; then


or instead of a semicolon you can have a new line between the bracket, so it would become



if [ "$j" -eq 0 ] && [ "$k" -eq 0 ]
then






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 20:59

























answered Nov 21 '18 at 20:45









csStudentcsStudent

487




487








  • 1





    In many languages, spaces can often be added or removed to improve readability without changing the meaning. But in shell syntax, this is not the case. In the shell, spaces are important delimiters, and adding or removing them can change the meaning of a command, sometimes in unexpected ways. In the test expression, spaces are required between each element. In the assignment, they're forbidden. There are a few places where spaces are optional, but they're rare.

    – Gordon Davisson
    Nov 22 '18 at 1:04














  • 1





    In many languages, spaces can often be added or removed to improve readability without changing the meaning. But in shell syntax, this is not the case. In the shell, spaces are important delimiters, and adding or removing them can change the meaning of a command, sometimes in unexpected ways. In the test expression, spaces are required between each element. In the assignment, they're forbidden. There are a few places where spaces are optional, but they're rare.

    – Gordon Davisson
    Nov 22 '18 at 1:04








1




1





In many languages, spaces can often be added or removed to improve readability without changing the meaning. But in shell syntax, this is not the case. In the shell, spaces are important delimiters, and adding or removing them can change the meaning of a command, sometimes in unexpected ways. In the test expression, spaces are required between each element. In the assignment, they're forbidden. There are a few places where spaces are optional, but they're rare.

– Gordon Davisson
Nov 22 '18 at 1:04





In many languages, spaces can often be added or removed to improve readability without changing the meaning. But in shell syntax, this is not the case. In the shell, spaces are important delimiters, and adding or removing them can change the meaning of a command, sometimes in unexpected ways. In the test expression, spaces are required between each element. In the assignment, they're forbidden. There are a few places where spaces are optional, but they're rare.

– Gordon Davisson
Nov 22 '18 at 1:04


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419917%2fbash-if-then-else-syntax%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory