0

I wrote this:

game=1 # (Explained soon)
i=1
echo -e "Type a number and I will count to it."
read number

while { $i < $number };
  do
    echo -e "$i"
    sleep 1
    (( e ++ ))
  done
echo -e "$i."
echo -e "I just counted to $i."

This worked for me, but then I decided to be lazy, because of my coder instincts.

for game in { $game..10 };
  do
    echo -e "Would you like me to count $number more times again? y/n"
    read answer
    if [[ $answer == "n" ]]; then
        echo -e "Ok."
        exit 1
    else
        for game in { $i..$number };
          do
            echo -e "$number,"
            sleep 1
            (( $number ++ ))
          done
        echo -e "$number."
        echo -e "I just counted to $number."
    fi
  done

And like the feeling you get when you realize you've stayed up to late looking at memes, karma hit me HARD.

console:
Would you like me to count 10 times again? y/n
y
10,
11.
I just counted to 11.
^C
root@kali>$

Any Remedies?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 1
    Quick notes -- (1) http://shellcheck.net/ is your friend. (2) Don't use `echo -e`. It's nonstandard and doesn't behave reliably even if it's 100% certain your shell will only ever be bash; the POSIX spec itself says to use `printf` instead of any use of `echo` that calls for backslashes or `-n`. (3) `read -p "prompt" answer` avoids the need to do a separate `echo` altogether. – Charles Duffy Dec 01 '22 at 20:37
  • Also, is there a reason you just undid the formatting I added to your question? You absolutely do not need and should not add extra spaces between each line, as long as you have a row of three backticks above and below each code segment. – Charles Duffy Dec 01 '22 at 20:37
  • 2
    Also, `{$game..10}` doesn't work; never has, never will. Use a C-style `for` loop: `for ((i=game; i<10; i++)); do` -- see [BashPitfalls #33](https://mywiki.wooledge.org/BashPitfalls#for_i_in_.7B1...24n.7D); same for `for game in ${i..$number}; do`-- it needs to be `for ((game=$i; game – Charles Duffy Dec 01 '22 at 20:39
  • On the don't-use-`echo -e` thing, see [Why is printf better than echo?](https://unix.stackexchange.com/a/65819/3113) over at [unix.se], and the APPLICATION USAGE and RATIONALE sections of https://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html. Re: the assertions that `echo -e` isn't reliable even on bash, see the `xpg_echo` configuration flag, which can be set at compile time, or through the environment, or explicitly. – Charles Duffy Dec 01 '22 at 20:42
  • `printf '%s\n' {0..$var}` can work with `ksh` – Gilles Quénot Dec 01 '22 at 20:43
  • `(( $number ++ ))` should give you a syntax error. – user1934428 Dec 02 '22 at 09:18

0 Answers0