This commit is contained in:
PanSi21 2025-02-02 23:29:47 +01:00
parent cc70838a77
commit 9eec379464
Signed by untrusted user who does not match committer: PanSi21
GPG key ID: 755F8874C65EF462
2 changed files with 67 additions and 567 deletions

67
TesiZIP/modifica.sh Executable file
View file

@ -0,0 +1,67 @@
#!/bin/bash
# Controlla che sia stato fornito almeno un argomento
if [ $# -lt 1 ] || [ $# -gt 2 ]; then
echo "Uso: $0 [s] file.tex"
echo " - Se viene passato 's' come primo argomento, sovrascrive il file originale."
echo " - Altrimenti, crea un nuovo file con '-modificato' nel nome."
exit 1
fi
# Determina se sovrascrivere o meno
if [ "$1" == "s" ]; then
overwrite=true
input_file="$2"
else
overwrite=false
input_file="$1"
fi
# Verifica se il file esiste
if [ ! -f "$input_file" ]; then
echo "Errore: Il file '$input_file' non esiste!"
exit 1
fi
# Se non si sovrascrive, crea un nuovo nome file
if [ "$overwrite" = false ]; then
output_file="${input_file%.tex}-modificato.tex"
else
output_file="$input_file"
fi
# Variabile per tenere traccia se siamo dentro un blocco lstlisting
inside_lstlisting=0
# Creiamo un file temporaneo per la modifica
temp_file=$(mktemp)
# Legge il file riga per riga
while IFS= read -r line; do
# Controlla se inizia un blocco lstlisting
if [[ "$line" =~ \\begin\{lstlisting\} ]]; then
inside_lstlisting=1
fi
# Se siamo dentro lstlisting, non modificare la riga
if [ $inside_lstlisting -eq 1 ]; then
echo "$line"
else
# Sostituzione solo fuori dai blocchi lstlisting
echo "$line" | sed -E 's/"([^"]+)"/\\enquote{\1}/g'
fi
# Controlla se termina un blocco lstlisting
if [[ "$line" =~ \\end\{lstlisting\} ]]; then
inside_lstlisting=0
fi
done < "$input_file" > "$temp_file"
# Se deve sovrascrivere, sostituisce il file originale
if [ "$overwrite" = true ]; then
mv "$temp_file" "$input_file"
echo "File sovrascritto: $input_file"
else
mv "$temp_file" "$output_file"
echo "File convertito: $output_file"
fi