I tried for over 4 days to figur this out.
I got a Shell-Code which contains some If-statements which are driving me crazy.
First of all the statements:
err=0;
echo "If-Test begins..."
if ! [[ "$err"==1 ]];then
echo "If NOT 0"
fi
if [[ "$err"!=0 ]];then
echo "If NOT (inside braces) 0"
fi
if [[ "$err"==1 ]];then
echo "If 0"
fi
if [[ "$err"==1 ]];then
echo "Using \" for 0"
fi
As "err" is 0 the last two statements shouldn't become true.
But when I am running the scrip the secon, third and fourth statement are returning "true" back, the first one not.
I really cannot get it... Where is my mistake?
All of the tests in your post are for strings. while this may work I would suggest using the correct operators for numeric tests. -eq,-gt,-lt,-ne,-ge,-le. I also recommend indenting your code three spaces for readability.
ie:
if [[ ${err} -ne 1 ]]
then
echo "variable not equal to 1"
fi
In general the number of square brackets matters. There is rarely a reason to use "[[ condition ]]" when "[ test ]" would do.
Paraphrasing and decomplicating your code. I have removed surplus semi-colons, moved exclamation marks (meaning NOT), replaced string comparisons "=" with integer comparisons "-eq", and inserted spaces where required. The last two tests are identical, so I have removed the duplicate.
Enjoy.
# Script starts here
err=0
echo "If-Test begins..."
if [ ${err} -eq 1 ]
then
echo "If ${err} equals 1"
fi
if [ ! ${err} -eq 0 ]
then
echo "If NOT (inside braces) ${err} NOT equals zero"
fi
if [ ${err} -eq 1 ]
then
echo "If (inside braces) ${err} equals one"
fi