Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bash scripts
09-21-2011, 11:28 AM
Post: #1
Bash scripts
Found or wrote a usefull BASH script and want it to share?
Maybe others can use or even improve it, I guess that one of the benefits of Open Source :]

I'll kick of with a simple game I found on the web, it's a simple nibble game (AFAIK it 's simulair to a wellknown cellphone game)

Code:
#!/bin/bash
# BashNibbles: An implementation of Nibbles in bash shell script.
# Use the vi cursor keys (hjkl) to control a snake moving around
# the screen. Pick up the #s; avoid crashing into your own tail.
# Terminal-independant (uses tput).
#
# About as good as the TI-83 version is. More challenging. For
# an even more challenging game, use a smaller xterm.
# Adam Sampson, ats@offog.org

wormdie () {
    tput clear
    echo "Adam's BashNibbles"
    echo
    echo $1
    echo Your score: `cat ~/.nibblesscore`
    echo >>~/.nibbleshs `cat ~/.nibblesscore` - `whoami` - `date`
    sort -r -n ~/.nibbleshs | head -10 >~/.nibbleshs2
    mv -f ~/.nibbleshs2 ~/.nibbleshs
    echo
    echo "High scores:"
    cat ~/.nibbleshs
    echo
    echo "Press any key to finish"
    echo >~/.nibblesstop
}

bgprocess () {
    tput clear
    xs=`tput cols`
    ys=`tput lines`
    l=1
    ox=0
    oy=0
    x=$((($xs / 2)))
    y=$((($ys / 2)))
    y=10
    tx=$((($RANDOM % $xs)))
    ty=$((($RANDOM % $ys)))
    echo "ox=0; oy=0" >~/.nibblestail
    while : ; do
    if [ "$tx" = "$x" -a "$ty" = "$y" ] ; then
        l=$((($l + 1)))
        tx=$((($RANDOM % $xs)))
        ty=$((($RANDOM % $ys)))
    fi
    if [ "$x" = "-1" -o "$x" = "$xs" -o "$y" = "-1" -o "$y" = "$ys" ] ; then
        wormdie "Hit the wall"
        return
    fi
    if grep -q "ox=$x ; oy=$y" ~/.nibblestail ; then
        wormdie "Hit your own tail"
        return
    fi
    if [ -f ~/.nibblesquit ] ; then
        wormdie "Quit while you were ahead"
        return
    fi
    tput cup $ty $tx ; echo -n "#"
    tput cup $y $x ; echo -n "o"
    tput cup $oy $ox ; echo -n " "
    echo "ox=$x ; oy=$y" >>~/.nibblestail
    tail -$l ~/.nibblestail >~/.nibblestail2
    mv -f ~/.nibblestail2 ~/.nibblestail
    eval $(head -1 ~/.nibblestail)
    tput cup 0 0 ; echo -n "Score: " $l
    x=$((($x + `cat ~/.nibblesdx`)))
    y=$((($y + `cat ~/.nibblesdy`)))
    echo $l >~/.nibblesscore
    done
}

setdir () {
    echo $1 >~/.nibblesdx
    echo $2 >~/.nibblesdy
}

nibbles() {
    bgprocess &
    stty -icanon min 1 time 0 -echo
    setdir 1 0
    key=none
    while [ ! -f ~/.nibblesstop ] ; do
    key=`dd bs=1 count=1 2>/dev/null`
    [ "$key" = "h" ] && setdir -1 0
    [ "$key" = "j" ] && setdir 0 1
    [ "$key" = "k" ] && setdir 0 -1
    [ "$key" = "l" ] && setdir 1 0
    [ "$key" = "q" ] && echo >~/.nibblesquit
    done
    stty sane
    rm -f ~/.nibbleskeypress ~/.nibblesstop ~/.nibblesquit ~/.nibblesscore
}

nibbles

it must bet be saved as nibbles.sh and don't forget to make it executable.

Hardware in use: Compaq PIII- 1GHZ, Geforce 4000, 256mb, 80gb HD
Dutch Delicate Linux information: http://www.delicatelinux.nl.ae
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Bash scripts - nl2stk - 09-21-2011 11:28 AM
RE: Bash scripts - tavvva - 09-21-2011, 11:45 AM
RE: Bash scripts - lmemsm - 05-10-2012, 07:10 PM

Forum Jump:


User(s) browsing this thread: 1 Guest(s)

Contact Us | DeLi(cate) Linux | Return to Top | Return to Content | Lite (Archive) Mode | RSS Syndication