Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

MinuteNow=date +%M;

for ((i=2;i<=57;i+=5))
do
        if [ "$MinuteNow" == "$i" ]; then
                *************
        fi
done

The problem is for 2 and 7, the date commande output is 02 and 07 but the variable $i is 2 and 7... I tried to add a condition to change only these 2, but it was bugging the loop...

Can you give me a tip please?

share|improve this question
1  
Unix is an operating system. Saying "how do I do X in a Unix script" is like "how do I do X in a Windows programming language" without telling which of the hundred languages you use. –  grawity Feb 22 '11 at 13:59
add comment

4 Answers

Use "-eq" to compare numbers, then you wont have this problems:

if [ "$MinuteNow" -eq "$i" ]; then

should do it.

(there are lots of other possibilities, being ${MinuteNow#0} another simple one, but I thing using the right operator -eq instead of == is the better one).

share|improve this answer
add comment

In Bash, you will need to force the string to base 10:

if (( 10#$MinuteNow == i ))    # you can omit the dollar sign on bare variables in this context

In the Bourne shell (sh):

if [ $MinuteNow -eq $i ]

In zsh or ksh93:

if (( MinuteNow == i ))    # you can omit the dollar sign on bare variables in this context
share|improve this answer
add comment

Try

 [ $MinuteNow -eq $i ]

See man test for more info.

share|improve this answer
add comment

In bash, use:

(( $MinuteNow == $i ))

as in:

if (( $MinuteNow == $i )); then

If sh compatibility is needed, use:

[ "$MinuteNow" -eq "$i" ]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.