Enviar Respuesta  Enviar Tema 
[BASH] Convertir la codificación de todos los archivos de un directorio (iconv)
Autor Mensaje
warp
Q


Mensajes: 2,009
Grupo: Registrado
Registro en: Jun 2005
Estado: Sin Conexión
Reputación: 10
Mensaje: #1
[BASH] Convertir la codificación de todos los archivos de un directorio (iconv)

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...


01-09-2007 01:04 AM
Visita el website del usuario Encuentra todos los mensajes de este usuario Cita este mensaje en tu respuesta
Enviar Respuesta  Enviar Tema 

Posibles Temas Similares...
Tema: Autor Respuestas: Vistas: Ultimo Mensaje
  Bash Linux - HPLJ8000 carlosrr 3 163 03-06-2008 06:55 PM
Ultimo Mensaje: dragonauta
  [bash] Borrar archivos temporales .~ warp 1 2,701 12-29-2006 01:48 PM
Ultimo Mensaje: angelm
  SED, Reemplazar strings en archivos angelm 21 3,897 12-17-2006 10:53 PM
Ultimo Mensaje: p_eter

Ver la Versión para Impresión
Mandar este Tema a algún Amigo
Subscríbete a este Tema | Agrega este Tema a Tus Favoritos

Salto de Foro: