Useful bash scripts / scripts bash utiles
These scripts are distributed under GNU General Public License.
See COPYING file.
Copyright © 2003-2006 Thomas Harding
These programs are free software; you can redistribute them and/or modify
them 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.
These programs are distributed in the hope that they 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 these programs; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
File list / Liste des fichiers
File / fichier
display_version () {
cat <<EOF
Copyright (c)2003 Thomas Harding. <URL:mailto:thomas.harding@laposte.net>
get_rfc download RFCs
version 0.2
EOF
}
display_licence () {
display_version
cat <<EOF
EOF
}
DEFAULT_EXTENSION=.txt
GETCOMMAND=wget
PREPENDED_URL=http://www.rfc-editor.org/rfc/
LOCAL_REPOSITORY=~/Documentation/Standards/RFC/
URLS_LIST_FILE=${LOCAL_REPOSITORY}urls_list
FILE[1]="http://rfc-editor.org/rfc-index2.html"
FILE_LABEL[1]="rfc_desc"
FILE[2]="http://rfc-editor.org/rfc-index.html"
FILE_LABEL[2]="rfc_asc"
FILE[3]="ftp://ftp.isi.edu/in-notes/rfc-index.txt"
FILE_LABEL[3]="rfc_txt"
FILE[4]="ftp://ftp.ietf.org/ietf/1id-guidelines.txt"
FILE_LABEL[4]="Internet_Draft_Guidelines"
FILE[5]="ftp://ftp.isi.edu/in-notes/rfc2026.txt"
FILE_LABEL[5]="Internet_Standard_Process--Revision_3"
PREFERRED_RFC_LIST=${LOCAL_REPOSITORY}rfc-index.txt
lang=${LANG:0:2}
if [[ $lang == 'fr' ]]; then
MSG_NO_REPOSITORY="le répertoire n existe pas, \
les RFC seront chargées dans le répertoire courant"
MSG_GIVMERFC="indiquer un ou plusieurs numéros de RFC"
MSG_LOAD_RFC="chargement des RFC dans"
MSG_UNKNOWN_OPTION="option non reconnue"
MSG_UNKNOWN_METHOD="méthode non implémentée"
MSG_PROG_NOT_FOUND="pas trouvé le programme"
MSG_NON_ALLOWED_FILE_EXTENSION="type de fichier non autorisé pour une RFC : \
les extensions autorisées sont .txt et .pdf"
MSG_GET_HELP="taper -h pour l aide"
MSG_SELECT_IN_IDX="Choisissez un fichier parmi l index :"
MSG_NOT_IN_LIST="Cela ne figure pas dans la liste"
MSG_PLEASE_SELECT_BY_NBR="Choisissez par numéro SVP"
MSG_LOAD_URLS_LIST_FILE="Chargement de la liste des fichiers"
BREAK_LABEL="sortir"
SELECT_LABEL="selectionner"
PROCEED_LABEL="télécharger"
PS3="faites votre choix : "
display_usage () {
echo -n "USAGE
$0" >&2
cat >&2 <<EOF
[ arguments ... ][ liste de rfc ]
arguments :
-h|--help
affiche ce message et quitte
-V
affiche la version et quitte
-o OPTION VALEUR
modifie une OPTION parmi les suivantes :
GETCOMMAND (programme|'programme options')
LOCAL_REPOSITORY ~/mon/repertoire
DEFAULT_EXTENSION (.txt|.pdf)
PREPENDED_URL ("http://the.repository.org/rfc/")
URLS_LIST_FILE ("/ma/liste/fichiers/etiquettes")
syntaxe de liste : FILE[nbr]=URL
FILE_LABEL[nbr]=étiquette
--force-not-rfc
--get-by-list
met à jour à partir d une liste (interactif)
liste de rfc
([nom_du_fichier]|[numéro_de_rfc])[extension] etc...
EXEMPLES
get_rfc 1605 1776 2324 rfc2549.pdf rfc2795
get_rfc -o LOCAL_REPOSITORY /mon/repertoire -o DEFAULT_EXTENSION .pdf 1605
EOF
}
else
MSG_NO_REPOSITORY="directory do not exists, \
RFC will be loaded in current directory"
MSG_GIVMERFC="type one or more RFC number"
MSG_LOAD_RFC="loading RFCs in "
MSG_UNKNOWN_OPTION="unknown option"
MSG_UNKNOWN_METHOD="unknown method"
MSG_PROG_NOT_FOUND="can t find the program"
MSG_NON_ALLOWED_FILE_EXTENSION="authorized RFC filetypes: \
.txt and .pdf"
MSG_GET_HELP="type -h for help"
MSG_SELECT_IN_IDX="Select a file in index :"
MSG_NOT_IN_LIST="This is not in the list"
MSG_PLEASE_SELECT_BY_NBR="Please choice by number"
MSG_LOAD_URLS_LIST_FILE="Loading file list"
BREAK_LABEL="exit"
SELECT_LABEL="select"
PROCEED_LABEL="download"
PS3="please choice: "
display_usage () {
echo -n "USAGE
$0" >&2
cat >&2 <<EOF
[ arguments ... ][ rfc list ]
arguments :
-h|--help
display this message and exit
-V
display version and quit
-o OPTION VALUE
modify an OPTION among:
GETCOMMAND (program|'program options')
LOCAL_REPOSITORY ~/my/directory
DEFAULT_EXTENSION (.txt|.pdf)
PREPENDED_URL ("http://the.repository.org/rfc/")
URLS_LIST_FILE ("/my/file/list/labels")
syntaxe de liste : FILE[nbr]=URL
FILE_LABEL[nbr]=label
--force-not-rfc
--get-by-list
update from a list (interactive)
RFC list
([filename]|[rfc_nbr])[extension] [filename...
EXEMPLES
get_rfc 1605 1776 2324 rfc2549.pdf rfc2795
get_rfc -o LOCAL_REPOSITORY /my/directory -o DEFAULT_EXTENSION .pdf 1605
EOF
}
fi
get_options () {
while [[ "$1" ]]; do
case $1 in
-V)
display_licence
exit 0
;;
-h|--help)
display_usage
exit 0
;;
-o)
echo $2 | grep -E \
"(DEFAULT_EXTENSION|\
GETCOMMAND|\
PREPENDED_URL|\
LOCAL_REPOSITORY\
URLS_LIST_FILE)" &>/dev/null
if [[ $? -eq 0 ]] ; then
export $2="$3"
shift 3
(( OPTIONS_NBR += 3 ))
else
echo "$0: $2: $MSG_UNKNOWN_OPTION" >&2
echo "$MSG_GET_HELP" >&2
exit 1
fi
;;
--url-list-file)
export URLS_LIST_FILE="$2"
shift 2
(( OPTIONS_NBR += 2 ))
;;
--get-by-list)
go_to_download_directory
get_by_list
shift 1
(( OPTIONS_NBR += 1 ))
;;
--interactive)
go_to_download_directory
select_in_list
shift 1
(( OPTIONS_NBR += 1 ))
;;
--force-not-rfc)
FORCE_NOT_RFC=1
shift 1
(( OPTIONS_NBR += 1 ))
;;
--)
shift 1
(( OPTIONS_NBR += 1 ))
break
;;
-*)
echo "$0: $1: $MSG_UNKNOWN_OPTION" >&2
echo "$MSG_GET_HELP" >&2
exit 1
;;
*)
break
;;
esac
done
}
are_args_presents () {
if [ -z "$1" ] ; then
echo "$MSG_GIVMERFC" >&2
exit 0
fi
}
go_to_download_directory () {
if [ -d "$LOCAL_REPOSITORY" ]; then
cd $LOCAL_REPOSITORY
echo "$MSG_LOAD_RFC" "$LOCAL_REPOSITORY" >&2
else
echo "$0: $LOCAL_REPOSITORY: $MSG_NO_REPOSITORY" >&2
fi
}
is_extension_ok () {
FILE_EXTENSION=$( echo "$FILE_NAME" |
awk -F . '
{ $1 = "" }
{ getline }
{ ORS = "" }
{ for(i = 2 ; i <= NF ; i++) print "." $i }' )
case "$FILE_EXTENSION" in
'')
FILE_EXTENSION="$DEFAULT_EXTENSION"
echo -e "no file extension provided"
echo -n -e "\tfile extension will be "
echo -e "$FILE_EXTENSION"
;;
.txt|.pdf)
:
;;
*)
echo "$0: $FILE_EXTENSION $MSG_NON_ALLOWED_FILE_EXTENSION" >&2
return 1
;;
esac
}
is_rfc_name_ok () {
if [[ -z "${FILE_NAME//[0-9]/}" ]] ; then
echo -e "\trfc name is rfc$FILE_NAME"
export FILE_NAME=rfc$FILE_NAME
APPROVED=OK
else
return 1
fi
}
get_file () {
if [[ ! -x $( which $GETCOMMAND ) ]] ; then
echo "$0: $GETCOMMAND $MSG_PROG_NOT_FOUND" >&2
exit 1
fi
if [[ "$GETCOMMAND" == "wget" ]] ; then
$GETCOMMAND ${PREPENDED_URL}${FILE_NAME}
elif [[ $APPROVED == OK ]] ; then
$GETCOMMAND ${PREPENDED_URL}${FILE_NAME}
else
echo "$0: $GETCOMMAND: $MSG_UNKNOWN_METHOD" >&2
echo "proposed command is:"
echo "$GETCOMMAND ${PREPENDED_URL}${FILE_NAME}"
echo "proceed ? [y/N/a(ll)/q(uit)]"
read
case $REPLY in
Y|y)
echo "$GETCOMMAND ${PREPENDED_URL}${FILE_NAME}"
;;
a*|A*)
echo "$GETCOMMAND ${PREPENDED_URL}${FILE_NAME}"
APPROVED=OK
;;
Q*|q*)
exit 0
;;
esac
fi
}
download_rfcs () {
for FILE_NAME in "$@" ; do
echo -e "processing: $FILE_NAME"
if [[ ! $FORCE_NOT_RFC -eq 1 ]] ; then
is_extension_ok $FILE_NAME || { append_to_failed_list ; continue ;}
FILE_NAME=$( echo "$FILE_NAME" | sed -e 's/\(\.[[:print:]]*\)//g' )
is_rfc_name_ok || { append_to_failed_list ; continue ;}
FILE_NAME=${FILE_NAME}${FILE_EXTENSION}
else
:
fi
if [[ -f $FILE_NAME ]] ; then
echo "RFC is already in repository: skip"
else
get_file
fi
done
}
append_to_failed_list () {
FAILED_LIST[$failed_list_cnt]="$FILE_NAME"
(( failed_list_cnt += 1 ))
}
declare -a FAILED_LIST
(( failed_list_cnt = 0 ))
display_failed_list () {
echo $MSG_DOWNLOADS_FAILED $FAILED_LIST
}
get_by_list () {
LABEL_LIST="${FILE_LABEL[*]}"
SELECT_LABEL_LIST="$LABEL_LIST
$SELECT_LABEL
$PROCEED_LABEL
$BREAK_LABEL"
MANUAL_SELECT_CMD=select_in_list
PREVIOUS_PREPENDED_URL=$PREPENDED_URL
unset PREPENDED_URL
echo $MSG_SELECT_IN_IDX
select SELECTED_LABEL in $SELECT_LABEL_LIST ; do
if [[ $SELECTED_LABEL == $BREAK_LABEL ]] ; then
break
elif [[ $SELECTED_LABEL == $SELECT_LABEL ]] ; then
$MANUAL_SELECT_CMD
elif [[ $SELECTED_LABEL == $PROCEED_LABEL ]] ; then
get_by_batch
elif [[ -z "$SELECTED_LABEL" ]] ; then
echo $MSG_PLEASE_SELECT_BY_NBR
elif [[ $REPLY -le ${
FILE_NAME=${FILE[$REPLY]}
append_to_batch
unset FILE_NAME
else
echo $MSG_NOT_IN_LIST
fi
echo $MSG_SELECT_IN_IDX
done
PREPENDED_URL=$PREVIOUS_PREPENDED_URL
}
append_to_batch () {
if [[ -z "$BATCH" ]]; then
BATCH=$( mktemp )
fi
echo $FILE_NAME >>$BATCH
}
get_by_batch () {
if [[ -f "$BATCH" ]] ; then
count=$( wc -l $BATCH | awk '{ print $1 }' )
(( i = 1 ))
while ((i <= $count)) ; do
read FILE_NAME
get_file
unset FILE_NAME
((i += 1))
done <"$BATCH"
echo -n > $BATCH
fi
}
select_in_list () {
echo "interactive selection not implemented" >&2
echo "would you want to use your browser instead ? [Y/n]" >&2
read
case $REPLY in
Y|y|'')
lynx $PREFERRED_RFC_LIST
;;
*)
:
;;
esac
}
load_url_list_file () {
if [[ ( "$URLS_LIST_FILE" && -f $URLS_LIST_FILE ) ]] ; then
echo $MSG_LOAD_URLS_LIST_FILE >&2
. $URLS_LIST_FILE
fi
}
OPTIONS_NBR=0
go_to_download_directory
are_args_presents "$@"
get_options "$@"
shift $OPTIONS_NBR
download_rfcs "$@"