'Where did I make a mistake in this linux bash code?
This code always returns the TRUE answer but the value of $myvar is "null"
if [ $myvar = "true" ]
then
cat << EOF > file1
blabla
EOF
echo $myvar
else
cat << EOF > file1
blublu
EOF
echo $myvar
fi
Where did I make a mistake ? Many Thanks Olive
Solution 1:[1]
There are multiple obvious errors in the command.
About your if statement:
First of all, square brackets [] are an alternative version of the test command. You want to use no brackets or parentheses ().
When writing oneliner you need to end each part of the command with a semicolon ; or that your shell knows at which point you start a new command.
Also in nearly all cases you want to quote your variables.
This leaves you with the following:
if "$myvar" = "true"; then echo "is true"; echo "some other command"; else echo "is not true"; echo "another command; fi
I cannot say much about the rest of the command, as it is unclear what you are trying to do with all of these EOFs.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mashuptwice |
