#!/bin/bash
# 检查参数个数是否正确
if [ $# -ne 3 ]; then
echo "Usage: $0 <source_file> <source_encoding> <target_encoding>"
exit 1
fi
source_file=$1
source_encoding=$2
target_encoding=$3
# 检查源文件是否存在
if [ ! -f $source_file ]; then
echo "Source file $source_file does not exist."
exit 1
fi
# 生成目标文件名
target_file="${source_file%.*}_$target_encoding.${source_file##*.}"
# 使用iconv进行编码转换
iconv -f $source_encoding -t $target_encoding $source_file -o $target_file
# 检查iconv命令是否成功执行
if [ $? -eq 0 ]; then
echo "Encoding conversion successful. Output file: $target_file"
else
echo "Encoding conversion failed."
fi