Created
January 6, 2025 10:07
-
-
Save vijinho/8c8bc86754651e82315beb82057f50db to your computer and use it in GitHub Desktop.
bash shell script to transliterate a file - that is, to strip any accents from the file and make it standard English alphabetic characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Parse command-line options | |
while getopts ":f:" opt; do | |
case ${opt} in | |
f) | |
file=$OPTARG | |
;; | |
\?) | |
echo "Usage: $0 [-f /path/to/file] or [--file /path/to/file]" | |
exit 1 | |
;; | |
:) | |
echo "Invalid option: -$OPTARG requires an argument" 1>&2 | |
exit 1 | |
;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
# Check if the file argument is provided | |
if [ -z "$file" ]; then | |
echo "Error: No file specified." | |
echo "Usage: $0 [-f /path/to/file] or [--file /path/to/file]" | |
exit 1 | |
fi | |
# Check if the file exists | |
if [ ! -f "$file" ]; then | |
echo "Error: File not found: $file" | |
exit 1 | |
fi | |
new_name=$(echo "$file" | iconv -f UTF-8 -t ASCII//TRANSLIT) | |
# Check if the new name is different from the original name | |
if [ "$new_name" != "$file" ]; then | |
mv -- "$file" "$new_name" | |
echo "File renamed successfully: $file -> $new_name" | |
else | |
echo "No change needed. File name already in ASCII: $file" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment