OVH Cloud OVH Cloud

Idée d'exercice

9 réponses
Avatar
deub
Bonjour,

Je fais une mini-formation/initiation à l'écriture de scripts unix.
Je cherche une idée d'exercice final, un script à écrire qui demanderait
d'utiliser un peu de tout (expressions génériques, rationnelles, boucles ,
tests, exceptions ...).

9 réponses

Avatar
FrekoDing
deub ecrivait le 27.02.2006 12:46:

Bonjour,


Bonjour.

Je fais une mini-formation/initiation à l'écriture de scripts unix.
Je cherche une idée d'exercice final, un script à écrire qui demanderait
d'utiliser un peu de tout (expressions génériques, rationnelles, boucles ,
tests, exceptions ...).


des scripts de backup ?
@+

Avatar
Sébastien Monbrun aka TiChou
Dans le message <news:dtun8b$onn$,
*deub* tapota sur f.c.o.unix :

Bonjour,

Je fais une mini-formation/initiation à l'écriture de scripts unix.
Je cherche une idée d'exercice final, un script à écrire qui demanderait
d'utiliser un peu de tout (expressions génériques, rationnelles, boucles ,
tests, exceptions ...).


Un parser de logs générant des statistiques.

--
Sébastien Monbrun aka TiChou

Avatar
Stephane Chazelas
On Mon, 27 Feb 2006 12:46:14 +0100, deub wrote:
Bonjour,

Je fais une mini-formation/initiation à l'écriture de scripts unix.
Je cherche une idée d'exercice final, un script à écrire qui demanderait
d'utiliser un peu de tout (expressions génériques, rationnelles, boucles ,
tests, exceptions ...).
[...]


Une fonction qui nettoie $PATH: enleve tous les chemins relatifs
("", ".", "bin"), canonifie les chemins (/foo/ -> /foo,
/foo//./bar -> /foo/bar). Faire attention au cas ou $PATH n'est
pas defini. Que faire s'il est defini mais vide?

Qu'est-ce que tu entends par "exceptions"?

--
Stephane

Avatar
Pascal Bourguignon
"deub" writes:
Je fais une mini-formation/initiation à l'écriture de scripts unix.
Je cherche une idée d'exercice final, un script à écrire qui demanderait
d'utiliser un peu de tout (expressions génériques, rationnelles, boucles ,
tests, exceptions ...).


Évidement, ça pourrait être bien de choisir une application de script réelle.

Mais c'est amusant de proposer l'écriture de choses inattendues, comme
un client/serveur SMTP ou POP-3.

Par exemple, j'ai un serveur SMTP en moins de 70 lignes de bash, un
client SMTP en moins de 50 lignes de bash, et un client POP en moins
de 80 lignes de bash.



--
__Pascal Bourguignon__ http://www.informatimago.com/

"By filing this bug report you have challenged the honor of my
family. Prepare to die!"

Avatar
Bob qui Trolle
deub wrote:
Bonjour,

Je fais une mini-formation/initiation à l'écriture de scripts unix.
Je cherche une idée d'exercice final, un script à écrire qui dema nderait
d'utiliser un peu de tout (expressions génériques, rationnelles, bo ucles ,
tests, exceptions ...).


Un outil pour trier le courrier non-sollicité. Pour la beauté de
l'exercice, choisir des critères simples au départ (contenant une lig ne
commençant par et contenant ) et complexifier progressivement les
critères utilisables pour le tri.

Avatar
rixed
On 2006-02-27, Pascal Bourguignon wrote:
Par exemple, j'ai un serveur SMTP en moins de 70 lignes de bash,


Comme quoi bash est un serveur SMTP plus facile à configurer que
sendmail ;-p

Avatar
Thomas vO
bonjour,

À (at) Mon, 27 Feb 2006 18:38:11 +0100,
Pascal Bourguignon nous disait (told us):
Par exemple, j'ai un serveur SMTP en moins de 70 lignes de bash, un
client SMTP en moins de 50 lignes de bash, et un client POP en moins
de 80 lignes de bash.


est-ce que le source est disponible quelque part ? j'y connais pas
grand chose, mais j'y jetterai bien un oeil...

--
Thomas vO - <http://perso.enstimac.fr/~vanouden/>

Avatar
Pascal Bourguignon
Thomas vO writes:

bonjour,

À (at) Mon, 27 Feb 2006 18:38:11 +0100,
Pascal Bourguignon nous disait (told us):
Par exemple, j'ai un serveur SMTP en moins de 70 lignes de bash, un
client SMTP en moins de 50 lignes de bash, et un client POP en moins
de 80 lignes de bash.


est-ce que le source est disponible quelque part ? j'y connais pas
grand chose, mais j'y jetterai bien un oeil...


----(smtpd.sh)----------------------------------------------------------
#!/bin/bash
#*****************************************************************************
#FILE: smtpd.sh
#LANGUAGE: sh
#SYSTEM: POSIX
#USER-INTERFACE: NONE
#DESCRIPTION
#
# A very SIMPLE mail transfer agent.
#
# Put in inetd.conf:
# smtp stream tcp nowait mail /usr/local/bin/smtpd.sh smtpd.sh
#
#AUTHORS
# <PJB> Pascal Bourguignon
#MODIFICATIONS
# 2003-09-04 <PJB> Created.
#BUGS
#LEGAL
# GPL
#
# Copyright Pascal Bourguignon 2003 - 2003
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA 02111-1307 USA
#*****************************************************************************

mailboxes=/tmp/var/spool/mail
mkdir -p "$mailboxes" >/dev/null 2>&1 || true
echo "220 `hostname` SMTP SIMPLE mail transport agent Version 1.0"

done=0
while [ $done -eq 0 ] ; do
read command arguments
command="$(echo "$command" | tr -d '15')"
arguments="$(echo "$arguments" | tr -d '15')"
case "$command" in
[hH][eE][lL][oO]) echo "250 Hi $arguments" ; done=1 ;;
*) echo "503 Bad command" ;;
esac
done

s_idle=0 ; s_from=1 ; s_rcpt=2
state=$s_idle
while true ; do
read command arguments
command="$(echo "$command" | tr -d '15')"
arguments="$(echo "$arguments" | tr -d '15')"
case "$command" in
[Mm][Aa][Ii][Ll])
if [ 0 -eq $(( $state & $s_from )) ] ; then
from=$(echo "$arguments" | sed -e 's/^ *[Ff][Rr][Oo][Mm] *: *//')
state=$(( $state | $s_from ))
echo "250 OK"
else
echo "503 Bad sequence of commands."
fi
;;
[Rr][Cc][Pp][Tt])
if [ $s_from -eq $(( $state & ( $s_from | $s_rcpt ) )) ] ; then
to=$(echo "$arguments" | sed -e 's/^ *[Tt][Oo] *: *//')
state=$(( $state | $s_rcpt ))
echo "250 OK"
else
echo "503 Bad sequence of commands."
fi
;;
[Dd][Aa][Tt][Aa])
if [ $(( $s_from | $s_rcpt )) -eq $(( $state & ( $s_from | $s_rcpt ) )) ] ; then
file="${mailboxes}/$(echo "$to" | sed -e 's/^ *< *//' -e 's/ *> *$/1/' -e 's/@.*//')"
echo "From $from `date '+%a %b %d %H:%M:%S %Y'`" >> "$file"
echo "354 Enter mail, end with <CR><LF>.<CR><LF>"
data=1
while [ $data -eq 1 ] ; do
read line
line="$(echo "$line" | tr -d '15')"
if [ "$line" = "." ] ; then
data=0
state=$s_idle
echo "" >> "$file"
echo "250 OK"
else
echo "$line" >> "$file"
fi
done
else
echo "503 Bad sequence of commands."
fi
;;
[Nn][Oo][Oo][Pp]) echo "250 OK" ;;
[Rr][Ss][Ee][Tt]) state=$s_idle ; echo "250 OK" ;;
[Vv][Rr][Ff][Yy]) echo "252 Cannot VRFY." ;;
[Qq][Uu][Ii][Tt]) echo "221 Bye" ; exit 0 ;;
*) echo "502 Command not implemented." ;;
esac
done
#### smtpd.sh -- 2003-09-04 19:20:50 -- pascal ####

----(smtp.sh)-----------------------------------------------------------
#!/bin/bash
#*****************************************************************************
#FILE: smtp.sh
#LANGUAGE: sh
#SYSTEM: POSIX
#USER-INTERFACE: NONE
#DESCRIPTION
#
# Implements a naive SMTP client.
#
#AUTHORS
# <PJB> Pascal Bourguignon
#MODIFICATIONS
# 2004-03-01 <PJB> Created.
#BUGS
#LEGAL
# GPL
#
# Copyright Pascal Bourguignon 2004 - 2004
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA 02111-1307 USA
#*****************************************************************************
smtp_server=localhost
local_name=localhost
if [ "$1" = "--log" ] ; then shift ; traceìho ; else trace=true ; fi
from="$1" ; shift # the rest of arguments are envelop recipients
log=/dev/tty
message=/tmp/message.$$
trap "rm $message" 0
cat>$message
(
read line 0<&3 ; $trace "$line">$log
case "$line" in 2*) ;; *) echo "QUIT" 1>&3 ; exit 0 ;; esac
echo "HELO $local_name" 1>&3
read line 0<&3 ; $trace "$line">$log
case "$line" in 2*) ;; *) echo "QUIT" 1>&3 ; exit 0 ;; esac
echo "MAIL FROM: <${from}>" 1>&3
read line 0<&3 ; $trace "$line">$log
case "$line" in 2*) ;; *) echo "QUIT" 1>&3 ; exit 0 ;; esac
ok=0
for recipient ; do
echo "RCPT TO: <${recipient}>" 1>&3
read line 0<&3 ; $trace "$line">$log
case "$line" in
2*) ok=1 ;;
*) echo "Cannot send to ${recipient}">$log ;;
esac
done
if [ $ok -ne 0 ] ; then
echo "DATA" 1>&3
read line 0<&3 ; $trace "$line">$log
case "$line" in 3*) ;; *) echo "QUIT" ; exit 0 ;; esac
while read line ; do
case "$line" in
.*) echo ".$line" 1>&3 ;;
*) echo "$line" 1>&3 ;;
esac
done < $message
echo "." 1>&3
read line 0<&3 ; $trace "$line">$log
case "$line" in
2*) [ "$trace" != ":" ] && echo "Message sent">$log ;;
*) echo "Could not send the emssage.">$log ;;
esac
fi
echo "QUIT" 1>&3
) 3<>/dev/tcp/$smtp_server/25
exit 0
#### smtp.sh -- -- ####
----(pop.sh)------------------------------------------------------------
#!/bin/bash
#*****************************************************************************
#FILE: pop.sh
#LANGUAGE: sh
#SYSTEM: POSIX
#USER-INTERFACE: NONE
#DESCRIPTION
#
# A simple POP-3 client.
#
#AUTHORS
# <PJB> Pascal Bourguignon
#MODIFICATIONS
# 2004-01-17 <PJB> Created.
#BUGS
#LEGAL
# GPL
#
# Copyright Pascal Bourguignon 2004 - 2004
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA 02111-1307 USA
#*****************************************************************************

POP_SERVER=pop.afaa.asso.fr
POP_USER=pascal
POP_PASS=pari-fle
SPOOL=/tmp
delete=0 # set to 1 to delete messages from mailbox after transfer.
trace=0

function get_ok () {
read code one two three rest 0<&3
case "$code" in
+OK) true ;;
*) echo "Error, got: $code $one $two $three $rest" >&2 ;;# ; exit 1 ;;
esac
if [ $trace -ne 0 ] ; then echo "GOT $code" >&2; fi
}


function get_line () {
read line 0<&3
echo "$line" | tr -d '15'
}


if [ $trace -ne 0 ] ; then echo "Opening session with $POP_SERVER." >&2 ; fi
3<> /dev/tcp/$POP_SERVER/110
get_ok
echo "USER $POP_USER" 1>&3
get_ok
echo "PASS $POP_PASS" 1>&3
get_ok
if [ $trace -ne 0 ] ; then echo "LOGIN complete." >&2 ; fi
if [ "$three" -eq 0 ] ; then
echo "No message in mailbox." >&2
exit 1
fi

echo "LIST" 1>&3
get_ok
mcount=0
reading=1
while [ $reading -ne 0 ] ; do
read index size 0<&3
index=$(echo "$index" | tr -d '15')
if [ $trace -ne 0 ] ; then echo "$index $size" >&2 ; fi
case "$index" in
.) reading=0 ;;
*) message[$mcount]=$index ; mcount=$(($mcount + 1)) ;;
esac
done
if [ $trace -ne 0 ] ; then echo "GOT message list: $mcount messages." >&2 ; fi

mindex=0
while [ $mindex -lt $mcount ] ; do
if [ $trace -ne 0 ] ; then echo "Reading message ${message[$mindex]}" >&2 ; fi
echo "RETR ${message[$mindex]}" 1>&3
get_ok
file="$SPOOL/message-$$-${message[$mindex]}"
reading=1
while [ $reading -ne 0 ] ; do
read line 0<&3
line="$(echo "$line" | tr -d '15')"
case "$line" in
.) reading=0 ;;
*) echo "$line" >> "$file" ; if [ $trace -ne 0 ] ; then echo -n . >&2 ; fi ;;
esac
done
if [ $trace -ne 0 ] ; then echo '' >&2 ; fi
mindex=$(($mindex + 1))
if [ $delete -ne 0 ] ; then
if [ $trace -ne 0 ] ; then echo "Deleting message ${message[$mindex]}" >&2 ; fi
echo "DELE ${message[$mindex]}" 1>&3
get_ok
fi
done

if [ $trace -ne 0 ] ; then echo "Closing session with $POP_SERVER." >&2 ; fi
echo QUIT 1>&3
read line 0<&3

echo $SPOOL/message-$$-*
exit 0
#### pop.sh -- 2004-01-17 01:36:39 -- pascal ####
------------------------------------------------------------------------
--
__Pascal Bourguignon__ http://www.informatimago.com/
Our enemies are innovative and resourceful, and so are we. They never
stop thinking about new ways to harm our country and our people, and
neither do we. -- Georges W. Bush


Avatar
Deub
Merci pour vos suggestions. Je vais réfléchir à la meilleure solution.

Qu'est-ce que tu entends par "exceptions"?


Padron, je voulais dire signaux.