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 2 Weeks Ago
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 2 Weeks Ago
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 27 June 2017, 8:38 AM EDT
Location: NM
Posts: 11,095
Thanks: 537
Thanked 1,068 Times in 990 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 2 Weeks Ago
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 2 Weeks Ago
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 27 June 2017, 4:29 PM EDT
Location: Saskatchewan
Posts: 22,256
Thanks: 1,105
Thanked 4,200 Times in 3,885 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 2 Weeks Ago
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 27 June 2017, 2:26 PM EDT
Location: Simplicity
Posts: 3,582
Thanks: 282
Thanked 1,191 Times in 1,077 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 2 Weeks Ago 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 04:47 PM.