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   -   Original Discussion by Gevni
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   -   Original Discussion by Gevni
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 22 November 2017, 2:26 PM EST
Location: NM
Posts: 11,240
Thanks: 571
Thanked 1,117 Times in 1,031 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   -   Original Discussion by Gevni
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   -   Original Discussion by Gevni
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 23 November 2017, 12:16 PM EST
Location: Saskatchewan
Posts: 22,523
Thanks: 1,154
Thanked 4,274 Times in 3,947 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   -   Original Discussion by Gevni
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 23 November 2017, 10:59 AM EST
Location: Simplicity
Posts: 3,831
Thanks: 319
Thanked 1,284 Times in 1,162 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 10:40 AM
Whats wrong with this if-else skyineyes UNIX for Dummies Questions & Answers 8 12-12-2011 12:13 PM
Whats wrong with the following Larry_1 Homework & Coursework Questions 1 12-16-2010 03:51 PM
whats wrong with this? ferrycorsten73 UNIX for Dummies Questions & Answers 1 04-07-2009 03:41 PM
whats wrong with this find statement ? crackthehit007 Shell Programming and Scripting 2 03-26-2009 05:52 AM



All times are GMT -4. The time now is 02:30 PM.