mallek-ahmed / Git-Repo

0 stars 0 forks source link

convertir_format.sh #21

Open mallek-ahmed opened 11 months ago

mallek-ahmed commented 11 months ago

how to use ./convertir_format.sh fichier.txt unix

!/bin/bash

Vérifier les arguments

if [ -z "$1" ] || [ -z "$2" ]; then echo "Usage: $0 " echo " doit être 'unix' ou 'windows'" exit 1 fi

Nom du fichier et format cible

fichier="$1" format="$2"

Déterminer le format actuel

if file "$fichier" | grep -q "CRLF"; then actuel="windows" else actuel="unix" fi

Afficher le format actuel

echo "Format actuel : $actuel"

Convertir le fichier si nécessaire

if [ "$actuel" != "$format" ]; then temp_file=$(mktemp) if [ "$format" == "unix" ]; then dos2unix < "$fichier" > "$temp_file" elif [ "$format" == "windows" ]; then unix2dos < "$fichier" > "$temp_file" else echo "Format inconnu: $format" exit 1 fi mv "$temp_file" "$fichier" echo "Fichier converti en format $format." else echo "Le fichier est déjà en format $format." fi