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
09-21-2011, 11:45 AM (This post was last modified: 09-21-2011 11:48 AM by tavvva.)
Post: #2
RE: Bash scripts
I would patch it a bit in order to optimise the speed ... it doesn't work well on faster computers without the sleep cycle and the key combination is crazy Big Grin
The following change introduce new key controls :
U - up
J - down
K - left
L - right

Code:
--- nibbles.sh.orig    2011-09-21 12:43:20.179494534 +0200
+++ nibbles.sh    2011-09-21 12:40:35.843408518 +0200
@@ -39,6 +39,7 @@
     ty=$((($RANDOM % $ys)))
     echo "ox=0; oy=0" >~/.nibblestail
     while : ; do
+    sleep 0.1
     if [ "$tx" = "$x" -a "$ty" = "$y" ] ; then
         l=$((($l + 1)))
         tx=$((($RANDOM % $xs)))
@@ -82,9 +83,9 @@
     key=none
     while [ ! -f ~/.nibblesstop ] ; do
     key=`dd bs=1 count=1 2>/dev/null`
-    [ "$key" = "h" ] && setdir -1 0
+    [ "$key" = "k" ] && setdir -1 0
     [ "$key" = "j" ] && setdir 0 1
-    [ "$key" = "k" ] && setdir 0 -1
+    [ "$key" = "u" ] && setdir 0 -1
     [ "$key" = "l" ] && setdir 1 0
     [ "$key" = "q" ] && echo >~/.nibblesquit
     done
Visit this user's website Find all posts by this user
Quote this message in a reply
05-10-2012, 07:10 PM
Post: #3
RE: Bash scripts
I found a bash script for creating a directory tree that I thought was really nice:
http://www.centerkey.com/tree/
Find all posts by this user
Quote this message in a reply
Post Reply 


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