PreguntasLinux

Versión Completa: [BASH] Convertir la codificación de todos los archivos de un directorio (iconv)
Actualmente estas viendo una versión simplificada de nuestro contenido. Para ver la versión completa en el formato correcto, dale click aquí
Este script les va a ayudar en la tarea de convertir la codificación (charset) de todos los archivos que contenega un directorio en perticular, utilizando para esto iconv (wikipedia).

Código:
#
# This package 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; version 2 dated June, 1991.
#
# By warptrosse (warptrosse@gmail.com)
#

#!/bin/bash

DIR=”"
HELP=”"
INPUT_CODESET=”"
OUTPUT_CODESET=”"
CHECKIV=”"
CHECKENC=”"
TEMP_FILES=”"
TEMP_DIR=”"

function encode_data (){
CHECKENC=$(iconv -l | grep -i $2)
if [ “$CHECKENC” == “” ]; then
echo -e “\nERROR: Bad encoding type. (use -h (| –help) for help)\n”
exit 1
fi
if [ -d “$1″ ]; then
TEMP_DIR=$1″temp_dir_enc/”
mkdir $TEMP_DIR > /dev/null
echo -e “\n>> START CONVERTION:\n”
for TEMP_FILES in $(find $1 -type f)
do
INPUT_CODESET=$(file –mime $TEMP_FILES)
if [[ -n $(echo $INPUT_CODESET | grep “text/”) ]]; then
INPUT_CODESET=$(echo $INPUT_CODESET | cut -d ‘=’ -f 2);
CHECKENC=$(iconv -l | grep -i $INPUT_CODESET)
if [ “$CHECKENC” == “” ]; then
echo -e “\nERROR: Bad encoding type of input file “$TEMP_FILES”. (use -h (| –help) for help)\n”
rm -R $TEMP_DIR > /dev/null
exit 1
else
INPUT_CODESET=$(echo $INPUT_CODESET | tr “[:lower:]” “[:upper:]”)
OUTPUT_CODESET=$(echo $OUTPUT_CODESET | tr “[:lower:]” “[:upper:]”)
if [ “$INPUT_CODESET” != “$OUTPUT_CODESET” ]; then
iconv –from-code=$INPUT_CODESET –to-code=$OUTPUT_CODESET $TEMP_FILES > $TEMP_DIR$(basename $TEMP_FILES)
mv -f $TEMP_DIR$(basename $TEMP_FILES) $TEMP_FILES
echo $TEMP_FILES [Converted]
else
echo $TEMP_FILES [Skiped]
fi
fi
fi
done
echo -e “\n>> END CONVERTION\n”
rm -R $TEMP_DIR > /dev/null
else
echo -e “\nERROR: Directory do not exist or not been correctly specified. (use -h (| –help) for help)\n”
exit 1
fi
}

CHECKIV=$(whereis iconv | grep iconv.h)
if [ -n “$CHECKIV” ]; then
while [ “$1″ != “” ]; do
case $1 in
-d | –directory ) shift
DIR=$1
;;
-e | –encoding ) shift
OUTPUT_CODESET=$1
;;
-l | –list ) shift
iconv -l
exit 0
;;
-h | –help ) shift
HELP=”OK”
;;
* ) echo “\nERROR: Unrecognized Option. (use -h (| –help) for help)\n”
exit 1
esac
shift
done

if [ “$HELP” != “OK” -a -n “$DIR” -a -n “$OUTPUT_CODESET” ]; then
encode_data $DIR $OUTPUT_CODESET
else
echo -e “\n——————————————————————————————-”
echo “Convert Encoding (v0.01) By Warptrosse”
echo “——————————————————————————————-”
echo -e “This script is useful to convert encoding type of all files of one directory”
echo -e “- Use next command to convert:\n convert_encoding.sh -d (| –directory) -e (| –encoding) \n”
echo -e “- Use next command to list all convertion types:\n convert_encoding.sh -l (| –list)\n”
exit 0
fi
else
echo -e “\nERROR: You must be installed iconv (http://www.gnu.org/software/libiconv/)\n”
exit 1
fi


saludos...

URLs de Referencia