IF-Statements not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting IF-Statements not working
# 1  
Old 09-16-2009
[SOLVED]IF-Statements not working

Dear Community,

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:
Code:
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?

Actually I tried every form of the comparison:
Code:
$err=0
$err==0
"$err"="0"
"$err"=="0"
'$err'='0'
 '$err'=='0'
$'err'='0'
$'err'=='0'

I also tried to mix the various format (eg. $err=="0") but really nothing is working.

---------- Post updated at 05:29 AM ---------- Previous update was at 05:22 AM ----------

Nervermind, just realisied that I have to put whitespaces between the argument and the variable...
Thanks anyway!

Last edited by Henry_Ford; 09-16-2009 at 07:30 AM.. Reason: [SOLVED]
# 2  
Old 09-16-2009
Java

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:

Code:
if [[ ${err} -ne 1 ]]
then
   echo "variable not equal to 1"
fi


Last edited by frank_rizzo; 09-16-2009 at 08:24 PM.. Reason: typo
# 3  
Old 09-16-2009
Quote:
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
It would help to know which shell you use.

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.

Code:
# 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


Last edited by methyl; 09-16-2009 at 09:24 PM.. Reason: typo
# 4  
Old 09-17-2009
You guys are amazing!
Appreciate your effort!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple expect/send statements not working in shell script

Hi I am trying the following in my bash script which logs into my machine and runs a command. Trying to solve this using expect. The first expect statement is hit and it enters the address "10.10.0.10" but when the second expect statement is hit it exits #!/bin/bash expect -c ' spawn... (2 Replies)
Discussion started by: skorada
2 Replies

2. Shell Programming and Scripting

Too many if statements..

Hello. I am new here and new to scripting. I used to have a very basic script that worked for simple backup/restore of files. I have expanded it and well... I have ended up with a complete mess. It still backs up and restores but there is so many issues that stem from the many if statements I... (3 Replies)
Discussion started by: gameinn
3 Replies

3. UNIX for Dummies Questions & Answers

if and then statements

I came across a bash script that outputs the forecast for the day and the max temperature but at the end of the day the max temperature disappears ($6) and I am left with "°C" after the forecast. Here is the script: #! /bin/bash curl -s --connect-timeout 30... (7 Replies)
Discussion started by: _light_
7 Replies

4. Homework & Coursework Questions

Using While and If statements

1. The problem statement, all variables and given/known data: Two problems I need solving please. I created a script where the user types in 7 numbers as standard input and each one is then stored in an array. Now I need to perform the following calculations on those numbers: 1) Use a while... (11 Replies)
Discussion started by: jjb1989
11 Replies

5. Shell Programming and Scripting

vi and if statements

Hi I am very new to Unix programming and shell scripting. I am trying t figure out how to write a little script that will output the number of directories. I can find the number of directories using ls -l | grep "^d" | wc -l I can not figure out how to do it so when I type the name... (8 Replies)
Discussion started by: Reddoug
8 Replies

6. Shell Programming and Scripting

Help with IF statements

I am writing a script that does a search for a argument in a file and lists all like occurrences. The script verifies that it is a file and then runs another script that list the lines. My problem is that I need the script to accept a file or a directory and then go to that directory check all... (1 Reply)
Discussion started by: zero3ree
1 Replies

7. Shell Programming and Scripting

HELP!! if statements

I am kind of new in Unix and i have to make a menu. I want to put an if statement in the menu. you should enter the filename and it goes to that file. How do i do this? (1 Reply)
Discussion started by: trob
1 Replies

8. Shell Programming and Scripting

Please help on IF statements.

I had different problem scenarios with IF statement. Can any expert please enlighten me on the difference with these scenarios. Thank you. 1st Scenario: testdate=`date +%Y%m` test=`cat /var/log/database0.$testdate*.log | grep "Errors found during processing" | tail -10` if then ... (4 Replies)
Discussion started by: filthymonk
4 Replies

9. UNIX for Dummies Questions & Answers

Else in If Statements

Sorry to be a pain, but how does the else work in the if statements? Ive been making scripts with if statements but i cant get the else statements working. Can you help? (8 Replies)
Discussion started by: chapmana
8 Replies

10. Shell Programming and Scripting

or statements?

how do i do an or in an if-then statement? i tried: if ; then bleh fi how???? (1 Reply)
Discussion started by: Blip
1 Replies
Login or Register to Ask a Question