BASH Grouping tests in IF statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH Grouping tests in IF statement
# 1  
Old 06-28-2015
BASH Grouping tests in IF statement

Ok I got the following to work by removing the parentheses arount $year and it works. Can a kind soul please explain this behavior. Also, to my topic question, am I using curly braces correctly in the IF statement? The intent is to check for a leap year. If the year is divisible by 400 then no need to test further. If not, then the year must be divisible by 4 AND not be divisible by 100.

Code:
#!/bin/bash

# get year from user's terminal

echo "Type the year that you would like to test (4 digits), followed by [ENTER]:"

read year

if [[ $(( "$year" % 400 )) -eq "0" ]]  || { [[ $(( "$year" % 4 )) -eq "0" ]] && [[ $(( "$year" % 100 )) -ne "0" ]] ; } ;then
   echo "$year is a leap year"
else
   echo "$year is not a leap year"
fi

Ok I got it working by removing the parentheses. Why can I not quote a variable in an arithmetic expression?

Last edited by Riker1204; 06-28-2015 at 07:09 PM.. Reason: got it working
# 2  
Old 06-28-2015
It isn't immediately obvious to me why bash complains about your provided code. (But, I usually use ksh instead of bash.) A recent ksh runs it without complaining.

Both bash and ksh are happy with:
Code:
#!/bin/bash

# get year from user's terminal

echo "Type the year that you would like to test (4 digits), followed by [ENTER]:"

read year

if [[ $((year % 400)) == 0 || ( $((year % 4)) == 0 && $((year % 100)) != 0 ) ]]
then
   echo "$year is a leap year"
else
   echo "$year is not a leap year"
fi

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 06-28-2015
Double equals

Firstly thank you. Your code looks much better than mine. Why do you use double equals "==" ? What would be the consequences of using a single equals sign? Thanks in advance
# 4  
Old 06-28-2015
The correct syntax with test expr and [ expr ] for expressions performing numeric comparisons is -lt, -le, -eq, -ne, -gt, or -ge and for string comparisons is = and !=.

With [[ expr ]], the preferred operators for string and numeric comparisons are <, <=, ==, !=, >=, and >, but any of the forms used with test and [ ] can also be used. For [[ ]], = is deprecated and == is preferred.

Depending on what shell you're using, == may be accepted as a synonym for = in test and [ ], but == is not specified by the standards and does not work portably.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 06-28-2015
Thank you for explaining that. Marking as solved.
# 6  
Old 06-28-2015
Quote:
Originally Posted by Riker1204
Thank you for explaining that. Marking as solved.
You should notice that inside a $((...)) the expression is treated as if it were quoted and the variables may be referred by name without using the `$' symbol.
Code:
if [[ $((year % 400)) == 0 || ( $((year % 4)) == 0 && $((year % 100)) != 0 ) ]]

# 7  
Old 06-29-2015
In terms of efficiency, meaning to reduce the number of tests, I'd test the most probable one first, i.e. year % 4, which becomes 0 every fourth year, as opposed to year % 400, which becomes 0 every 400th year only (and not for the next 385 years., btw). So in the vast majority of cases, you'll be off with one single test only.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Bash statement equivalent

Hi all, i need a equivalent for the statement i run in bash, so it would also run in other shells. Specially i need it for ksh to run on AIX. Here the statements: exec > >(tee -a $log) exec 2> >(tee -a $log >&2) Thanks. (5 Replies)
Discussion started by: Kosak
5 Replies

2. Shell Programming and Scripting

[Solved] If statement in bash

I have the following code in bash, however "set red frmt" is not displayed. echo "iarg_rd = $iarg_rd" iarg_rd="2" if ; then echo "Hello World" fi if ; then frmt="${gap}${!frmt_titl_yl}" elif ; then frmt="${gap}${!frmt_titl_bk}" elif ; then echo... (2 Replies)
Discussion started by: kristinu
2 Replies

3. Shell Programming and Scripting

Bash - Nesting if statement in for loop

I have the basic command written in bash for element in 1 2 do if ]; then set el = "t" else set el = "p" fi done but i get the following error syntax error near unexpected token `for' ` for element in 1 2' What should i do differently? (3 Replies)
Discussion started by: ncwxpanther
3 Replies

4. Shell Programming and Scripting

BASH - case statement

Hi Gurus, I have the below BASH code which does not works for upper case alphabets except Z (upper case Z). What may be the reason. Also escape sequences like \n, \t, \b, \033(1m \033(0m (For bold letter) are not working. case $var in ) echo "Lower case alphabet" ;; ... (7 Replies)
Discussion started by: GaneshAnanth
7 Replies

5. UNIX for Dummies Questions & Answers

BASH Shell Scripting: If, Then Statement

I'm having trouble trying to create a BASH shell script. I want the user to input a command "cat file_name.c" and then the shell script will delete all comments "/* */" from file_name.c else exit. So far I have this: #!/bin/bash read "cat file" // User will input command cat... (7 Replies)
Discussion started by: inkjoy00
7 Replies

6. UNIX for Dummies Questions & Answers

Bash - OR within an IF statement

Hey guys, Currently trying to write a wee script that runs only when logged in as one of two users. The rest of the script is working fine, but no matter what user I try to run it as, it always fails! This is the puzzling part:if ]; then echo "Run script as admin " exit 1 else... (6 Replies)
Discussion started by: jimbob01
6 Replies

7. Shell Programming and Scripting

bash if statement help needed

Hi I need a script with an if statement that goes. I need it to search through all files within a directory with the extension .test if it finds the string '71502FSC1206' then do sed 's/71502FSC1206/\n&/g' > send.test If it finds the string '715MCH' or '715JAC' then I need it to move the... (1 Reply)
Discussion started by: firefox2k2
1 Replies

8. Shell Programming and Scripting

"if" statement grouping

Hi I work in ksh88. I am trying to group a few conditional statements to check for the value of the variable DATE, but getting a syntax error: $DATE=1836 $] && ]] || ] && ]]; then ksh: syntax error: `]]]' unexpected is there a way to user "OR" between first part: if ] && ] and... (3 Replies)
Discussion started by: aoussenko
3 Replies

9. UNIX for Dummies Questions & Answers

Conditional statement in bash

I want to combine 2 conditional statements by using -o in bash, but it won't work. if ; then echo "The number needs to be between 0 and $nr" fi Each time i execute the file it says: ./selectCitaat: line 10: syntax error near unexpected token `$1' (3 Replies)
Discussion started by: doc.arne
3 Replies

10. Shell Programming and Scripting

Bash: evaluating $? variable (if statement)

Hello, i'm unable to write a correct if... statement to evaluate the $? variable. Could anybody send to me an example? for example, this lines of code didn't work... if ; then etc etc if ; then etc etc Thank you in advanced. (5 Replies)
Discussion started by: aristegui
5 Replies
Login or Register to Ask a Question