Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Whats wrong with this If statement?

Shell Programming and Scripting


Tags
bash scripting, if statement

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-13-2017
Gevni Gevni is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 13 June 2017, 10:14 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Whats wrong with this If statement?

Hi
I am pretty new to bash scripting.I am trying to write the if statement in bash and it give me error. Can you please help me what I am doing wrong in If statement?


Code:
if [$DB_SIZE%$kk==0] && [$DB_SIZE/$kk<=$M] && [$DB_SIZE/$kk >= $divide]
then

			fector=$kk;
			divide=$DB_SIZE/$kk;
			echo "factor value:$fector"
			echo"divide value:$divide"
			

fi

line 1: [1024%2==0]: command not found

Sponsored Links
    #2  
Old Unix and Linux 06-13-2017
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 19 August 2017, 11:00 PM EDT
Location: NM
Posts: 11,141
Thanks: 555
Thanked 1,083 Times in 1,004 Posts
The [ and ] are actually command keywords like echo and ls. They have to have spaces around them so that bash can see they are "words".

Next - the && and || syntax is not recognized by [ but [[ does recognize it.


Code:
if  [[ [ $DB_SIZE % $kk==0 ] && [ $(( $DB_SIZE / $kk )) <= $M ] && [ $(($DB_SIZE / $kk )) >= $divide ]  ]]

Please note: I just lost my glasses so proof reading this is problematic - I may have a typo.
Sponsored Links
    #3  
Old Unix and Linux 06-13-2017
Gevni Gevni is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 13 June 2017, 10:14 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Thanks for your reply with this I am getting this error

Code:
if  [[ [ ($DB_SIZE % $kk ) == 0 ] && [ ( $DB_SIZE / $kk ) <= $M ] && [ ($DB_SIZE / $kk ) >= $divide ]  ]];
then
.....

Error:  conditional binary operator expected

---------- Post updated at 08:01 AM ---------- Previous update was at 08:01 AM ----------

Hi Thanks for your reply with this I am getting this error

Code:
if  [[ [ ($DB_SIZE % $kk ) == 0 ] && [ ( $DB_SIZE / $kk ) <= $M ] && [ ($DB_SIZE / $kk ) >= $divide ]  ]];
then
.....

Error:  conditional binary operator expected

    #4  
Old Unix and Linux 06-13-2017
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 18 August 2017, 6:48 PM EDT
Location: Saskatchewan
Posts: 22,336
Thanks: 1,116
Thanked 4,216 Times in 3,899 Posts
You removed all the syntax which lets it work to make it prettier. Try the code you were given.
Sponsored Links
    #5  
Old Unix and Linux 06-13-2017
MadeInGermany MadeInGermany is online now Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 20 August 2017, 7:45 AM EDT
Location: Simplicity
Posts: 3,682
Thanks: 298
Thanked 1,229 Times in 1,110 Posts
The numeric operations like / work in $(( )).
Within [ ] and [[ ]] you do numerical comparison with -eq (equivalent), -gt (greater than), -le (less|equal), etc.
Within $(( )) you do not need to $-prefix referenced variables.

Code:
if [[ $(( DB_SIZE % kk )) -eq 0 && $(( DB_SIZE / kk )) -le $M && $(( DB_SIZE / kk )) -ge $divide ]]
then

Also the shell can handle &&. Sometimes this is better readable

Code:
if [[ $(( DB_SIZE % kk )) -eq 0 ]] && [[ $(( DB_SIZE / kk )) -le $M ]] && [[ $(( DB_SIZE / kk )) -ge $divide ]]
then

---------- Post updated at 12:22 ---------- Previous update was at 12:08 ----------

The previous should work in all Posix-compliant shells.
The following works in bash (and ksh and zsh I think):

Code:
if (( DB_SIZE % kk == 0 && DB_SIZE / kk <= M && DB_SIZE / kk >= divide ))
then

Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Whats wrong with this If statement? Gevni UNIX for Beginners Questions & Answers 1 06-13-2017 09:40 AM
Whats wrong with this if-else skyineyes UNIX for Dummies Questions & Answers 8 12-12-2011 11:13 AM
Whats wrong with the following Larry_1 Homework & Coursework Questions 1 12-16-2010 02:51 PM
whats wrong with this? ferrycorsten73 UNIX for Dummies Questions & Answers 1 04-07-2009 02:41 PM
whats wrong with this find statement ? crackthehit007 Shell Programming and Scripting 2 03-26-2009 04:52 AM



All times are GMT -4. The time now is 07:52 AM.