0

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?

1
  • 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. Feb 22, 2011 at 13:59

4 Answers 4

2

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).

1

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
1

Try

 [ $MinuteNow -eq $i ]

See man test for more info.

0

In bash, use:

(( $MinuteNow == $i ))

as in:

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

If sh compatibility is needed, use:

[ "$MinuteNow" -eq "$i" ]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .