1

having a little trouble with this rather basic script. This script works on Bash in my macbook pro, but not on my Linux Mint desktop, which also uses bash.

I can't figure out what's wrong with it.

I'm still getting an error from bash saying:

line 6: [: -lt: unary operator expected
line 16: [: -gt: unary operator expected

with this updated code:

#!/bin/bash
clear
counter=0

function countup {
while [ $counter -lt 500 ]
do
  ((counter++))
  echo $counter
  sleep 0.2
done
countdown
}

function countdown {
while [ $counter -gt 0 ]
do
  ((counter--))
  echo $counter
  sleep 0.2
done
countup
}

countup
4
  • 2
    add space after 0 => 0 ] and remove : in both functions
    – Costas
    Commented Apr 11, 2015 at 16:25
  • 1
    Take a look at: shellcheck.net
    – Cyrus
    Commented Apr 11, 2015 at 16:25
  • Shellcheck says it is all looking good, but I still get the error.
    – CYQ00000A
    Commented Apr 11, 2015 at 16:50
  • 2
    Works for me. Have you really posted the code you're running? Seems like $counter is empty in your real code (use "$counter").
    – choroba
    Commented Apr 11, 2015 at 16:53

1 Answer 1

0

Try this:

#!/bin/bash
clear
counter=0

function countup {
    while [[ $counter -lt 500 ]]; do
        ((counter++))
        echo $counter
        sleep 0.2
    done

    countdown
}

function countdown {
    while [[ $counter -gt 0 ]]; do
        ((counter--))
        echo $counter
        sleep 0.2
    done

    countup
}

countup

[[ ]] is a bit more robust to unvalued variables. But, this script does run. It seems that you typed it differently here than your machine. This also looks like it will count up and down forever, but that seems to be intentional.

You must log in to answer this question.

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