dimanche, décembre 22, 2024
Nom d'utilisateur : Mot de passe :
Home > Scripts Bash > Shell quickies > Gérer ses films, en bash !
[NEWS]
Envoyé par unreal
Cette fois-ci ce n'est pas un script 'système' mais plutôt un script rigolo qui permet de gérer facilement une base de données des films qu'on a vus, ou qu'on souhaite voir.

Il dispose des fonctionnalités suivantes :
- ajouter/supprimer un film
- rechercher
- changer de vu à pas vu et inversément
- lister les films vus, à voir, ou l'ensemble, en triant par nom ou date

Pour l'utiliser, il suffit de copier le code dans un fichier "movie", par exemple, qu'on rend exécutable avec chmod 755 movie.

Enjoy !

#!/bin/bash
#
# Movie script 1.0
# Keep track of your movie watching.
#
# Send bug reports to dev <at> slashorg <dot> net
#
# Last update: 20090525


#<config>
DBFILE="$HOME/.movie.db"
COLOR1="\033[32m"
#</config>

[ ! -f "$DBFILE" ] && touch "$DBFILE"

ACTION=$1
PARAM=$2

function cleanname {
        echo "$*" | sed -E -e 's/[ ]{2,}/ /g' -e 's/^[ ]+//' -e 's/[ ]+$//' -e 's/\./ /g' -e 's/[^a-zA-Z0-9 ]//g' | tr A-Z a-z
}

function missing {
        [ ! "$1" ] && echo " -> $2." && exit
}

function results {
        [ $1 -eq 0 ] && echo " -> No results found." && exit
        [ $1 -eq 1 ] && echo " - One result found -"
        [ $1 -ne 1 ] && echo " - $1 results found -"
}

case "$ACTION" in

        "add" )
                missing "$PARAM" "File name missing"
                NAME=`cleanname "$PARAM"`

                missing "$NAME" "File name may contain illegal characters"

                # Check for duplicate
                [ `cat "$DBFILE" | grep -i ":$NAME" | wc -l` -ne 0 ] && echo " -> $NAME already exists in database." && exit
                echo "`date +"%d-%m-%Y"`:$NAME" >> "$DBFILE"
                echo " -> Record added."
                ;;

        "remove" )
                missing "$PARAM" "File name missing"
                
                cat "$DBFILE" | grep -vi ":$PARAM" > "$DBFILE.new"
                if [ `diff "$DBFILE" "$DBFILE.new" | wc -l` -eq 0 ]; then
                        echo " -> Name not found."
                        rm -f "$DBFILE.new"
                        exit
                else
                        echo " -> One record deleted."
                        mv "$DBFILE.new" "$DBFILE"
                fi
                ;;

        "search" )
                missing "$PARAM" "Search query missing"

                SORTBY=2
                NAME=`cleanname "$PARAM"`
                RES="`cat $DBFILE | grep "$NAME" | sort -t ":" -k $SORTBY | awk 'BEGIN { FS = ":" } { print " \033[32m"$2"\033[31m"$3"\033[0m added "$1; }'`"
                COUNT=`echo "$RES" | sed '/^$/d' | wc -l | sed 's/[^0-9]//g'`

                results $COUNT
                echo "$RES"
                echo "# = unseen"
                ;;

        "flip" )
                missing "$PARAM" "File name missing"
                NAME=`cleanname "$PARAM"`

                [ `cat "$DBFILE" | grep -iE ":$NAME[:#]*$" | wc -l` -eq 0 ] && echo " -> $NAME cannot be found." && exit
                if [ "`cat "$DBFILE" | grep -i ":$NAME" | awk 'BEGIN { FS = ":" } { print $3; }'`" == "" ]; then
                        echo -e " -> Changing status to$COLOR1 unseen\033[0m."
                        cat "$DBFILE" | sed "s/:$NAME/:$NAME:#/" > "$DBFILE.new"
                else
                        echo -e " -> Changing status to$COLOR1 seen\033[0m."
                        cat "$DBFILE" | sed "s/:$NAME:#/:$NAME/" > "$DBFILE.new"
                fi
                mv "$DBFILE.new" "$DBFILE"
                ;;

        "list" | "seen" | "unseen" )
                SORTBY=1
                [ "$PARAM" == "date" ] && SORTBY=1
                [ "$PARAM" == "name" ] && SORTBY=2
                [ "$ACTION" == "list" ] && SEARCH="grep :"
                [ "$ACTION" == "seen" ] && SEARCH="grep -v :#$"
                [ "$ACTION" == "unseen" ] && SEARCH="grep :#$"
                RES="`cat $DBFILE | $SEARCH | sort -t ":" -k $SORTBY | awk 'BEGIN { FS = ":" } { print " \033[32m"$2"\033[0m added "$1; }'`"
                COUNT=`echo "$RES" | sed '/^$/d' | wc -l | sed 's/[^0-9]//g'`

                results $COUNT
                echo "$RES"
                ;;

        * )
                echo " - Usage -"
                echo -e "$COLOR1 $0 add <name>\033[0m - add <name> to database."
                echo -e "$COLOR1 $0 remove <name>\033[0m - remove <name> from database."
                echo -e "$COLOR1 $0 search <key>\033[0m - search database for <key>."
                echo -e "$COLOR1 $0 flip <name>\033[0m - change status from seen to unseen."
                echo -e "$COLOR1 $0 list [date|name]\033[0m - list complete database sorting by date or name."
                echo -e "$COLOR1 $0 seen [date|name]\033[0m - list seen database sorting by date or name."
                echo -e "$COLOR1 $0 unseen [date|name]\033[0m - list unseen database sorting by date or name."
                ;;
esac


Posté le 16/08/09 à 02:21

Gérer ses films, en bash !
Vous pourriez commenter si vous aviez un compte !