• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

How to Fix 'iconv: illegal input sequence' Error in Bash

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
When working with file encoding conversions in Bash, you may encounter errors such as 'iconv: illegal input sequence'. This error often occurs during the encoding of files that contain unexpected byte sequences not conforming to the specified encoding format. In this article, we will discuss how to download files, unzip them, retrieve their file types and encoding formats, and convert them to UTF-8 safely.

Introduction


If you're looking for an efficient way to handle file downloads and encoding in Bash, you've come to the right place. The script you’re using is designed to download ZIP files, extract them, and convert their contents to UTF-8. However, issues like the 'illegal input sequence' error can derail automation efforts. Let's break down your task step-by-step, ensuring that all files can be processed into the desired encoding format.

Understanding the Encoding Issue


The 'illegal input sequence' error typically arises when iconv encounters byte sequences in the original file that are invalid for the specified source encoding. This can happen with files that have inconsistent character encodings or are corrupted.

To handle encoding more robustly in your script, here are some modifications and solutions:

Step 1: Modify Your Bash Script


Make sure your script can handle errors more gracefully. Let’s revise your existing Bash script to incorporate error handling and output checking.

#!/usr/bin/bash

# Create folders
mkdir -p data1
cd data1

# Downloading data
wget -q

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.


wget -q

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



# Unzipping data
for i in *.zip; do unzip -o $i; done

# Iterate over all the files in the data1 folder
for name in *; do
# Skip if it's a directory or a zip file
[ -d "$name" ] && continue
[[ "$name" == *.zip ]] && continue

# Getting extension
extension=${name##*.}

# Getting encoding format
encoding=$(file -i "$name" | sed 's/.*charset=//')

# Preparing output file name
name2=${name%.*}

# Encoding current file
if iconv -f "$encoding" -t UTF-8//TRANSLIT "$name" -o "conversion_$name2.$extension";
then
echo "Converted $name to UTF-8 successfully."
else
echo "Failed to convert $name. Check encoding." >&2
fi
done

# Moving old files to the 'old' folder
mkdir -p old
mv * !(*conversion_*) old


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу