Mostrando entradas con la etiqueta Entretenimiento. Mostrar todas las entradas
Mostrando entradas con la etiqueta Entretenimiento. Mostrar todas las entradas

Extraer audio de Youtube.

d
Nombre: Youtube2Mp3.bash
Autor: @WizardIP
Descripción: Script que permite extraer audio de vídeos de Youtube
Motor del Script visto en Command Line Fu

#!/bin/bash
if [ $# -eq 0 ]
        then
               echo "¿Y el link?"
               echo "Uso: $0 [ URL del vídeo ]"
        exit 1
fi

url=$1
regexp='(https?\:\/\/([a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*)(\/\w+)+)'
if [[ $1 =~ $regexp  ]]; then
ifyoutubedl=`type -p youtube-dl`
        if [ -z $ifyoutubedl ]; then

                echo -e "\n\nyoutube-dl necesita estar instalado\n"
                exit 1
        else
        youtube-dl -t --extract-audio --audio-format mp3 $1
        echo "Sonido extraído"
        ls | grep *.mp3
        exit 0
        fi
else
        echo "No es una URL"
        exit 1
fi
exit 0

Permisos: chmod 700 Youtube2mp3.bash 

Ejecución ./Youtube2Mp3.bash. [ URL del vídeo ]
Leer más...

Tic Tac Toe

d
Nombre:ttt.bash
Autor: Chris F.A. Johnson
Descripción: Script que permite jugar Tic Tac Toe en la terminal contra la máquina.
Visto en cfaj
#!/bin/bash
# Sat Jul 12 16:52:30 EDT 2003
# NAME: ttt
# Copyright 2003, Chris F.A. Johnson
# Released under the terms of the GNU General Public License

ESC=$'\e'

tictactoe() {
[ $verbose -ge 1 ] && echo tictactoe $* >&2
    board=`printf "%9.9s" "        "`
    mvnum=1
    remainder=" 1 2 3 4 5 6 7 8 9 "
    show "$board"
    while :
    do
      status
      eval \$player$P
      [ $? -ne 0 ] && return
      put $_MOVE "$board"
      board=$_PUT
#      show "$board"
      mkbold "$_MOVE"
      printat 0 0 $mvnum
      printat $prompt
      sleep $pause &
      mlist="$mlist $_MOVE"
      remainder=${remainder/ $_MOVE / }

      checkboard "$board" && {
   mkbold $win
   printat $prompt "$cle"
   eval winner \$player$P
   return
      }

      [ $mvnum -eq 9 ] && {
   B=$U mkbold "$_MOVE"
   winner "Neither player"
   return
      }
      wait
      B=$U mkbold "$_MOVE"
      if [ $P = O ]
      then
   P=X
   OP=O
      else
   P=O
          OP=X
      fi
      mvnum=$(( $mvnum + 1 ))
    done
}

winner() {
[ $verbose -ge 1 ] && echo winner $* >&2
     printat $prompt "$cle $* wins\n\n"
}

status() {
[ $verbose -ge 1 ] && echo status $* >&2
    if [ $verbose -ge 1 ]
    then
 printat 4 0
 printf "${cle}Move: %s Player: %s Board: \"%s\"  Last move: %s  Move list: %s" \
     "$mvnum" "$P" "${board// /-}" "$_MOVE" "$mlist"
    else
 false
    fi
}

show() {
[ $verbose -ge 1 ] && echo show $* >&2
    printat 0 0 $mvnum
    printat 0 $top  ##$(( ($LINES - 5) / 2 ))
    printf "  %${margin}.${margin}s | %1s | %1s\n" "${1:0:1}" "${1:1:1}" "${1:2:1}"
    printf "%${margin}.${margin}s---+---+---\n" " "
    printf "  %${margin}.${margin}s | %1s | %1s\n" "${1:3:1}" "${1:4:1}" "${1:5:1}"
    printf "%${margin}.${margin}s---+---+---\n" " "
    printf "  %${margin}.${margin}s | %1s | %1s\n" "${1:6:1}" "${1:7:1}" "${1:8:1}"
}

put() {
[ $verbose -ge 1 ] && echo put $1 \""$2"\" >&2
   local n=$(( $1 - 1 ))
   _PUT=${2:-$board}
   [ "${_PUT:$n:1}" = " " ] &&
            _PUT=${_PUT:0:$n}$P${_PUT:$1}
}

checkboard() {
[ $verbose -ge 1 ] && echo checkboard \"$*\" >&2
    case $1 in
 $P??$P??$P??) win="1 4 7" ;;
 ?$P??$P??$P?) win="2 5 8" ;;
 ??$P??$P??$P) win="3 6 9" ;;
 ??$P?$P?$P??) win="3 5 7" ;;
        $P???$P???$P) win="1 5 9" ;;
 $P$P$P??????) win="1 2 3" ;;
 ???$P$P$P???) win="4 5 6" ;;
 ??????$P$P$P) win="7 8 9" ;;
        *) false ;;
    esac
}

mkbold() {
[ $verbose -ge 1 ] && echo mkbold $* >&2
  for sq in $*
  do
    row=$(( ($sq - 1) / 3 ))
    col=$(( ($sq - 1) % 3 + 1 ))
    printat $(( ($col * 4) + $margin - 2 )) $(( ($row * 2) + $top ))
    printf "${B}$P${U}"
  done
}

human() {
[ $verbose -ge 1 ] && echo human $* >&2
   while :
   do
       key "Select ($remainder)"
#       case $_MOVE in
#   "$ESC") read -sn1; read -sn1 kp; continue ;;
#       esac
       _MOVE=$_KEY
       case $_MOVE in
    1|2|3|4|5|6|7|8|9) [ "${board:_MOVE-1:1}" = " " ] && break ;;
    q|Q|x|X) echo; return 5 ;;
    p) printat $prompt
       KEYECHO= KEYMAX=$(( $COLUMNS - 10 )) key "Enter delay in seconds"
       pause=$_KEY
       ;;
    "") set -- $remainder
        _MOVE=$1
        [ $verbose -ge 1 ] && echo "|$_MOVE|" >&2
        break ;;
    *) printat $prompt
       printf "\r%s%s\r" "  "$cle" $_MOVE: Invalid move"
       sleep 1
       printf "%s\r" "$cle" ;;
       esac
   done
}

randy() {
[ $verbose -ge 1 ] && echo randy $* >&2
    randstr $remainder
    _MOVE=$_RANDSTR
}

key() {
[ $verbose -ge 1 ] && echo key $* >&2
    printat $prompt "$cle$CVIS$beep"
    read -${KEYECHO}n$KEYMAX -p "${1:-PAK}: " _KEY 2>&1
    printf "$CINV"
}

getkey() {
    local OKchars=${1:-${remainder// /}}
    local kp2 kp3
    stty -echo
    while :
      do
      prompt="==>"
      prompt
      IFS= read -r -sn1 -p " " kp 2>&1 || exit 2 #cleanup
      case $OKchars in
          *"$kp"*)
              case $kp in
                  $ESC)
                      read -st1 -n1 kp2
                      case $kp2 in
                          \[) read -st1 -n1 kp3
                              case $kp3 in
                                  A) kp=$UP; break ;;
                                  B) kp=$DN; break ;;
                                  C) kp=$RT; break ;;
                                  D) kp=$LF; break ;;
                              esac
                              ;;
                       esac
                       ;;
                  *) break ;;
              esac
              ;;
       esac
    done
    _KEY=$kp
}

randstr() {
[ $verbose -ge 1 ] && echo randstr $* >&2
#    [ -n "$1" ] || return 1
    n=$(( ($RANDOM % $#) + 1 ))
    eval _RANDSTR=\${$n}
}

isblock() {
[ $verbose -ge 1 ] && echo isblock $* >&2
    win=
    block=
    for _MOVE in $remainder
    do
        P=$OP put $_MOVE
 P=$OP checkboard "$_PUT" && return
    done
    _MOVE=
}

block() {
[ $verbose -ge 1 ] && echo block $* >&2
    block=
    for _MOVE in $remainder
    do
        put $_MOVE
 checkboard "$_PUT" && return
        P=$OP put $_MOVE
 P=$OP checkboard "$_PUT" && block=$_MOVE
    done
    [ "$block" ] && { _MOVE=$block; return; }
    randy
}

computer() {
[ $verbose -ge 1 ] && echo computer $* >&2
   local r c
   case $mvnum in
       1) randstr 1 3 7 9
   _MOVE=$_RANDSTR ;;
       2)
    if [ "${board:4:1}" = " " ]
    then
        _MOVE=5
    else
        randstr 1 3 7 9
        _MOVE=$_RANDSTR
    fi
    ;;
       3)  r=${mlist// /}
    c='1 3 7 9'
    randstr ${c//[$r]/}
#    randstr ${remainder//[$r]/}
    _MOVE=$_RANDSTR
    ;;
       4)
    isblock
    [ "$_MOVE" ] && return
     r=${mlist// /}
     c='2 4 6 8'
     randstr ${remainder//[$r]/}
     _MOVE=$_RANDSTR
     ;;
       *) block ;;
   esac
}

printat() { #== print arguments 3-... at Y=$2 X=$1
[ $verbose -ge 1 ] && echo printat $* >&2
    [ $# -lt 2 ] && return 1
    local y=$2
    local x=$1
    shift 2
    msg="$*"
    printf "\e[%d;%dH%b" $y $x "$msg"
}

version()
{
    echo "    $progname, version $version
    Copyright $copyright, $author $email
    This is free software, released under the terms of the GNU General
    Public License.  There is NO warranty; not even for MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.
"
}

usage()
{
echo "
    ${B}NAME${U}:  $progname - Tic-Tac-Toe

    ${B}USAGE${U}: $progname [OPTIONS]

    ${B}OPTIONS${U}:

        -x player1         - specify first player
        -o player2         - specify second player
        -c player1,player2 - specify both players
        -p seconds         - time to pause between moves

        -h, --help       - help: print this message
        -H, --help_long  - help: print more detailed message (if available)
        -v, --verbose    - sends messsages to \$HOME/tttlog
        -V, --version    - print version information

    Copyright 2003, Chris F.A. Johnson
"
}

verbose=0
longusage=0
version="1.0"
copyright=2003
author="Chris F.A. Johnson"
progname=${0##*/}
P=X
OP=O

playerX=human
playerO=computer
co=
li=

if tput ce >/dev/null 2>&1
then ## e.g. FreeBSD
    co=co
    li=li
elif tput el 2>/dev/null
then ## e.g. Linux
    co=cols
    li=lines
fi

UNBOLD=$'\E[0m'
standout=$'\E[0;1;7m'
cle=$'\E[K'
clb=$'\E[1K'
ULINE=$'\E[0;4m'
REVERSE=$'\E[0;7m'
BLINK=$'\E[0;5m'
BOLD=$'\E[0;1m'
CINV=$'\E[0;8m'

R=$REVERSE
U=$UNBOLD
B=$BOLD
BR=$B$R
Cl=$'\f'
if [ "$co" ]
then
    tput reset
    COLUMNS=${COLUMNS:=`tput $co`}
else
    COLUMNS=${COLUMNS:-80}
fi
top=6
margin=11
prompt="3 $(( $top + 9 ))"
beep=  #$'\a'
pause=1
KEYMAX=1
KEYECHO=s

while getopts vVhH-:xoX:O:c:p: var
do
 case "$var" in

     x) playerX=human
 playerO-computer
 ;;
     o) playerO=human
 playerX=computer
 ;;
     X) playerX=$OPTARG ;;
     O) playerO=$OPTARG ;;
     c|C) playerO=${OPTARG#*,}
   playerX=${OPTARG%,*}
   ;;
     p) pause=$OPTARG ;;

    -) case $OPTARG in
         help) usage; exit ;;
         verbose) verbose=$(( $verbose + 1 )) ;;
         version) version; exit ;;
       esac
       ;;
    h) usage; exit ;;
    H) longusage=1; usage; exit ;;
    v) verbose=$(( $verbose + 1 )) ;;
    V) version; exit ;;
    *);;
 esac
done
shift $(( $OPTIND - 1 ))

if [ "$co" ]
then
    COLUMNS=`tput $co`
else
    COLUMNS=${COLUMNS:-80}
fi
margin=11  ##$(( ($COLUMNS - 11) / 2 ))
clear

if [ $verbose -ge 1 ]
then
    exec 2>$HOME/tttlog
    set -x
fi

tictactoe
printat $prompt "\n$CVIS\n"
Permisos: chmod 700 tt.bash 
Ejecución: ./ttt.bash
Leer más...

Juego del Ahorcado.

d
Nombre: Ahorcado.bash
Autor: blackman
Descripción: Script que permite jugar "Ahorcado" en la terminal
Visto en Espacio Linux
El script hace uso de un archivo de texto donde va sacando las palabras a adivinar
#!/bin/bash

function message() {
    case $mensage in
 game_banner) #tput cup 1 $cols
   echo
   tput rev
   echo "$(tput setaf 4)EL JUEGO DEL AHORCADO EN BASH SCRIPT$(tput sgr0)"
   echo
   echo "$(tput setaf 7)$(tput bold)Contiene $(tput setaf 1)$(( $long_palabra-$espacios ))$(tput setaf 7) caracteres sin contar los espacios"
   echo "$(tput setaf 7)Puedes usar los comodines. Dispones de $(tput setaf 1)$comodines$(tput setaf 7). Para usarlos pulsa $(tput setaf 1)+"
   [[ $(( 6-intentos )) -lt 2 ]] && vida="vida" || vida="vidas"
   echo "$(tput setaf 7)Dispones de $(tput setaf 1)$(( 6-intentos )) $vida"
   #echo "$(tput setaf 7)El nombre de este país es $(tput setaf 1)$palabra"¡
   echo
   echo "$(tput setaf 7)El nombre de este país es: $(tput setaf 1)$word"
   echo -n "$(tput setaf 7)Vidas: $(tput setaf 1)"
   for (( i=1; i<=$(( 6-intentos )); i++ )); do echo -n $'\342\231\245'; done; echo
   echo -n "$(tput setaf 7)Comodines: $(tput setaf 1)"
   for (( i=1; i<=$comodines; i++ )); do echo -n $'\342\230\245'; done; echo
   echo "$(tput setaf 7)Caracteres restantes: $(tput setaf 1)$(( ($long_palabra-$espacios)-$aciertos ))"
   echo "$(tput setaf 7)Puntuación: $(tput setaf 1)$score";;
 game_over)  echo "$(tput setaf 2)Fenomenal, eres un crack";;
 game_read) read -n1 -p "$(tput setaf 7)Vida $(tput setaf 1)$intentos $(tput setaf 7)de $(tput setaf 1)5$(tput setaf 7). Introduce una letra: $(tput setaf 1)" caracter
   echo;;
 salir)  echo
   tput rev
   echo "$(tput setaf 1)GameOver$(tput sgr0)"
   echo "$(tput setaf 7)$(tput bold)Tu mejor puntuación es: $(tput setaf 1)$best_score"
   echo "$(tput setaf 7)Tu puntuación acumulada es: $(tput setaf 1)$acumulado"
   echo
   echo "$(tput setaf 7)La solución es: $(tput setaf 1)$palabra"
   echo "$(tput setaf 7)Que deseas hacer?"
   read -n1 -p "$(tput setaf 7)Pulsa $(tput setaf 1)Q $(tput setaf 7)para $(tput setaf 1)salir $(tput setaf 7)o $(tput setaf 1)G $(tput setaf 7)para $(tput setaf 1)volver a jugar$(tput setaf 7). Introduce una letra: $(tput setaf 1)" caracter
   echo "$(tput setaf 7)";;
 comodines_restantes) echo "$(tput setaf 4)Te dispones a usar un comodín. Suerte, sólo dispones de $(tput setaf 1)$comodines"
    echo "$(tput setaf 2)Comodín = $(tput setaf 1)$caracter";;
 comodines_finalizados) echo "$(tput setaf 4)Ups, has gastado todos los comodines";;
 continuar) read -n1 -p "$(tput setaf 1)Pulsar cualquier tecla para continuar$(tput setaf 7)";;
 acierto_mal) echo "$(tput setaf 4)Has fallado, ahora tienes un intento menos";;
 #acierto_letra) echo "$(tput setaf 7)Letra introducida: $(tput setaf 1)$letra";;
 acierto_bien) echo "$(tput setaf 2)Muy bien, ya casi lo tienes";;
 acierto_repetido) echo "$(tput setaf 3)Upps, Esa letra ya la has introducido";;
    esac
}
function init {
#file="diccionario"
    file="paises"
    max=$(cat $file | wc -l)
    num=$(( $RANDOM%$max ))
    palabra=$(head -$num $file | tail -1)
    aciertos=0
    #diccionario
    #palabra=$(echo ${palabra%% *,,} | sed 's/á/a/g;s/é/e/g;s/í/i/g;s/ó/o/g;s/ú/u/g')

    palabra=$(echo ${palabra,,} | sed 's/á/a/g;s/é/e/g;s/í/i/g;s/ó/o/g;s/ú/u/g;s/-/ /g;s/Á/a/g')
    long_palabra=${#palabra}
    comodines=$(( long_palabra/4 ))
    score=$(( (comodines*50)+500 ))
    #PROVISIONAL
    ###########################
    #[[ $long_palabra -lt 15 ]] && main;
    ###########################
    unset array[*]
    i=0; espacios=0; unset word
    while [[ $i -le $(( $long_palabra-1 )) ]]
 do
     caracter="${palabra:$i:1}"
     if [[ $caracter = " "  ]]
  then
      (( espacios++ ))
      caracter=" "
  else
      array=( ${array[*]} $caracter )
      caracter="-"
     fi
     word="$word $caracter"
     (( i++ ))
    done
}

function comodin {
    if [[ $comodines -gt 0 ]]
 then
     long_array=$(( ${#array[*]}-1 ))
     index=$(( $RANDOM%${#array[*]} ))
     caracter=${array[$index]}
     i=0
     while [[ $i -le $long_array ]]
  do
      if [[ ${array[$i]} = $caracter ]]
   then
       unset array[$i]
       array2=( ${array2[*]} $caracter )
       (( aciertos++ ))
       score=$(( score+50 ))
      fi
      (( i++ ))
     done
     array=( ${array[*]} )
     array2=( ${array2[*]} )
     (( comodines-- ))
     (( intentos-- ))
     mensage="comodines_restantes"; message mensage
    else
 (( intentos-- ))
 mensage="comodines_finalizados"; message mensage
    fi
    mensage="continuar"; message mensage
    game
}

function acierto {
    unset find
    long_array=$(( ${#array[*]}-1 ))
    i=0
    while [[ $i -le $long_array ]]
 do
     if [[ ${array[$i]} = $caracter ]]
  then
      unset array[$i]
      find="true"
      array2=( ${array2[*]} $caracter )
      (( aciertos++ ))
      score=$(( score+100 ))
     fi
     (( i++ ))
    done
    if [[ $find = "true" ]]
 then
     mensage="acierto_bien"; message mensage
     (( intentos-- ))
 else
     long_array2=$(( ${#array2[*]}-1 ))
     i=0
     while [[ $i -le $long_array2 ]]
  do
      if [[ ${array2[$i]} = $caracter ]]
   then
       find="true"
      fi
      (( i++ ))
     done
     if [[ $find = "true" ]]
  then
      mensage="acierto_repetido"; message mensage
      (( intentos-- ))
      score=$(( score-10 ))
  else
      mensage="acierto_mal"; message mensage
      score=$(( score-50 ))
     fi
    fi
    array=( ${array[*]} )
    array2=( ${array2[*]} )
    mensage="continuar"; message mensage
    game
}
function game {
    clear
    lines=$(( $(tput lines)/10 ))
    cols=$(( $(tput cols)/10 ))
    i=0; unset word;
    while [[ $i -le $(( $long_palabra-1 )) ]]
 do
     letra="${palabra:$i:1}"
     if [[ $letra = " "  ]]
  then
      letra=" "
  else
      unset letra_acertada
      for x in ${array2[*]}
   do
       if [[ $x = $letra ]]
    then
        letra_acertada="true"
       fi
      done
      if [[ $letra_acertada != "true" ]]
   then
       letra="-"
      fi
     fi
     word="$word $letra"
     (( i++ ))
    done
    (( intentos++ ))
    mensage="game_banner"; message mensage
    dibujo
    if [[ $intentos -gt 5 ]]
 then
     [[ $best_score -eq 0 ]] && best_score=0
     [[ $acumulado -eq 0 ]] && acumulado=0
     salir
    fi

    if [[ -z ${array[*]} ]]
 then
     [[ $score -gt $best_score ]] && best_score=$score
     acumulado=$(( acumulado+score ))
     mensage="game_over"; message mensage
     salir
    fi
    mensage="game_read"; message mensage
    if [[ $caracter = "+" ]]
 then
     comodin
    fi
    acierto
}

function dibujo {
    case $intentos in
 1) echo "________________ "
  echo "               | "
  echo "               | "
  echo "               O "
  echo;;
 2) echo "________________ "
  echo "               | "
  echo "               | "
  echo "               O "
  echo "               | "
  echo "               | "
  echo;;
 3) echo "________________ "
  echo "               | "
  echo "               | "
  echo "               O "
  echo "              \| "
  echo "               | "
  echo;;
 4) echo "________________ "
  echo "               | "
  echo "               | "
  echo "               O "
  echo "              \|/"
  echo "               | "
  echo;;
 5) echo "________________ "
  echo "               | "
  echo "               | "
  echo "               O "
  echo "              \|/"
  echo "               | "
  echo "              /  "
  echo;;
 *) echo "________________ "
  echo "               | "
  echo "               | "
  echo "               O "
  echo "              \|/"
  echo "               | "
  echo "              / \\"
  echo;;
    esac
}
function salir {
    mensage="salir"; message mensage
    case $caracter in
 q) clear;exit;;
 g) unset word; unset intentos; unset comodines; unset aciertos; unset score; unset array[*]; unset array2[*];main;;
 *) clear; mensage="game_banner"; message mensage; dibujo; salir;;
    esac
    clear
}

function main {
    init
    game
}
main
Permisos: chmod 700 ahorcado.bash 
Ejecución: ./ahorcado.bash
Y a jugar! :D
Leer más...

Nieve en la Terminal

d
Llego la navidad para nuestra terminal demasiado tarde :) pero llego.
Asi que se puede ver que con bash se puede realizar cosas interesantes



#!/bin/bash
LINES=$(tput lines)
COLUMNS=$(tput cols)

declare -A snowflakes
declare -A lastflakes

clear

function move_flake() {
i="$1"

if [ "${snowflakes[$i]}" = "" ] || [ "${snowflakes[$i]}" = "$LINES" ]; then
snowflakes[$i]=0
else
if [ "${lastflakes[$i]}" != "" ]; then
printf "\033[%s;%sH \033[1;1H " ${lastflakes[$i]} $i
fi
fi

printf "\033[%s;%sH❄\033[1;1H" ${snowflakes[$i]} $i

lastflakes[$i]=${snowflakes[$i]}
snowflakes[$i]=$((${snowflakes[$i]}+1))
}

while :
do
i=$(($RANDOM % $COLUMNS))

move_flake $i

for x in "${!lastflakes[@]}"
do
move_flake "$x"
done

sleep 0.1
done
Guardamos como nieve.sh
chmod +x nieve.sh 
y lo ejecutamos
./neige.sh
Leer más...